Skip to content

Commit

Permalink
nshlib/pipeline: Concat variable arguments failed
Browse files Browse the repository at this point in the history
1. Without this patch

  nsh> set var_test `uname`
  nsh> echo $var_test
  NuttX
  nsh> echo $var_test | cat
  sh [5:100]

  nsh>

2. With this patch

  nsh> set var_test `uname`
  nsh> echo $var_test
  NuttX
  nsh> echo $var_test | cat
  sh [4:100]
  NuttX
  nsh>

Signed-off-by: wangjianyu3 <[email protected]>
  • Loading branch information
JianyuWang0623 committed Nov 12, 2024
1 parent 0c467dc commit 0555d73
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions nshlib/nsh_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
{
FAR char *arg;
FAR char *sh_argv[4];
char cmd_str[CONFIG_NSH_LINELEN];

if (argv[argc][g_pipeline1_len])
{
Expand All @@ -2693,20 +2694,27 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
goto dynlist_free;
}

for (ret = 0; ret < argc - 1; ret++)
memset(cmd_str, 0, sizeof(cmd_str));

for (ret = 0; ret < argc; ret++)
{
FAR char *p_arg = argv[ret];
size_t len = strlen(p_arg);
if (sizeof(cmd_str) - strlen(cmd_str) - 1 <
strlen(argv[ret]) + 1)
{
goto dynlist_free;
}

/* Restore from split args to concat args. */
strcat(cmd_str, argv[ret]);

DEBUGASSERT(&p_arg[len + 1] == argv[ret + 1]);
p_arg[len] = ' ';
if (ret < argc - 1)
{
strcat(cmd_str, " ");
}
}

sh_argv[0] = "sh";
sh_argv[1] = "-c";
sh_argv[2] = argv[0];
sh_argv[2] = cmd_str;
sh_argv[3] = NULL;

ret = pipe2(pipefd, 0);
Expand Down

0 comments on commit 0555d73

Please sign in to comment.