-
Couldn't load subscription status.
- Fork 932
Add null pointer check for lfs_dir_fetchmatch #1159
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
base: master
Are you sure you want to change the base?
Conversation
Tests passed ✓, Code: 17132 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
|
|
Rationale as explained in #1158 (comment) … |
|
Hi @liconghui628, thanks for creating an issue + PR.
Hmmm. It's true Earlier in this loop we check if the tag is "valid" (if the sign bit is clear), and Lines 1182 to 1186 in adad0fb
Lines 351 to 353 in adad0fb
Nothing else touches Short of in-RAM corruption or some other bug. |
@geky This question is indeed very strange. I tried to add some printing information, and in fact, it did call up here. |
|
Ah! @liconghui628, this looks like an older version of the codebase. In particular it's missing this patch: #337, which was merged in v2.1.4. I believe this is the same issue. |
|
Would it make sense to include the NULL pointer check anyway; and be it as an assertion at the call site of |
I'm not opposed to it, but what would it really add over hardfaulting? Both point out this There's a lot of things that can be NULL in C, fortunately even microcontrollers these days make NULL dereferences "safe" and easy to find. The log @liconghui628 shared even had a full backtrace + |
|
@geky @BenBE Thank you for your patient explanation. From the perspective of internal implementation alone, I think NULL detection is necessary because it is uncertain whether external modifications will be executed here in the future, the impact on business is quite significant. We just need to make the interface implementation more robust, without worrying about how it is called externally. |
IMO, it's valuable to make intent/expectations explicit between |
The internal interface of littlfs calls lfs_dir_fetchmatch with a null cb parameter, and lfs_dir_fetchmatch does not internally determine cb, which may directly call and cause the program to crash.
static int lfs_dir_fetch(lfs_t *lfs,
lfs_mdir_t *dir, const lfs_block_t pair[2]) {
// note, mask=-1, tag=-1 can never match a tag since this
// pattern has the invalid bit set
return (int)lfs_dir_fetchmatch(lfs, dir, pair,
(lfs_tag_t)-1, (lfs_tag_t)-1, NULL, NULL, NULL);
}