Skip to content

nshlib/nsh_netcmds: check nsh_getfullpath return value for NULL - #4295

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/nsh-netcmds-null-fullpath
Dec 5, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/nsh-netcmds-null-fullpath

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

In nshlib/nsh_netcmds.c, cmd_get(), cmd_put(), and cmd_wget() call nsh_getfullpath() to resolve the local file path, but none of the three checked for a NULL return on allocation failure before passing fullpath to tftpget(), tftpput(), or open().

This patch checks if fullpath is NULL in all three commands, reports g_fmtcmdoutofmemory, frees any already-allocated path buffers (args.destpath in cmd_get()/cmd_put() and allocfile in cmd_wget()), and returns ERROR.

Function Before After
cmd_get() Passes fullpath == NULL to tftpget() and leaks args.destpath if allocated Reports g_fmtcmdoutofmemory, frees args.destpath if allocated, returns ERROR
cmd_put() Passes fullpath == NULL to tftpput() and leaks args.destpath if allocated Reports g_fmtcmdoutofmemory, frees args.destpath if allocated, returns ERROR
cmd_wget() Passes fullpath == NULL to open() Reports g_fmtcmdoutofmemory via errout, frees allocfile, returns ERROR

Impact

Prevents NULL pointer dereferences and path-buffer leaks in NSH network file-transfer commands when nsh_getfullpath() fails to allocate memory. No Kconfig, API, or host-tool changes.

Testing

  • Style verified with nxstyle on nshlib/nsh_netcmds.c (exit code 0) and checkpatch.sh -m -f nshlib/nsh_netcmds.c (All checks pass).
  • Built sim:nsh with CONFIG_NET=y, CONFIG_NET_TCP=y, CONFIG_NET_UDP=y, CONFIG_NET_IPv4=y, CONFIG_NETUTILS_TFTPC=y, and CONFIG_NETUTILS_WEBCLIENT=y, and verified cmd_get, cmd_put, and cmd_wget are linked into nuttx:
LD:  nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz
-rwxr-xr-x 1 swatantra swatantra 6.0M Dec  4 12:42 nuttx
00000000400d4ad1 T cmd_get
00000000400d64a2 T cmd_put
00000000400d66da T cmd_wget

Not tested at runtime with simulated OOM in nsh_getfullpath().

In cmd_get(), cmd_put(), and cmd_wget(), check if nsh_getfullpath() returns NULL on allocation failure, report g_fmtcmdoutofmemory, free any allocated path strings, and return ERROR instead of passing NULL to tftpget(), tftpput(), or open().

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <maverickswatantra@gmail.com>
@Swatantra-66
Swatantra-66 force-pushed the fix/nsh-netcmds-null-fullpath branch from 0f0b7cf to 9651b31 Compare December 4, 2026 16:23
@xiaoxiang781216
xiaoxiang781216 merged commit 58340e5 into apache:master Dec 5, 2026
23 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants