nshlib/nsh_fscmds: Fix potential NULL pointer dereferences#3575
Merged
Conversation
Fix two potential NULL pointer dereferences in nsh_fscmds.c: 1. fdinfo_callback: asprintf() failure left filepath potentially NULL, which was then passed to nsh_catfile(). Add early return on asprintf failure. 2. cmd_cat: malloc(BUFSIZ) for stdin reading was used without checking the return value. Add NULL check with -ENOMEM return. Also fix a typo in error message: 'nsh_catfaile' -> 'nsh_catfile'. Signed-off-by: hanzhijian <hanzhijian@zepp.com>
xiaoxiang781216
approved these changes
Jun 30, 2026
acassis
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix two potential NULL pointer dereferences in
nshlib/nsh_fscmds.c.Changes
1.
fdinfo_callback— NULL deref after asprintf failureWhen
asprintf()fails (returns < 0),filepathmay be NULL. The code logged an error but did not return — it fell through and passed the NULLfilepathtonsh_catfile(), causing a NULL pointer dereference.Fix: Add
return ret;after the asprintf error path.Also fixed a typo in the error message:
"nsh_catfaile"→"nsh_catfile".2.
cmd_cat— NULL deref after malloc failureWhen
argc == 1(reading from stdin),malloc(BUFSIZ)was called but the return value was never checked. If malloc fails,bufis NULL andnsh_read(vtbl, buf, BUFSIZ)would dereference it.Fix: Add
if (buf == NULL) return -ENOMEM;after the malloc call.Verification
Built and tested on
sim:nshconfiguration:Signed-off-by: hanzhijian hanzhijian@zepp.com