Skip to content

Commit

Permalink
Use lib_get_tempbuffer Saving stack
Browse files Browse the repository at this point in the history
Signed-off-by: zhangshoukui <[email protected]>
  • Loading branch information
Zhangshoukui committed Jan 16, 2025
1 parent 14fa7d2 commit fbaa045
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 20 deletions.
17 changes: 13 additions & 4 deletions netutils/rexec/rexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ static int do_rexec(FAR struct rexec_arg_s *arg)

int main(int argc, FAR char **argv)
{
char cmd[LINE_MAX];
struct rexec_arg_s arg;
FAR char *cmd;
int option;
int ret;
int i;

memset(&arg, 0, sizeof(arg));
Expand Down Expand Up @@ -202,13 +203,21 @@ int main(int argc, FAR char **argv)
usage(argv[0]);
}

cmd = lib_get_tempbuffer(LINE_MAX);
if (cmd == NULL)
{
return -ENOMEM;
}

cmd[0] = '\0';
for (i = optind; i < argc; i++)
{
strlcat(cmd, argv[i], sizeof(cmd));
strlcat(cmd, " ", sizeof(cmd));
strlcat(cmd, argv[i], LINE_MAX);
strlcat(cmd, " ", LINE_MAX);
}

arg.command = cmd;
return do_rexec(&arg);
ret = do_rexec(&arg);
lib_put_tempbuffer(cmd);
return ret;
}
24 changes: 19 additions & 5 deletions nshlib/nsh_mmcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <nuttx/config.h>

#include <stdio.h>
#include <string.h>

#include "nsh.h"
Expand Down Expand Up @@ -58,18 +59,29 @@ int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)

int cmd_memdump(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
char arg[LINE_MAX] = "";
FAR char *arg;
int ret;
int i;

arg = lib_get_tempbuffer(LINE_MAX);
if (arg == NULL)
{
return -ENOMEM;
}

arg[0] = '\0';

if (argc == 1)
{
strlcpy(arg, "used", LINE_MAX);
}
else if (argc >= 2 && (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "help") == 0))
{
return nsh_catfile(vtbl, argv[0],
CONFIG_NSH_PROC_MOUNTPOINT "/memdump");
ret = nsh_catfile(vtbl, argv[0],
CONFIG_NSH_PROC_MOUNTPOINT "/memdump");
lib_put_tempbuffer(arg);
return ret;
}
else
{
Expand All @@ -83,8 +95,10 @@ int cmd_memdump(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
}
}

return nsh_writefile(vtbl, argv[0], arg, strlen(arg),
CONFIG_NSH_PROC_MOUNTPOINT "/memdump");
ret = nsh_writefile(vtbl, argv[0], arg, strlen(arg),
CONFIG_NSH_PROC_MOUNTPOINT "/memdump");
lib_put_tempbuffer(arg);
return ret;
}

#endif /* !CONFIG_NSH_DISABLE_MEMDUMP && NSH_HAVE_WRITEFILE */
13 changes: 11 additions & 2 deletions system/nxcamera/nxcamera_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ static int nxcamera_cmd_help(FAR struct nxcamera_s *pcam, FAR char *parg)

int main(int argc, FAR char *argv[])
{
char buffer[LINE_MAX];
FAR char *buffer;
int len;
int x;
bool running = true;
Expand All @@ -422,6 +422,14 @@ int main(int argc, FAR char *argv[])
return -ENOMEM;
}

buffer = lib_get_tempbuffer(LINE_MAX);
if (buffer == NULL)
{
nxcamera_release(pcam);
printf("Error: lib_get_tempbuffer failed\n");
return -ENOMEM;
}

/* Loop until the user exits */

while (running)
Expand All @@ -433,7 +441,7 @@ int main(int argc, FAR char *argv[])

/* Read a line from the terminal */

