This is an automated email from the ASF dual-hosted git repository.

ligd pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 533096676239c729aee7b6f788bde16b8902a495
Author: Xiang Xiao <xiaoxi...@xiaomi.com>
AuthorDate: Sun Jan 19 10:00:11 2025 +0800

    nshlib: Replace the big temp buffer with lib_get_[path|temp]buffer
    
    to save the stack consumption
    
    Signed-off-by: Xiang Xiao <xiaoxi...@xiaomi.com>
---
 nshlib/nsh_fsutils.c | 14 +++++++++++---
 nshlib/nsh_timcmds.c |  9 ++++++++-
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/nshlib/nsh_fsutils.c b/nshlib/nsh_fsutils.c
index 99e948784..c3793b206 100644
--- a/nshlib/nsh_fsutils.c
+++ b/nshlib/nsh_fsutils.c
@@ -73,7 +73,7 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl,
                            FAR struct dirent *entryp, FAR void *pvarg)
 {
   FAR struct getpid_arg_s *arg = (FAR struct getpid_arg_s *)pvarg;
-  char buffer[PATH_MAX];
+  FAR char *buffer;
   int fd;
   int len;
 
@@ -82,13 +82,19 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl,
       return -E2BIG;
     }
 
-  /* Match the name of the process */
+  buffer = lib_get_pathbuffer();
+  if (buffer == NULL)
+    {
+      return -errno;
+    }
 
-  snprintf(buffer, sizeof(buffer), "%s/%s/cmdline", dirpath, entryp->d_name);
+  /* Match the name of the process */
 
+  snprintf(buffer, PATH_MAX, "%s/%s/cmdline", dirpath, entryp->d_name);
   fd = open(buffer, O_RDONLY | O_CLOEXEC);
   if (fd < 0)
     {
+      lib_put_pathbuffer(buffer);
       return 0;
     }
 
@@ -96,6 +102,7 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl,
   close(fd);
   if (len < 0)
     {
+      lib_put_pathbuffer(buffer);
       return -errno;
     }
 
@@ -107,6 +114,7 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl,
         arg->pids[arg->next++] = atoi(entryp->d_name);
     }
 
+  lib_put_pathbuffer(buffer);
   return OK;
 }
 #endif
diff --git a/nshlib/nsh_timcmds.c b/nshlib/nsh_timcmds.c
index 732bb512e..a0952acc1 100644
--- a/nshlib/nsh_timcmds.c
+++ b/nshlib/nsh_timcmds.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/config.h>
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
@@ -552,7 +553,6 @@ int cmd_timedatectl(FAR struct nsh_vtbl_s *vtbl, int argc, 
FAR char **argv)
 #ifndef CONFIG_NSH_DISABLE_WATCH
 int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
 {
-  char buffer[LINE_MAX];
   int interval = 2;
   int count = -1;
   FAR char *cmd;
@@ -595,8 +595,15 @@ int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 
   for (i = 0; i < count; i++)
     {
+      FAR char *buffer = lib_get_tempbuffer(LINE_MAX);
+      if (buffer == NULL)
+        {
+          return ERROR;
+        }
+
       strlcpy(buffer, cmd, LINE_MAX);
       ret = nsh_parse(vtbl, buffer);
+      lib_put_tempbuffer(buffer);
       if (ret < 0)
         {
           nsh_error(vtbl, g_fmtcmdfailed, argv[0], cmd, NSH_ERRNO);

Reply via email to