pellepl / spiffs

Wear-leveled SPI flash file system for embedded devices
MIT License
1.52k stars 402 forks source link

Fix possible source of overflow in SPIFFS_check #270

Closed samcaulfield-ubotica closed 4 years ago

samcaulfield-ubotica commented 4 years ago

Hi there,

I was testing a file system configuration with a logical page size of 32 KiB and got the following compiler warning:

In file included from spiffs/src/spiffs.h:14:0,
                 from spiffs/src/spiffs_check.c:23:
spiffs/src/spiffs_check.c: In function 'spiffs_page_consistency_check_i':
../shared/spiffs_config.h:223:43: warning: large integer implicitly truncated to unsigned type [-Woverflow]
 #define SPIFFS_CFG_LOG_PAGE_SZ(ignore)    (32 * 1024)
                                           ^
spiffs/src/spiffs_check.c:524:41: note: in expansion of macro 'SPIFFS_CFG_LOG_PAGE_SZ'
   const spiffs_page_ix pages_per_scan = SPIFFS_CFG_LOG_PAGE_SZ(fs) * 8 / bits;

My file system is 124 MiB in size, so I chose uint16_t as the type used to store spiffs_page_ix, because a uint16_t can safely store page indices for the roughly 4,000 pages in the file system. However in the above calculation the value computed for pages_per_scan is 65536 which is an overflow of uint16_t.

I changed the type of pages_per_scan to u32_t, which isn't the best solution but should work for the vast majority of configurations!

pellepl commented 4 years ago

Cheers!