[git commit] ash: eval: Prevent recursive PS4 expansion

Denys Vlasenko vda.linux at googlemail.com
Thu Sep 9 14:29:37 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=eb607777697f4c5eb2dfd86e5837a8c379f65979
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

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

    Yaroslav Halchenko <yoh at 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 at googlemail.com>
---
 shell/ash.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

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 {
 	uint8_t exitstatus;     /* exit status of last 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 inps4;		/* Prevent PS4 nesting. */
 	int savestatus;         /* exit status of last command outside traps */
 	int rootpid;            /* pid of main shell */
 	/* 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 back_exitstatus   (G_misc.back_exitstatus )
 #define job_warning       (G_misc.job_warning)
+#define inps4       (G_misc.inps4      )
 #define savestatus  (G_misc.savestatus )
 #define rootpid     (G_misc.rootpid    )
 #define shlvl       (G_misc.shlvl      )
@@ -10423,10 +10425,12 @@ evalcommand(union node *cmd, int flags)
 	}
 
 	/* Print the command if xflag is set. */
-	if (xflag) {
+	if (xflag && !inps4) {
 		const char *pfx = "";
 
+		inps4 = 1;
 		fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX));
+		inps4 = 0;
 
 		sp = varlist.list;
 		while (sp) {
@@ -14323,6 +14327,7 @@ exitreset(void)
 	}
 	evalskip = 0;
 	loopnest = 0;
+	inps4 = 0;
 
 	/* from expand.c: */
 	ifsfree();


More information about the busybox-cvs mailing list