Skip to content

Commit c8a111f

Browse files
committed
Fix FS size and add type size checks
1 parent 799f680 commit c8a111f

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

hardware/esp8266com/esp8266/cores/esp8266/spiffs_api.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
*/
2424

2525
#include "FS.h"
26+
#undef max
27+
#undef min
2628
#include "FSImpl.h"
2729
#include "spiffs/spiffs.h"
2830
#include "debug.h"
31+
#include <limits>
2932

3033
extern "C" {
3134
#include "c_types.h"
@@ -119,6 +122,27 @@ class SPIFFSImpl : public FSImpl {
119122
config.log_block_size = _blockSize;
120123
config.log_page_size = _pageSize;
121124

125+
126+
if (((uint32_t) std::numeric_limits<spiffs_block_ix>::max()) < (_size / _blockSize)) {
127+
DEBUGV("spiffs_block_ix type too small");
128+
abort();
129+
}
130+
131+
if (((uint32_t) std::numeric_limits<spiffs_page_ix>::max()) < (_size / _pageSize)) {
132+
DEBUGV("spiffs_page_ix type too small");
133+
abort();
134+
}
135+
136+
if (((uint32_t) std::numeric_limits<spiffs_obj_id>::max()) < (2 + (_size / (2*_pageSize))*2)) {
137+
DEBUGV("spiffs_obj_id type too small");
138+
abort();
139+
}
140+
141+
if (((uint32_t) std::numeric_limits<spiffs_span_ix>::max()) < (_size / _pageSize - 1)) {
142+
DEBUGV("spiffs_span_ix type too small");
143+
abort();
144+
}
145+
122146
// hack: even though fs is not initialized at this point,
123147
// SPIFFS_buffer_bytes_for_cache uses only fs->config.log_page_size
124148
// suggestion: change SPIFFS_buffer_bytes_for_cache to take
@@ -381,8 +405,8 @@ extern "C" uint32_t _SPIFFS_page;
381405
extern "C" uint32_t _SPIFFS_block;
382406

383407
static SPIFFSImpl s_defaultFs(
384-
(uint32_t) &_SPIFFS_start - 0x40200000,
385-
(uint32_t) (&_SPIFFS_end - &_SPIFFS_start),
408+
(uint32_t) (&_SPIFFS_start) - 0x40200000,
409+
(uint32_t) (&_SPIFFS_end) - (uint32_t) (&_SPIFFS_start),
386410
(uint32_t) &_SPIFFS_page,
387411
(uint32_t) &_SPIFFS_block,
388412
5);

0 commit comments

Comments
 (0)