Skip to content

Commit d1c7c04

Browse files
Don't crash when includeing LittleFS.h w/no FS (#8173)
* Don't crash when includeing LittleFS.h w/no FS The LittleFS constructor could cause a divide-by-zero error and crash the chip during pre-main startup (i.e. when the constructors were called). Avoid by only initializing the LittleFS control structure when there is a filesystem specified in the flash layout. * Be even more paranoid on begin() and format()
1 parent 20de825 commit d1c7c04

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

libraries/LittleFS/src/LittleFS.h

+22-20
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,26 @@ class LittleFSImpl : public FSImpl
5959
_mounted(false) {
6060
memset(&_lfs, 0, sizeof(_lfs));
6161
memset(&_lfs_cfg, 0, sizeof(_lfs_cfg));
62-
_lfs_cfg.context = (void*) this;
63-
_lfs_cfg.read = lfs_flash_read;
64-
_lfs_cfg.prog = lfs_flash_prog;
65-
_lfs_cfg.erase = lfs_flash_erase;
66-
_lfs_cfg.sync = lfs_flash_sync;
67-
_lfs_cfg.read_size = 64;
68-
_lfs_cfg.prog_size = 64;
69-
_lfs_cfg.block_size = _blockSize;
70-
_lfs_cfg.block_count =_blockSize? _size / _blockSize: 0;
71-
_lfs_cfg.block_cycles = 16; // TODO - need better explanation
72-
_lfs_cfg.cache_size = 64;
73-
_lfs_cfg.lookahead_size = 64;
74-
_lfs_cfg.read_buffer = nullptr;
75-
_lfs_cfg.prog_buffer = nullptr;
76-
_lfs_cfg.lookahead_buffer = nullptr;
77-
_lfs_cfg.name_max = 0;
78-
_lfs_cfg.file_max = 0;
79-
_lfs_cfg.attr_max = 0;
62+
if (_size && _blockSize) {
63+
_lfs_cfg.context = (void*) this;
64+
_lfs_cfg.read = lfs_flash_read;
65+
_lfs_cfg.prog = lfs_flash_prog;
66+
_lfs_cfg.erase = lfs_flash_erase;
67+
_lfs_cfg.sync = lfs_flash_sync;
68+
_lfs_cfg.read_size = 64;
69+
_lfs_cfg.prog_size = 64;
70+
_lfs_cfg.block_size = _blockSize;
71+
_lfs_cfg.block_count = _size / _blockSize;
72+
_lfs_cfg.block_cycles = 16; // TODO - need better explanation
73+
_lfs_cfg.cache_size = 64;
74+
_lfs_cfg.lookahead_size = 64;
75+
_lfs_cfg.read_buffer = nullptr;
76+
_lfs_cfg.prog_buffer = nullptr;
77+
_lfs_cfg.lookahead_buffer = nullptr;
78+
_lfs_cfg.name_max = 0;
79+
_lfs_cfg.file_max = 0;
80+
_lfs_cfg.attr_max = 0;
81+
}
8082
}
8183

8284
~LittleFSImpl() {
@@ -181,7 +183,7 @@ class LittleFSImpl : public FSImpl
181183
}
182184

183185
bool begin() override {
184-
if (_size <= 0) {
186+
if ((_blockSize <= 0) || (_size <= 0)) {
185187
DEBUGV("LittleFS size is <= zero");
186188
return false;
187189
}
@@ -203,7 +205,7 @@ class LittleFSImpl : public FSImpl
203205
}
204206

205207
bool format() override {
206-
if (_size == 0) {
208+
if ((_blockSize <= 0) || (_size <= 0)) {
207209
DEBUGV("lfs size is zero\n");
208210
return false;
209211
}

0 commit comments

Comments
 (0)