Fix some edge case bugs in nsh_parse

- Handle nsh_filecat returning NULL on failure
- Background and redirect must be restored after an empty line
- Output redirection should be removed from argv like background
This commit is contained in:
Norman Rasmussen 2021-12-31 06:54:57 -08:00 committed by Xiang Xiao
parent 6993c66389
commit 92287e2b14

View File

@ -943,8 +943,15 @@ static FAR char *nsh_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
/* Concatenate the file contents with the current allocation */ /* Concatenate the file contents with the current allocation */
argument = nsh_filecat(vtbl, *allocation, tmpfile); argument = nsh_filecat(vtbl, *allocation, tmpfile);
*allocation = argument; if (argument == NULL)
{
argument = (FAR char *)g_nullstring;
}
else
{
*allocation = argument;
}
/* We can now unlink the tmpfile and free the tmpfile string */ /* We can now unlink the tmpfile and free the tmpfile string */
@ -2305,8 +2312,8 @@ static int nsh_parse_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
* status. * status.
*/ */
NSH_MEMLIST_FREE(&memlist); ret = 0;
return OK; goto exit;
} }
/* Parse all of the arguments following the command name. The form /* Parse all of the arguments following the command name. The form
@ -2346,6 +2353,7 @@ static int nsh_parse_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
/* Restore the backgrounding and redirection state */ /* Restore the backgrounding and redirection state */
exit:
#ifndef CONFIG_NSH_DISABLEBG #ifndef CONFIG_NSH_DISABLEBG
vtbl->np.np_bg = bgsave; vtbl->np.np_bg = bgsave;
#endif #endif
@ -2522,17 +2530,12 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
} }
} }
/* Last argument vector must be empty */
argv[argc] = NULL;
/* Check if the command should run in background */ /* Check if the command should run in background */
#ifndef CONFIG_NSH_DISABLEBG #ifndef CONFIG_NSH_DISABLEBG
if (argc > 1 && strcmp(argv[argc - 1], "&") == 0) if (argc > 1 && strcmp(argv[argc - 1], "&") == 0)
{ {
vtbl->np.np_bg = true; vtbl->np.np_bg = true;
argv[argc - 1] = NULL;
argc--; argc--;
} }
#endif #endif
@ -2566,6 +2569,10 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
} }
#endif #endif
/* Last argument vector must be empty */
argv[argc] = NULL;
/* Check if the maximum number of arguments was exceeded */ /* Check if the maximum number of arguments was exceeded */
if (argc > CONFIG_NSH_MAXARGUMENTS) if (argc > CONFIG_NSH_MAXARGUMENTS)