Skip to content

Commit 50fbe0f

Browse files
committed
Fixed handful of block_size-related bugs found by CI
- Valgrind-detected memory leak in test_superblocks. - Valgrind-detected uninitialized lfs->block_size in lfs_init caused by an unintended sed replacement. - Said sed replacement also changed the implementation of the v1 portion of lfs_migrate, this is out of scope and caused migrate to fail, which fortunately CI noticed. - We also need to copy the block_size/block_count configs in v1 so that low-level bd operations still work. - le-conversion in test_evil now matters due to difference in LFS_ERR_CORRUPT/LFS_ERR_INVAL paths during lfs_mount. This is good because we're actually testing for loop detection not pointer out-of-bounds as intended.
1 parent c776faf commit 50fbe0f

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

lfs.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4087,7 +4087,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
40874087
lfs->attr_max = LFS_ATTR_MAX;
40884088
}
40894089

4090-
LFS_ASSERT(lfs->cfg->metadata_max <= lfs->block_size);
4090+
LFS_ASSERT(lfs->cfg->metadata_max <= lfs->cfg->block_size);
40914091

40924092
// setup default state
40934093
lfs->root[0] = LFS_BLOCK_NULL;
@@ -4139,8 +4139,8 @@ static int lfs_rawformat(lfs_t *lfs, const struct lfs_config *cfg) {
41394139
if (!lfs->block_size) {
41404140
lfs->block_size = lfs->erase_size;
41414141
}
4142+
LFS_ASSERT(lfs->cfg->block_count != 0);
41424143
lfs->block_count = lfs->cfg->block_count;
4143-
LFS_ASSERT(lfs->block_count != 0);
41444144

41454145
// check that the block size is large enough to fit ctz pointers
41464146
LFS_ASSERT(4*lfs_npw2(0xffffffff / (lfs->block_size-2*4))
@@ -4846,6 +4846,7 @@ static int lfs_fs_rawstat(lfs_t *lfs, struct lfs_fsinfo *fsinfo) {
48464846
return 0;
48474847
}
48484848

4849+
#ifndef LFS_READONLY
48494850
int lfs_fs_rawgrow(lfs_t *lfs, lfs_size_t block_count) {
48504851
// shrinking is not supported
48514852
LFS_ASSERT(block_count >= lfs->block_count);
@@ -4882,6 +4883,7 @@ int lfs_fs_rawgrow(lfs_t *lfs, lfs_size_t block_count) {
48824883

48834884
return 0;
48844885
}
4886+
#endif
48854887

48864888
#ifdef LFS_MIGRATE
48874889
////// Migration from littelfs v1 below this //////
@@ -5057,7 +5059,7 @@ static int lfs1_dir_fetch(lfs_t *lfs,
50575059
}
50585060

50595061
if ((0x7fffffff & test.size) < sizeof(test)+4 ||
5060-
(0x7fffffff & test.size) > lfs->block_size) {
5062+
(0x7fffffff & test.size) > lfs->cfg->block_size) {
50615063
continue;
50625064
}
50635065

@@ -5245,6 +5247,13 @@ static int lfs1_mount(lfs_t *lfs, struct lfs1 *lfs1,
52455247
return err;
52465248
}
52475249

5250+
// setup block_size, v1 did not support block_size searching, so
5251+
// block_size and block_count are required
5252+
LFS_ASSERT(lfs->cfg->block_size != 0);
5253+
LFS_ASSERT(lfs->cfg->block_count != 0);
5254+
lfs->block_size = lfs->cfg->block_size;
5255+
lfs->block_count = lfs->cfg->block_count;
5256+
52485257
lfs->lfs1 = lfs1;
52495258
lfs->lfs1->root[0] = LFS_BLOCK_NULL;
52505259
lfs->lfs1->root[1] = LFS_BLOCK_NULL;
@@ -5491,8 +5500,8 @@ static int lfs_rawmigrate(lfs_t *lfs, const struct lfs_config *cfg) {
54915500

54925501
lfs_superblock_t superblock = {
54935502
.version = LFS_DISK_VERSION,
5494-
.block_size = lfs->block_size,
5495-
.block_count = lfs->block_count,
5503+
.block_size = lfs->cfg->block_size,
5504+
.block_count = lfs->cfg->block_count,
54965505
.name_max = lfs->name_max,
54975506
.file_max = lfs->file_max,
54985507
.attr_max = lfs->attr_max,

tests/test_evil.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ code = '''
237237
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
238238
lfs_dir_commit(&lfs, &mdir, LFS_MKATTRS(
239239
{LFS_MKTAG(LFS_TYPE_HARDTAIL, 0x3ff, 8),
240-
(lfs_block_t[2]){0, 1}})) => 0;
240+
(lfs_block_t[2]){lfs_tole32(0), lfs_tole32(1)}})) => 0;
241241
lfs_deinit(&lfs) => 0;
242242
243243
// test that mount fails gracefully
@@ -268,7 +268,7 @@ code = '''
268268
lfs_dir_fetch(&lfs, &mdir, pair) => 0;
269269
lfs_dir_commit(&lfs, &mdir, LFS_MKATTRS(
270270
{LFS_MKTAG(LFS_TYPE_HARDTAIL, 0x3ff, 8),
271-
(lfs_block_t[2]){0, 1}})) => 0;
271+
(lfs_block_t[2]){lfs_tole32(0), lfs_tole32(1)}})) => 0;
272272
lfs_deinit(&lfs) => 0;
273273
274274
// test that mount fails gracefully
@@ -297,6 +297,7 @@ code = '''
297297
lfs_pair_fromle32(pair);
298298
// change tail-pointer to point to ourself
299299
lfs_dir_fetch(&lfs, &mdir, pair) => 0;
300+
lfs_pair_tole32(pair);
300301
lfs_dir_commit(&lfs, &mdir, LFS_MKATTRS(
301302
{LFS_MKTAG(LFS_TYPE_HARDTAIL, 0x3ff, 8), pair})) => 0;
302303
lfs_deinit(&lfs) => 0;

tests/test_superblocks.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ code = '''
330330
{LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
331331
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
332332
&superblock})) => 0;
333+
lfs_deinit(&lfs) => 0;
333334
334335
// known block_size/block_count
335336
cfg->block_size = BLOCK_SIZE;
@@ -461,6 +462,7 @@ code = '''
461462
{LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
462463
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
463464
&superblock})) => 0;
465+
lfs_deinit(&lfs) => 0;
464466
465467
// known block_size/block_count
466468
cfg->block_size = BLOCK_SIZE;
@@ -521,6 +523,7 @@ code = '''
521523
{LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
522524
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
523525
&superblock})) => 0;
526+
lfs_deinit(&lfs) => 0;
524527
525528
// known block_size/block_count
526529
cfg->block_size = BLOCK_SIZE;

0 commit comments

Comments
 (0)