From 8c7b6b26e625ce803cd1ad86a19f1710cbe9bc7f Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Tue, 12 Aug 2025 14:56:29 +0200 Subject: [PATCH 1/7] fix: false uninitialized read warning Add asserts on file system reads to make sure no positive values are returned, which would make assumptions on error checks invalid. This fixes clang tidy warnings on uninitialized reads in uses of lfs_dir_get where only negative returns are considered errors. --- lfs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lfs.c b/lfs.c index 624f43cc..4086f00e 100644 --- a/lfs.c +++ b/lfs.c @@ -739,6 +739,7 @@ static lfs_stag_t lfs_dir_getslice(lfs_t *lfs, const lfs_mdir_t *dir, int err = lfs_bd_read(lfs, NULL, &lfs->rcache, sizeof(ntag), dir->pair[0], off, &ntag, sizeof(ntag)); + LFS_ASSERT(err <= 0); if (err) { return err; } @@ -767,6 +768,7 @@ static lfs_stag_t lfs_dir_getslice(lfs_t *lfs, const lfs_mdir_t *dir, err = lfs_bd_read(lfs, NULL, &lfs->rcache, diff, dir->pair[0], off+sizeof(tag)+goff, gbuffer, diff); + LFS_ASSERT(err <= 0); if (err) { return err; } From 11cecd079c26c4a220f250440b310814e6c9fe27 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Tue, 12 Aug 2025 16:29:31 +0200 Subject: [PATCH 2/7] fix: also assert inside lfs_bd_read --- lfs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lfs.c b/lfs.c index 4086f00e..967e7c42 100644 --- a/lfs.c +++ b/lfs.c @@ -93,6 +93,7 @@ static int lfs_bd_read(lfs_t *lfs, // bypass cache? diff = lfs_aligndown(diff, lfs->cfg->read_size); int err = lfs->cfg->read(lfs->cfg, block, off, data, diff); + LFS_ASSERT(err <= 0); if (err) { return err; } From 8b75de74c916ed516c634388f81c1bbcac446d41 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Tue, 12 Aug 2025 17:03:40 +0200 Subject: [PATCH 3/7] fix: add missing return causing uninitialized reads If lfs_bd_read fails, lfs_fcrc_fromle32 will read uninitialized memory, and hasfcrc will be set to true. This may end up in a "working" state later due to crcs not matching. but it's hard to follow if that woud be the case. --- lfs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lfs.c b/lfs.c index 624f43cc..49aa1215 100644 --- a/lfs.c +++ b/lfs.c @@ -1279,6 +1279,7 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, if (err == LFS_ERR_CORRUPT) { break; } + return err; } lfs_fcrc_fromle32(&fcrc); From 172a186fa91dbb817cdda3829120e7217d711510 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 2 Sep 2025 19:36:10 -0400 Subject: [PATCH 4/7] compact when dir count hits 0x3ff --- lfs.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lfs.c b/lfs.c index 624f43cc..ebfd695a 100644 --- a/lfs.c +++ b/lfs.c @@ -2333,6 +2333,10 @@ static int lfs_dir_relocatingcommit(lfs_t *lfs, lfs_mdir_t *dir, lfs->gdisk = lfs->gstate; lfs->gdelta = (lfs_gstate_t){0}; + if(dir->count == 0x3ff) + { + goto compact; + } goto fixmlist; } From f5b2226a804b8cfdab15e4cee29601c61bbf6d69 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Mon, 15 Sep 2025 17:32:05 -0700 Subject: [PATCH 5/7] Add littlefs-toy to the related projects section. --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ae6aa88..95db2a0e 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,11 @@ License Identifiers that are here available: http://spdx.org/licenses/ to create images of the filesystem on your PC. Check if littlefs will fit your needs, create images for a later download to the target memory or inspect the content of a binary image of the target memory. - + +- [littlefs-toy] - A command-line tool for creating and working with littlefs + images. Uses syntax similar to tar command for ease of use. Supports working + on littlefs images embedded inside another file (firmware image, etc). + - [littlefs2-rust] - A Rust wrapper for littlefs. This project allows you to use littlefs in a Rust-friendly API, reaping the benefits of Rust's memory safety and other guarantees. @@ -321,6 +325,7 @@ License Identifiers that are here available: http://spdx.org/licenses/ [littlefs-js]: https://github.com/geky/littlefs-js [littlefs-js-demo]:http://littlefs.geky.net/demo.html [littlefs-python]: https://pypi.org/project/littlefs-python/ +[littlefs-toy]: https://github.com/tjko/littlefs-toy [littlefs2-rust]: https://crates.io/crates/littlefs2 [nim-littlefs]: https://github.com/Graveflo/nim-littlefs [chamelon]: https://github.com/yomimono/chamelon From f24ff9fb2568d769d3d679c84d6ea19e269867f8 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 25 Sep 2025 13:03:42 -0500 Subject: [PATCH 6/7] Moved dir->count check before commit, limited to < 0xff This matches the logic originally implemented in 48bd2bf, which was lost during the big no-recursion refactor 84da4c0. Other notes: - Checking >= 0xff matches the split logic during compaction (line 2158): end - split < 0xff - Grouping dir->erased || dir->count >= 0xff together makes it clear these share a common code path. - Checking for dir->count >= 0xff early avoids committing >8-bit ids to disk. The cat may already be out-of-the bag on this one, but opening the id space up to the full 10-bits should probably be on a non-patch release. Found by dschendt --- lfs.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lfs.c b/lfs.c index ebfd695a..401c60a2 100644 --- a/lfs.c +++ b/lfs.c @@ -2264,7 +2264,7 @@ static int lfs_dir_relocatingcommit(lfs_t *lfs, lfs_mdir_t *dir, } } - if (dir->erased) { + if (dir->erased || dir->count >= 0xff) { // try to commit struct lfs_commit commit = { .block = dir->pair[0], @@ -2333,10 +2333,6 @@ static int lfs_dir_relocatingcommit(lfs_t *lfs, lfs_mdir_t *dir, lfs->gdisk = lfs->gstate; lfs->gdelta = (lfs_gstate_t){0}; - if(dir->count == 0x3ff) - { - goto compact; - } goto fixmlist; } From 4cd2bfc2c12b981f891d4fa335ce7492cc9141f4 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 25 Sep 2025 15:22:19 -0500 Subject: [PATCH 7/7] Fixed inverted dir->count check logic Curiously, the logic from 48bd2bf was incorrect, and would allow a commit to be tried if erased _or_ dir->count was at risk of overflow. That is clearly wrong, we should only try to commit if both conditions are met... Found again by dschendt --- lfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index 401c60a2..aecace6c 100644 --- a/lfs.c +++ b/lfs.c @@ -2264,7 +2264,7 @@ static int lfs_dir_relocatingcommit(lfs_t *lfs, lfs_mdir_t *dir, } } - if (dir->erased || dir->count >= 0xff) { + if (dir->erased && dir->count < 0xff) { // try to commit struct lfs_commit commit = { .block = dir->pair[0],