Skip to content

Commit 3a6ecb8

Browse files
jasonbuxiaoxiang781216
authored andcommitted
nshlib/nsh_parse: Fix variable arguments concat error of nsh_execute()
Without this patch nsh> set time 5 nsh> echo $time 5 nsh> sleep $time & sh [5:100] nsh> nsh: sleep: missing required argument(s) With this patch nsh> set time 5 nsh> echo $time 5 nsh> sleep $time & sh [4:100] nsh> ps PID GROUP PRI POLICY TYPE NPX STATE EVENT SIGMASK STACK COMMAND 0 0 0 FIFO Kthread - Ready 0000000000000000 0069616 Idle_Task 1 0 224 FIFO Kthread - Waiting Signal 0000000000000000 0067536 loop_task 2 0 224 FIFO Kthread - Waiting Semaphore 0000000000000000 0067504 hpwork 0x501760e0 0x50176128 3 3 100 FIFO Task - Running 0000000000000000 0067536 nsh_main 4 4 100 FIFO Task - Waiting Signal 0000000000000000 0067520 sh -c sleep Signed-off-by: buxiasen <buxiasen@xiaomi.com> Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
1 parent 1c7a7f7 commit 3a6ecb8

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

nshlib/nsh_parse.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -603,24 +603,25 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
603603
{
604604
FAR char *sh_argv[4];
605605
FAR char *sh_cmd = "sh";
606-
int i;
606+
char sh_arg2[CONFIG_NSH_LINELEN];
607607

608608
DEBUGASSERT(strncmp(argv[0], sh_cmd, 3) != 0);
609609

610-
sh_argv[0] = sh_cmd;
611-
sh_argv[1] = "-c";
612-
for (i = 0; i < argc - 1; i++)
613-
{
614-
FAR char *p_arg = argv[i];
615-
size_t len = strlen(p_arg);
610+
sh_arg2[0] = '\0';
616611

617-
/* Restore from split args to concat args. */
612+
for (ret = 0; ret < argc; ret++)
613+
{
614+
strlcat(sh_arg2, argv[ret], sizeof(sh_arg2));
618615

619-
DEBUGASSERT(&p_arg[len + 1] == argv[i + 1]);
620-
p_arg[len] = ' ';
616+
if (ret < argc - 1)
617+
{
618+
strcat(sh_arg2, " ");
619+
}
621620
}
622621

623-
sh_argv[2] = argv[0];
622+
sh_argv[0] = sh_cmd;
623+
sh_argv[1] = "-c";
624+
sh_argv[2] = sh_arg2;
624625
sh_argv[3] = NULL;
625626

626627
/* np.np_bg still there, try use nsh_builtin or nsh_fileapp to

0 commit comments

Comments
 (0)