This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 89cb8ee8c629d7a8f7428f434a2e99cdff850b4c Author: yukangzhi <[email protected]> AuthorDate: Fri Aug 7 17:10:08 2026 +0800 nshlib/ps: replace asprintf with a stack buffer in ps_readprocfs(). Replace asprintf() + free() in ps_readprocfs() with a PATH_MAX stack buffer and snprintf(). This eliminates 4~5 malloc/free pairs per process during ps command execution. The procfs path format is bounded (/proc/<pid>/<node>), so PATH_MAX is always sufficient. The snprintf() return value is still checked, and -ENAMETOOLONG is returned on truncation. Assisted-by: Kiro Signed-off-by: yukangzhi <[email protected]> --- nshlib/nsh_proccmds.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/nshlib/nsh_proccmds.c b/nshlib/nsh_proccmds.c index b204600af..8418a620d 100644 --- a/nshlib/nsh_proccmds.c +++ b/nshlib/nsh_proccmds.c @@ -331,26 +331,23 @@ ssize_t ps_readprocfs(FAR struct nsh_vtbl_s *vtbl, FAR const char *basepath, FAR const struct dirent *entryp, FAR struct nsh_taskstatus_s *status) { - FAR char *filepath = NULL; + char filepath[PATH_MAX]; int ret; - ret = asprintf(&filepath, "%s/%s/%s", dirpath, entryp->d_name, basepath); - if (ret < 0 || filepath == NULL) + ret = snprintf(filepath, sizeof(filepath), "%s/%s/%s", + dirpath, entryp->d_name, basepath); + if (ret < 0 || ret >= sizeof(filepath)) { - nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO); - status->td_buf[status->td_bufpos] = '\0'; + nsh_error(vtbl, g_fmtcmdfailed, "ps", "snprintf", NSH_ERRNO); + return -ENAMETOOLONG; } - else - { - ret = nsh_readfile(vtbl, "ps", filepath, - status->td_buf + status->td_bufpos, - status->td_bufsize - status->td_bufpos); - if (ret >= 0) - { - ret = strlen(status->td_buf + status->td_bufpos) + 1; - } - free(filepath); + ret = nsh_readfile(vtbl, "ps", filepath, + status->td_buf + status->td_bufpos, + status->td_bufsize - status->td_bufpos); + if (ret >= 0) + { + ret = strlen(status->td_buf + status->td_bufpos) + 1; } return ret;
