1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 03:32:24 +00:00

Fix side effect to exit status of DEBUG trap in comsub

This fixes the following:

trap ':' DEBUG
r=$(exit 123)
echo $? # Expected 123, but actually 0.

Thanks to Koichi Nakashima for the report and reproducer.

src/cmd/ksh93/sh/fault.c: sh_trap():
- Restore the saved current exit status (exitval) for all traps.
  Do not except the DEBUG trap from doing that. I've no idea why
  this exception was made, but it's not correct.

src/cmd/ksh93/tests/basic.sh:
- Add tests.

Makes progress on: https://github.com/ksh93/ksh/issues/155
This commit is contained in:
Martijn Dekker 2021-01-20 17:40:09 +00:00
parent 5ee290c7a8
commit d00b4b39f6
4 changed files with 19 additions and 2 deletions

5
NEWS
View file

@ -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-20:
- Fixed: executing a DEBUG trap in a command substitution had side effects
on the exit status ($?) of non-trap commands.
2021-01-19:
- Fixed a crash when using 'cd' in a virtual/non-forking subshell in a

View file

@ -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-19" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2021-01-20" /* must be in this format for $((.sh.version)) */
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
/* Arithmetic $((.sh.version)) uses the last 10 chars, so the date must be at the end. */

View file

@ -489,7 +489,7 @@ int sh_trap(const char *trap, int mode)
sh_popcontext(shp,&buff);
shp->intrap--;
sfsync(shp->outpool);
if(!shp->indebug && jmpval!=SH_JMPEXIT && jmpval!=SH_JMPFUN)
if(jmpval!=SH_JMPEXIT && jmpval!=SH_JMPFUN)
shp->exitval=savxit;
stakset(savptr,staktop);
fcrestore(&savefc);

View file

@ -734,5 +734,17 @@ 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 had side effects on the exit status
# https://github.com/ksh93/ksh/issues/155 (#4)
trap ':' DEBUG
(exit 123)
(((e=$?)==123)) || err_exit "DEBUG trap run in subshell affects exit status (expected 123, got $e)"
r=$(exit 123)
(((e=$?)==123)) || err_exit "DEBUG trap run in \$(comsub) affects exit status (expected 123, got $e)"
r=`exit 123`
(((e=$?)==123)) || err_exit "DEBUG trap run in \`comsub\` affects exit status (expected 123, got $e)"
trap - DEBUG
# ======
exit $((Errors<125?Errors:125))