aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-09-09 16:26:41 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-09-09 16:29:37 +0200
commiteb607777697f4c5eb2dfd86e5837a8c379f65979 (patch)
treeb623e6d90bd7a2dfdb2e48226e3c4757e11affdd
parent48cb983b136fb74c61db594a30e18bdc42b7264c (diff)
downloadbusybox-w32-eb607777697f4c5eb2dfd86e5837a8c379f65979.tar.gz
busybox-w32-eb607777697f4c5eb2dfd86e5837a8c379f65979.tar.bz2
busybox-w32-eb607777697f4c5eb2dfd86e5837a8c379f65979.zip
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>
-rw-r--r--shell/ash.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/shell/ash.c b/shell/ash.c
index ba116d83a..3524d046e 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -404,6 +404,7 @@ struct globals_misc {
404 uint8_t exitstatus; /* exit status of last command */ 404 uint8_t exitstatus; /* exit status of last command */
405 uint8_t back_exitstatus;/* exit status of backquoted command */ 405 uint8_t back_exitstatus;/* exit status of backquoted command */
406 smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ 406 smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */
407 smallint inps4; /* Prevent PS4 nesting. */
407 int savestatus; /* exit status of last command outside traps */ 408 int savestatus; /* exit status of last command outside traps */
408 int rootpid; /* pid of main shell */ 409 int rootpid; /* pid of main shell */
409 /* shell level: 0 for the main shell, 1 for its children, and so on */ 410 /* 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;
492#define exitstatus (G_misc.exitstatus ) 493#define exitstatus (G_misc.exitstatus )
493#define back_exitstatus (G_misc.back_exitstatus ) 494#define back_exitstatus (G_misc.back_exitstatus )
494#define job_warning (G_misc.job_warning) 495#define job_warning (G_misc.job_warning)
496#define inps4 (G_misc.inps4 )
495#define savestatus (G_misc.savestatus ) 497#define savestatus (G_misc.savestatus )
496#define rootpid (G_misc.rootpid ) 498#define rootpid (G_misc.rootpid )
497#define shlvl (G_misc.shlvl ) 499#define shlvl (G_misc.shlvl )
@@ -10423,10 +10425,12 @@ evalcommand(union node *cmd, int flags)
10423 } 10425 }
10424 10426
10425 /* Print the command if xflag is set. */ 10427 /* Print the command if xflag is set. */
10426 if (xflag) { 10428 if (xflag && !inps4) {
10427 const char *pfx = ""; 10429 const char *pfx = "";
10428 10430
10431 inps4 = 1;
10429 fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX)); 10432 fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX));
10433 inps4 = 0;
10430 10434
10431 sp = varlist.list; 10435 sp = varlist.list;
10432 while (sp) { 10436 while (sp) {
@@ -14323,6 +14327,7 @@ exitreset(void)
14323 } 14327 }
14324 evalskip = 0; 14328 evalskip = 0;
14325 loopnest = 0; 14329 loopnest = 0;
14330 inps4 = 0;
14326 14331
14327 /* from expand.c: */ 14332 /* from expand.c: */
14328 ifsfree(); 14333 ifsfree();