From 2a835a2d8a7979d37820705087ad43bcdadc5201 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sun, 24 Jan 2021 00:49:36 +0000 Subject: [PATCH] Fix restoring DEBUG trap upon exiting virtual subshell This trap failed to be restored correctly when being trapped in a subshell, causing corruption or a crash when restoring the parent shell environment's trap upon leaving the subshell. Thanks to Koichi Nakashima for the report and reproducer. src/cmd/ksh93/sh/fault.c: sh_sigreset(): - Fix an off-by-one error in the loop that restores the pseudosignal traps. src/cmd/ksh93/tests/basic.sh: - Test overwriting the main shell trap in a subshell for all pseudosignals. Makes progress on: https://github.com/ksh93/ksh/issues/155 --- NEWS | 5 +++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/fault.c | 2 +- src/cmd/ksh93/tests/basic.sh | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 9f3f2dbf6..6db435a3e 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2021-01-23: + +- Fixed: when the DEBUG trap was redefined in a subshell, the DEBUG trap in + the parent environment was corrupted or the shell crashed. + 2021-01-22: - Compile-time shell options can now be edited in src/cmd/ksh93/SHOPT.sh diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 6bbda4519..3bd045bc4 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -20,7 +20,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2021-01-20" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2021-01-23" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2021 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh/fault.c b/src/cmd/ksh93/sh/fault.c index 72b3eeaf3..1873b6378 100644 --- a/src/cmd/ksh93/sh/fault.c +++ b/src/cmd/ksh93/sh/fault.c @@ -344,7 +344,7 @@ void sh_sigreset(register int mode) sh.sigflag[sig] = flag; } } - for(sig=SH_DEBUGTRAP-1;sig>=0;sig--) + for(sig=SH_DEBUGTRAP; sig>=0; sig--) { if(trap=sh.st.trap[sig]) { diff --git a/src/cmd/ksh93/tests/basic.sh b/src/cmd/ksh93/tests/basic.sh index a70cb2f19..1540693db 100755 --- a/src/cmd/ksh93/tests/basic.sh +++ b/src/cmd/ksh93/tests/basic.sh @@ -744,6 +744,22 @@ got=$(eval 'x=${ for i in test; do case $i in test) true;; esac; done; }' 2>&1) got=$(eval 'x=`for i in test; do case $i in test) true;; esac; done`' 2>&1) \ || err_exit "case in a for loop inside a \`comsub\` caused syntax error (got $(printf %q "$got"))" +# ====== +# The DEBUG trap crashed when re-trapping inside a subshell +# https://github.com/ksh93/ksh/issues/155 (#2) +exp=$'trap -- \': main\' EXIT\ntrap -- \': main\' ERR\ntrap -- \': main\' KEYBD\ntrap -- \': main\' DEBUG' +got=$({ "$SHELL" -c ' + PATH=/dev/null + for sig in EXIT ERR KEYBD DEBUG + do trap ": main" $sig + ( trap ": LEAK : $sig" $sig ) + trap + trap - "$sig" + done +'; } 2>&1) +((!(e = $?))) && [[ $got == "$exp" ]] || err_exit 'Pseudosignal trap failed when re-trapping in subshell' \ + "(got status $e$( ((e>128)) && print -n / && kill -l "$e"), $(printf %q "$got"))" + # ====== # The DEBUG trap had side effects on the exit status # https://github.com/ksh93/ksh/issues/155 (#4)