Skip to content

Commit 5330966

Browse files
xiaoxiang781216GUIDINGLI
authored andcommitted
nshlib: Replace the big temp buffer with lib_get_[path|temp]buffer
to save the stack consumption Signed-off-by: Xiang Xiao <[email protected]>
1 parent 131d50a commit 5330966

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

nshlib/nsh_fsutils.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl,
7373
FAR struct dirent *entryp, FAR void *pvarg)
7474
{
7575
FAR struct getpid_arg_s *arg = (FAR struct getpid_arg_s *)pvarg;
76-
char buffer[PATH_MAX];
76+
FAR char *buffer;
7777
int fd;
7878
int len;
7979

@@ -82,20 +82,27 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl,
8282
return -E2BIG;
8383
}
8484

85-
/* Match the name of the process */
85+
buffer = lib_get_pathbuffer();
86+
if (buffer == NULL)
87+
{
88+
return -errno;
89+
}
8690

87-
snprintf(buffer, sizeof(buffer), "%s/%s/cmdline", dirpath, entryp->d_name);
91+
/* Match the name of the process */
8892

93+
snprintf(buffer, PATH_MAX, "%s/%s/cmdline", dirpath, entryp->d_name);
8994
fd = open(buffer, O_RDONLY | O_CLOEXEC);
9095
if (fd < 0)
9196
{
97+
lib_put_pathbuffer(buffer);
9298
return 0;
9399
}
94100

95101
len = read(fd, buffer, sizeof(buffer) - 1);
96102
close(fd);
97103
if (len < 0)
98104
{
105+
lib_put_pathbuffer(buffer);
99106
return -errno;
100107
}
101108

@@ -107,6 +114,7 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl,
107114
arg->pids[arg->next++] = atoi(entryp->d_name);
108115
}
109116

117+
lib_put_pathbuffer(buffer);
110118
return OK;
111119
}
112120
#endif

nshlib/nsh_timcmds.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <nuttx/config.h>
2828

29+
#include <stdio.h>
2930
#include <stdlib.h>
3031
#include <string.h>
3132
#include <strings.h>
@@ -552,7 +553,6 @@ int cmd_timedatectl(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
552553
#ifndef CONFIG_NSH_DISABLE_WATCH
553554
int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
554555
{
555-
char buffer[LINE_MAX];
556556
int interval = 2;
557557
int count = -1;
558558
FAR char *cmd;
@@ -595,8 +595,15 @@ int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
595595

596596
for (i = 0; i < count; i++)
597597
{
598+
FAR char *buffer = lib_get_tempbuffer(LINE_MAX);
599+
if (buffer == NULL)
600+
{
601+
return ERROR;
602+
}
603+
598604
strlcpy(buffer, cmd, LINE_MAX);
599605
ret = nsh_parse(vtbl, buffer);
606+
lib_put_tempbuffer(buffer);
600607
if (ret < 0)
601608
{
602609
nsh_error(vtbl, g_fmtcmdfailed, argv[0], cmd, NSH_ERRNO);

0 commit comments

Comments
 (0)