-
Notifications
You must be signed in to change notification settings - Fork 937
fix: false uninitialized read warning #1133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
Tests passed ✓, Code: 17112 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
|
|
Just looking at the changed code in this PR, there are the following likely scenarios:
Given there's a similar issue I reported a while back in #904, with potentially uninitialized memory, and the overall design in the core LittleFS codebase not ensuring properly initialized memory (at least as far as I had a look at for LittleFS v2), there may be further similar issues hidden. I this particular case I'm strongly leaning towards option 1 (buggy If that's the case, the PR as presented is insufficient and only fixes symptoms, not bugs. |
Ie it's checked here: Line 118 in 8e251dd
but not here: Line 96 in 8e251dd
So I would have hoped clang-tidy would have been happy with fixing that extra assert inside lfs_bd_read, but it was not oddly. Seems it could not follow that assertion. One better option could have been to change all cases to if (err < 0) to make sure no positive values ever slip out since they brake assumptions and asserts can be turned off. |
|
Wouldn't you need to put an assert in line 96 (i.e. after the This looks to me like a case of inconsistent and leaky API enforcement … |
Yea probably should add that to this patch. But annoyingly was not enough. |
Tests passed ✓, Code: 17112 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
|
That would be a bug on our end, thanks for creating a PR!
I think that makes sense if clang-tidy wants to protect against future implementations of lfs_bd_read. I've adopted a similar pattern in littlefs3 for functions that create sign-unioned errors from non-unioned errors. Frustratingly, I couldn't come up with an LFS_ASSERT definition that informs GCC about these assumptions without dragging in extra code, so there's a few unnecessary initializations to keep GCC happy. It's neat that clang-tidy can understand these asserts though.
I've toyed around with this before, but it created more issues than it solved. If a positive error is returned, the logic will fall through to the rest of the code with garbage state, which creates problems/warnings. |
It's worth noting rambd is a special case where we're treating memory as storage. We shouldn't be relying on uninitialized memory in the core code, but uninitialized storage is something littlefs needs to be prepared to handle. (Even if we erased the entire disk during format, storage after powerloss could be considered uninitialized.) If you need reproducibility you should probably use emubd (coincidentally I'm working on a lighter-weight variant of emubd for benchmarking that looks like rambd + erase emulation + read/prog/erase counters). |
|
@elupus, sorry about the delay on these, I should have brought them in earlier before becoming distracted out-of-project. Bringing them in now. Thanks again for the PRs! |
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.