ash: eval: Prevent recursive PS4 expansion

Date: Wed, 27 May 2020 13:19:10 +1000
eval: Prevent recursive PS4 expansion

    Yaroslav Halchenko <yoh@onerussian.com> wrote:
    > I like to (ab)use PS4 and set -x for tracing execution of scripts.
    > Reporting time and PID is very useful in this context.
    >
    > I am not 100% certain if bash's behavior (of actually running the command
    > embedded within PS4 string, probably eval'ing it) is actually POSIX
    > compliant, posh seems to not do that; but I think it is definitely not
    > desired for dash to just stall:
    >
    > - the script:
    > #!/bin/sh
    > set -x
    > export PS4='+ $(date +%T.%N) [$$] '
    > echo "lets go"
    > sleep 1
    > echo "done $var"
    >
    > - bash:
    > /tmp > bash --posix test.sh
    > +export 'PS4=+ $(date +%T.%N) [$$] '
    > +PS4='+ $(date +%T.%N) [$$] '
    > + 09:15:48.982296333 [2764323] echo 'lets go'
    > lets go
    > + 09:15:48.987829613 [2764323] sleep 1
    > + 09:15:49.994485037 [2764323] echo 'done '
    > done
    >
    ...
    > - dash: (stalls it set -x)
    > /tmp > dash test.sh
    > +export PS4=+ $(date +%T.%N) [$$]
    > ^C^C

    This patch fixes the infinite loop caused by repeated expansions
    of PS4.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2021-09-09 16:26:41 +02:00
parent 48cb983b13
commit eb60777769

View File

@ -404,6 +404,7 @@ struct globals_misc {
uint8_t exitstatus; /* exit status of last command */ uint8_t exitstatus; /* exit status of last command */
uint8_t back_exitstatus;/* exit status of backquoted command */ uint8_t back_exitstatus;/* exit status of backquoted command */
smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */
smallint inps4; /* Prevent PS4 nesting. */
int savestatus; /* exit status of last command outside traps */ int savestatus; /* exit status of last command outside traps */
int rootpid; /* pid of main shell */ int rootpid; /* pid of main shell */
/* shell level: 0 for the main shell, 1 for its children, and so on */ /* shell level: 0 for the main shell, 1 for its children, and so on */
@ -492,6 +493,7 @@ extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc;
#define exitstatus (G_misc.exitstatus ) #define exitstatus (G_misc.exitstatus )
#define back_exitstatus (G_misc.back_exitstatus ) #define back_exitstatus (G_misc.back_exitstatus )
#define job_warning (G_misc.job_warning) #define job_warning (G_misc.job_warning)
#define inps4 (G_misc.inps4 )
#define savestatus (G_misc.savestatus ) #define savestatus (G_misc.savestatus )
#define rootpid (G_misc.rootpid ) #define rootpid (G_misc.rootpid )
#define shlvl (G_misc.shlvl ) #define shlvl (G_misc.shlvl )
@ -10423,10 +10425,12 @@ evalcommand(union node *cmd, int flags)
} }
/* Print the command if xflag is set. */ /* Print the command if xflag is set. */
if (xflag) { if (xflag && !inps4) {
const char *pfx = ""; const char *pfx = "";
inps4 = 1;
fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX)); fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX));
inps4 = 0;
sp = varlist.list; sp = varlist.list;
while (sp) { while (sp) {
@ -14323,6 +14327,7 @@ exitreset(void)
} }
evalskip = 0; evalskip = 0;
loopnest = 0; loopnest = 0;
inps4 = 0;
/* from expand.c: */ /* from expand.c: */
ifsfree(); ifsfree();