len = readline_stream(buffer, sizeof(buffer),
len = readline_stream(buffer, LINE_MAX,
stdin, stdout);
if (len > 0)
{
Expand Down Expand Up @@ -495,6 +503,7 @@ int main(int argc, FAR char *argv[])
/* Release the NxCamera context */

nxcamera_release(pcam);
lib_put_tempbuffer(buffer);

return OK;
}
13 changes: 11 additions & 2 deletions system/nxlooper/nxlooper_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ static int nxlooper_cmd_help(FAR struct nxlooper_s *plooper, char *parg)

int main(int argc, FAR char *argv[])
{
char buffer[LINE_MAX];
FAR char *buffer;
int len;
int x;
int running;
Expand All @@ -523,6 +523,14 @@ int main(int argc, FAR char *argv[])
return -ENOMEM;
}

buffer = lib_get_tempbuffer(LINE_MAX);
if (buffer == NULL)
{
nxlooper_release(plooper);
printf("Error: lib_get_tempbuffer failed\n");
return -ENOMEM;
}

/* Loop until the user exits */

running = TRUE;
Expand All @@ -535,7 +543,7 @@ int main(int argc, FAR char *argv[])

/* Read a line from the terminal */

len = readline_stream(buffer, sizeof(buffer),
len = readline_stream(buffer, LINE_MAX,
stdin, stdout);
if (len > 0)
{
Expand Down Expand Up @@ -597,6 +605,7 @@ int main(int argc, FAR char *argv[])
/* Release the NxLooper context */

nxlooper_release(plooper);
lib_put_tempbuffer(buffer);

return OK;
}
13 changes: 11 additions & 2 deletions system/nxplayer/nxplayer_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ static int nxplayer_cmd_help(FAR struct nxplayer_s *pplayer, char *parg)

int main(int argc, FAR char *argv[])
{
char buffer[LINE_MAX];
FAR char *buffer;
int len;
int x;
int running;
Expand All @@ -760,6 +760,14 @@ int main(int argc, FAR char *argv[])
return -ENOMEM;
}

buffer = lib_get_tempbuffer(LINE_MAX);
if (buffer == NULL)
{
nxplayer_release(pplayer);
printf("Error: lib_get_tempbuffer failed\n");
return -ENOMEM;
}

/* Loop until the user exits */

running = TRUE;
Expand All @@ -772,7 +780,7 @@ int main(int argc, FAR char *argv[])

/* Read a line from the terminal */

len = readline_stream(buffer, sizeof(buffer),
len = readline_stream(buffer, LINE_MAX,
stdin, stdout);
if (len > 0)
{
Expand Down Expand Up @@ -841,6 +849,7 @@ int main(int argc, FAR char *argv[])
/* Release the NxPlayer context */

nxplayer_release(pplayer);
lib_put_tempbuffer(buffer);

return OK;
}
13 changes: 11 additions & 2 deletions system/nxrecorder/nxrecorder_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ static int nxrecorder_cmd_help(FAR struct nxrecorder_s *precorder,

int main(int argc, FAR char *argv[])
{
char buffer[LINE_MAX];
FAR char *buffer;
int len;
int x;
int running;
Expand All @@ -552,6 +552,14 @@ int main(int argc, FAR char *argv[])
return -ENOMEM;
}

buffer = lib_get_tempbuffer(LINE_MAX);
if (buffer == NULL)
{
nxrecorder_release(precorder);
printf("Error: lib_get_tempbuffer failed\n");
return -ENOMEM;
}

/* Loop until the user exits */

running = TRUE;
Expand All @@ -564,7 +572,7 @@ int main(int argc, FAR char *argv[])

/* Read a line from the terminal */

len = readline_stream(buffer, sizeof(buffer),
len = readline_stream(buffer, LINE_MAX,
stdin, stdout);
if (len > 0)
{
Expand Down Expand Up @@ -633,6 +641,7 @@ int main(int argc, FAR char *argv[])
/* Release the NxRecorder context */

nxrecorder_release(precorder);
lib_put_tempbuffer(buffer);

return OK;
}
13 changes: 10 additions & 3 deletions system/trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ static int trace_cmd_dump(FAR const char *name, int index, int argc,
static int trace_cmd_cmd(FAR const char *name, int index, int argc,
FAR char **argv, int notectlfd)
{
char command[LINE_MAX];
FAR char *command;
bool changed;
bool cont = false;

Expand All @@ -253,11 +253,17 @@ static int trace_cmd_cmd(FAR const char *name, int index, int argc,
return ERROR;
}

command = lib_get_tempbuffer(LINE_MAX);
if (command == NULL)
{
return -ENOMEM;
}

command[0] = '\0';
while (index < argc)
{
strlcat(command, argv[index], sizeof(command));
strlcat(command, " ", sizeof(command));
strlcat(command, argv[index], LINE_MAX);
strlcat(command, " ", LINE_MAX);
index++;
}

Expand All @@ -273,6 +279,7 @@ static int trace_cmd_cmd(FAR const char *name, int index, int argc,
changed = notectl_enable(name, true, notectlfd);

system(command);
lib_put_tempbuffer(command);

if (changed)
{
Expand Down

0 comments on commit fbaa045

Please sign in to comment.