dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* stalls whenever PS4 contains $() and set -x
@ 2020-05-25 13:41 Yaroslav Halchenko
  2020-05-27  3:19 ` [PATCH] eval: Prevent recursive PS4 expansion Herbert Xu
  0 siblings, 1 reply; 2+ messages in thread
From: Yaroslav Halchenko @ 2020-05-25 13:41 UTC (permalink / raw)
  To: dash; +Cc: Kyle Meyer

Dear DASH maintainer(s),

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 


- posh:
exit:130 /tmp > posh test.sh
+export PS4=+ $(date +%T.%N) [$$] 
+ $(date +%T.%N) [$$] echo lets go
lets go
+ $(date +%T.%N) [$$] sleep 1
+ $(date +%T.%N) [$$] echo done 
done 

- dash: (stalls it set -x)

/tmp > dash test.sh        
+export PS4=+ $(date +%T.%N) [$$] 
^C^C

-- 
Yaroslav O. Halchenko
Center for Open Neuroscience     http://centerforopenneuroscience.org
Dartmouth College, 419 Moore Hall, Hinman Box 6207, Hanover, NH 03755
Phone: +1 (603) 646-9834                       Fax: +1 (603) 646-1419
WWW:   http://www.linkedin.com/in/yarik        

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH] eval: Prevent recursive PS4 expansion
  2020-05-25 13:41 stalls whenever PS4 contains $() and set -x Yaroslav Halchenko
@ 2020-05-27  3:19 ` Herbert Xu
  0 siblings, 0 replies; 2+ messages in thread
From: Herbert Xu @ 2020-05-27  3:19 UTC (permalink / raw)
  To: Yaroslav Halchenko; +Cc: dash, kyle

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 
> 
> 
> - posh:
> exit:130 /tmp > posh test.sh
> +export PS4=+ $(date +%T.%N) [$$] 
> + $(date +%T.%N) [$$] echo lets go
> lets go
> + $(date +%T.%N) [$$] sleep 1
> + $(date +%T.%N) [$$] 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.

Reported-by: Yaroslav Halchenko <yoh@onerussian.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/src/eval.c b/src/eval.c
index 1b5d61d..d10be38 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -78,6 +78,9 @@ int exitstatus;			/* exit status of last command */
 int back_exitstatus;		/* exit status of backquoted command */
 int savestatus = -1;		/* exit status of last command outside traps */
 
+/* Prevent PS4 nesting. */
+MKINIT int inps4;
+
 
 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
 STATIC
@@ -123,6 +126,7 @@ EXITRESET {
 	}
 	evalskip = 0;
 	loopnest = 0;
+	inps4 = 0;
 }
 #endif
 
@@ -855,12 +859,14 @@ bail:
 	}
 
 	/* Print the command if xflag is set. */
-	if (xflag) {
+	if (xflag && !inps4) {
 		struct output *out;
 		int sep;
 
 		out = &preverrout;
+		inps4 = 1;
 		outstr(expandstr(ps4val()), out);
+		inps4 = 0;
 		sep = 0;
 		sep = eprintlist(out, varlist.list, sep);
 		eprintlist(out, osp, sep);
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-05-27  3:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-25 13:41 stalls whenever PS4 contains $() and set -x Yaroslav Halchenko
2020-05-27  3:19 ` [PATCH] eval: Prevent recursive PS4 expansion Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).