ash: prevent error in backquotes in PS1 from exiting shell
authorRon Yorston <rmy@pobox.com>
Thu, 18 Apr 2019 08:48:13 +0000 (09:48 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 19 Apr 2019 11:21:34 +0000 (13:21 +0200)
Setting PS1 to:

   PS1='`xxx(`'

causes the shell to terminate with the error:

   sh: syntax error: unexpected end of file (expecting ")")

This happens because old-style backquotes require the input to be reread
and thus call setinputstring() a second time.  Prevent the problem by
unwinding all recently opened files in expandstr().

function                                             old     new   delta
unwindfiles                                            -      22     +22
expandstr                                            247     262     +15
forkchild                                            631     625      -6
evalcommand                                         1694    1685      -9
ash_main                                            1346    1336     -10
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/3 up/down: 37/-25)             Total: 12 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c

index b707d00d055a416d28e18abaf44306826c856734..f3a2c6952aad4f894a555206efe863ba3e737c0e 100644 (file)
@@ -13042,6 +13042,7 @@ expandstr(const char *ps, int syntax_type)
 {
        union node n;
        int saveprompt;
+       struct parsefile *file_stop = g_parsefile;
 
        /* XXX Fix (char *) cast. */
        setinputstring((char *)ps);
@@ -13068,7 +13069,8 @@ expandstr(const char *ps, int syntax_type)
 
        doprompt = saveprompt;
 
-       popfile();
+       /* Try: PS1='`xxx(`' */
+       unwindfiles(file_stop);
 
        n.narg.type = NARG;
        n.narg.next = NULL;