eval: Prevent recursive PS4 expansion
commitd80189f2b99a4172a60589b6c23eedc2b4077a20
authorHerbert Xu <herbert@gondor.apana.org.au>
Wed, 27 May 2020 03:19:10 +0000 (27 13:19 +1000)
committerHerbert Xu <herbert@gondor.apana.org.au>
Mon, 1 Jun 2020 13:28:36 +0000 (1 23:28 +1000)
tree29af946fa09ffabbe01bfb2d96dea3b13fb79c08
parent8557066bcf8d76734331051cfbcdd9ba6cbb730f
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
>
>
> - 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>
src/eval.c