mirror of
https://github.com/apache/nuttx-apps.git
synced 2025-10-21 23:02:13 +08:00
apps/nshlib/nsh_parse.c: Fixes an error in the NSH parser. There was a bug when executing an nsh shell script which contains a redirection. When the command in the script is executed, it sets the vtbl->np.np_redirect flag (as it should), but then doesn't restore it, leaving it set at the end of the script execution. Then the vtbl->np.np_redirect flag is set when the 'sh' command completes, causing a restore from un-initialized variables, thus leading to a crash. See the code snippet below for an example test case.
Test case: NuttShell (NSH) nsh> mkrd -s 1024 40 nsh> mkfatfs /dev/ram0 nsh> mount -t vfat /dev/ram0 /tmp nsh> echo "echo 1 > /dev/null" > /tmp/test.sh nsh> cat /tmp/test.sh echo 1 > /dev/null nsh> sh /tmp/test.sh ... The nsh prompt doesn't get printed. You can type a couple of commands, but then the system will crash because of bad pointers.
This commit is contained in:
@@ -2332,6 +2332,9 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
|
|||||||
int oflags = 0;
|
int oflags = 0;
|
||||||
int argc;
|
int argc;
|
||||||
int ret;
|
int ret;
|
||||||
|
#if CONFIG_NFILE_STREAMS > 0
|
||||||
|
bool redirect_save;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Initialize parser state */
|
/* Initialize parser state */
|
||||||
|
|
||||||
@@ -2500,6 +2503,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
|
|||||||
|
|
||||||
if (strcmp(argv[argc-2], g_redirect1) == 0)
|
if (strcmp(argv[argc-2], g_redirect1) == 0)
|
||||||
{
|
{
|
||||||
|
redirect_save = vtbl->np.np_redirect;
|
||||||
vtbl->np.np_redirect = true;
|
vtbl->np.np_redirect = true;
|
||||||
oflags = O_WRONLY|O_CREAT|O_TRUNC;
|
oflags = O_WRONLY|O_CREAT|O_TRUNC;
|
||||||
redirfile = nsh_getfullpath(vtbl, argv[argc-1]);
|
redirfile = nsh_getfullpath(vtbl, argv[argc-1]);
|
||||||
@@ -2510,6 +2514,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
|
|||||||
|
|
||||||
else if (strcmp(argv[argc-2], g_redirect2) == 0)
|
else if (strcmp(argv[argc-2], g_redirect2) == 0)
|
||||||
{
|
{
|
||||||
|
redirect_save = vtbl->np.np_redirect;
|
||||||
vtbl->np.np_redirect = true;
|
vtbl->np.np_redirect = true;
|
||||||
oflags = O_WRONLY|O_CREAT|O_APPEND;
|
oflags = O_WRONLY|O_CREAT|O_APPEND;
|
||||||
redirfile = nsh_getfullpath(vtbl, argv[argc-1]);
|
redirfile = nsh_getfullpath(vtbl, argv[argc-1]);
|
||||||
@@ -2537,6 +2542,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
|
|||||||
if (redirfile)
|
if (redirfile)
|
||||||
{
|
{
|
||||||
nsh_freefullpath(redirfile);
|
nsh_freefullpath(redirfile);
|
||||||
|
vtbl->np.np_redirect = redirect_save;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user