Add null pointer check for lfs_dir_fetchmatch#1159
Add null pointer check for lfs_dir_fetchmatch#1159liconghui628 wants to merge 1 commit intolittlefs-project:masterfrom
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 |
|
I'm still not convinced an assert adds value over just dereferencing the callback, but we can add an @liconghui628, if you change this to This is currently tested in CI and won't change without a new major version. |
|
I believe this will be superseded by #1180 |
|
#1180 is landing in the next patch release, so I believe this can be closed. Feel free to reopen if I missed anything. |
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);
}