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 15, 2025
1 parent 14fa7d2 commit 5208142
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 30 deletions.
19 changes: 13 additions & 6 deletions examples/lp503x/lp503x_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,20 +625,26 @@ static int lp503x_cmd_help(FAR char *parg)

int main(int argc, FAR char *argv[])
{
FAR char *buffer;
FAR char *cmd;
FAR char *arg;
bool running;
char buffer[LINE_MAX];
int len;
int x;
char *cmd;
char *arg;

fd = open(CONFIG_EXAMPLES_LP503X_DEVPATH, O_CREAT);
if (fd < 0)
{
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
CONFIG_EXAMPLES_LP503X_DEVPATH, errno);
close(fd);
return ENODEV;
return -ENODEV;
}

buffer = lib_get_tempbuffer(LINE_MAX);
if (buffer == NULL)
{
close(fd);
return -ENOMEM;
}

running = true;
Expand All @@ -649,7 +655,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);
buffer[len] = '\0';
if (len > 0)
Expand Down Expand Up @@ -715,6 +721,7 @@ int main(int argc, FAR char *argv[])
}
}

lib_put_tempbuffer(buffer);
close(fd);

return 0;
Expand Down
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 */
9 changes: 9 additions & 0 deletions nshlib/nsh_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,14 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
FAR char *sh_argv[4];
FAR char *sh_arg2;

sh_arg2 = lib_get_tempbuffer(LINE_MAX);
if (sh_arg2 == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, cmd);
ret = -ENOMEM;
goto dynlist_free;
}

if (argv[argc][g_pipeline1_len])
{
arg = &argv[argc][g_pipeline1_len];
Expand All @@ -2708,6 +2716,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
if (!arg)
{
nsh_error(vtbl, g_fmtarginvalid, cmd);
lib_put_tempbuffer(sh_arg2);
ret = ERROR;
goto dynlist_free;
}
Expand Down
12 changes: 11 additions & 1 deletion nshlib/nsh_timcmds.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 <stdlib.h>
#include <string.h>
#include <strings.h>
Expand Down Expand Up @@ -552,7 +553,7 @@ 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];
FAR char *buffer;
int interval = 2;
int count = -1;
FAR char *cmd;
Expand Down Expand Up @@ -593,19 +594,28 @@ int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
count = INT_MAX;
}

buffer = lib_get_tempbuffer(LINE_MAX);
if (buffer == NULL)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], cmd, ENOMEM);
return -ENOMEM;
}

for (i = 0; i < count; i++)
{
strlcpy(buffer, cmd, LINE_MAX);
ret = nsh_parse(vtbl, buffer);
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], cmd, NSH_ERRNO);
lib_put_tempbuffer(buffer);
return ERROR;
}

sleep(interval);
}

lib_put_tempbuffer(buffer);
return OK;
}
#endif
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;
}
Loading

0 comments on commit 5208142

Please sign in to comment.