Skip to content

Fix CTZ traverse pointer conversion count - #1217

Open
Old-Ding wants to merge 1 commit into
littlefs-project:masterfrom
Old-Ding:codex/ctz-traverse-convert-count
Open

Fix CTZ traverse pointer conversion count#1217
Old-Ding wants to merge 1 commit into
littlefs-project:masterfrom
Old-Ding:codex/ctz-traverse-convert-count

Conversation

@Old-Ding

@Old-Ding Old-Ding commented Jul 5, 2026

Copy link
Copy Markdown

Summary

  • convert only the CTZ head pointers that lfs_bd_read actually filled
  • move the conversion after the read error check, so failed reads do not decode local buffer contents
  • avoid reading an uninitialized heads[1] slot when count == 1

Fixes #1214

Testing

  • git diff --check
  • git ls-files --eol lfs.c
  • Not run: make test requires the project test toolchain, but this Windows host does not have make, cc, or gcc available.

Convert only the CTZ head pointers that were read successfully. This avoids reading an uninitialized heads[1] slot when the CTZ index only requires one pointer.

Generated-by: OpenAI Codex
Signed-off-by: aineoae86-sys <ai.neo.ae86@gmail.com>
@geky-bot

geky-bot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator
Tests passed ✓, Code: 17200 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
Code Stack Structs Coverage
Default 17200 B (+0.0%) 1448 B (+0.0%) 812 B (+0.0%) Lines 2448/2609 lines (-0.0%)
Readonly 6234 B (+0.0%) 448 B (+0.0%) 812 B (+0.0%) Branches 1301/1638 branches (-0.0%)
Threadsafe 18056 B (+0.0%) 1448 B (+0.0%) 820 B (+0.0%) Benchmarks
Multiversion 17272 B (+0.0%) 1448 B (+0.0%) 816 B (+0.0%) Readed 29000746676 B (+0.0%)
Migrate 18864 B (+0.0%) 1752 B (+0.0%) 816 B (+0.0%) Proged 1482895246 B (+0.0%)
Error-asserts 18036 B (+0.0%) 1440 B (+0.0%) 812 B (+0.0%) Erased 1568921600 B (+0.0%)

@Kishore-Gorijavolu Kishore-Gorijavolu left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the CTZ traversal change and independently tested it against both the pre-fix and PR revisions.

The change looks correct to me. In the existing implementation, count can be either 1 or 2, but both heads[0] and heads[1] were unconditionally passed through lfs_fromle32(). When count == 1, only one entry is populated by lfs_bd_read(), so converting heads[1] unnecessarily accesses an uninitialized element.

This patch addresses that cleanly by:

  • checking the lfs_bd_read() result before consuming the returned data;
  • converting only count entries using the bounded loop; and
  • preserving the subsequent heads[count-1] traversal behavior.

I also performed a focused before/after test using the same test source against both revisions. The test created a 4096-byte multi-block file and exercised lfs_fs_traverse(). Both revisions completed successfully with the same 19-block traversal sequence, indicating no functional regression in the exercised CTZ traversal path.

I attempted to run the complete test suite locally as an additional regression check, but the local Windows/MSYS Python environment could not execute the littlefs test infrastructure because the POSIX termios module was unavailable. I therefore did not treat that attempt as a project test result. The PR's GitHub CI checks are passing.

The implementation is appropriately scoped to the pointer-conversion and read-error handling path.

Approved.

PR-1217.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strange code in lfs_ctz_traverse triggering clang-analyzer-security.ArrayBound

4 participants