ksh: Some bugs and crashes when using the DEBUG trap
Tested with ksh Version AJMv 93u+m/1.0.0-alpha+ccd98fe7 2021-01-08
on Ubuntu 20.04
1. Redirecting disables the DEBUG trap
edit: fixed in 2a835a2d8a7979d37820705087ad43bcdadc5201, e664b78f980fa31bd5e4c5755ce025dd5dae5618
trap 'echo LINENO: $LINENO >&1' DEBUG # 1
echo foo # 2
var=$(echo) # 3
echo bar # 4
echo baz # 5
Expected (This is what happens when you remove >&1
).
LINENO: 2
foo
LINENO: 3
LINENO: 4
bar
LINENO: 5
baz
Actual
LINENO: 2
foo
bar
baz
2. Crashes when re-trapping inside a subshell
edit: fixed in 2a835a2d8a7979d37820705087ad43bcdadc5201
set -e
trap ':' DEBUG
( trap ':' DEBUG )
echo ok
ksh.sh[22]: ^P?`?^X?%?d: not found [No such file or directory]
3. The split word will not work
edit: fixed in 70368c57d67c6d3f327a52de39697e8c6647ee7a
v=''
trap ': $v' DEBUG
A="a b c"
set -- $A
printf '%s\n' "$@"
Expected
a
b
c
Actual
a b c
4. Side effects to exit status
edit: fixed in d00b4b39f66e2912797195f2aac9913218826350
trap ':' DEBUG
(exit 123)
echo $? # => 123 (Correct)
r=$(exit 123)
echo $? # => Same as above, expected 123, but actually 0.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 19
Commits related to this issue
- 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 r... — committed to ksh93/ksh by McDutchie 3 years ago
- 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 enviro... — committed to ksh93/ksh by McDutchie 3 years ago
- Add regress test for redirection in DEBUG trap action (re: 2a835a2d) Turns out the previous commit also fixed the bug that disables the DEBUG trap if a redirection is used in a DEBUG trap action -- i... — committed to ksh93/ksh by McDutchie 3 years ago
- Fix field splitting bug triggered by DEBUG trap An unquoted variable expansion evaluated in a DEBUG trap action caused IFS field splitting to be deactivated in code executed after the trap action. Th... — committed to ksh93/ksh by McDutchie 3 years ago
Thank you. However, unfortunately it did not work in my actual use case. It might be because I use a lot of subshells or enable/disable the
DEBUG
trap many times, but I’m not sure why.I am using the following workaround. This avoids bug 1 and bug 3. I am guessing that the internal state was reset by reassigning the
IFS
.The very handy multishell repo allows us to use ‘git blame’ to trace the origin of these bugs.
The off-by-one error causing various bugs was introduced in ksh 93t 2008-07-25:
https://github.com/multishell/ksh93/commit/8e947ccf8ce2bf1fda57f04e59f4a9a4782cab99 (fault.c, line 321)
The incorrect check causing the exit status bug was introduced in ksh 93t 2008-11-04:
https://github.com/multishell/ksh93/commit/b1ade26843968b5606753eba1e0afa099daab528 (fault.c, line 459)
The ifstable save/restore causing the field splitting bug was introduced in ksh 93t+ 2009-11-30:
https://github.com/multishell/ksh93/commit/53d9f0095adece1dfe82a9f2b878fc6fc811a8bc (fault.c, lines 440, 444, 482)
So all the bugs reported in this issue were fixed by simply reverting these specific changes. I think that they are some experiments that the developers simply forgot to remove. I’ve suspected such a thing multiple times before. ksh93 was developed by researchers who were genius innovators, but incredibly sloppy maintainers.
The crash caused by bug 2 is intermittent for me, but when it doesn’t crash, it leaks:
Output (occasionally):
The problem seems to be an off-by-one error when resetting/restoring the traps. This fix works for me:
Output after fix:
…and your own test case passes as well.
Cool, glad to see that. 😃 The fix for redefining a running function was part of 047cb3303c7ea80a9cfde74c517b0496093abb65.
It’s still experimental, but I’ve started testing 93u+m (https://github.com/shellspec/shellspec/commit/aed1c899c23e4a9f89115b85e7a3bb15b5a46662). It seems to be working fine.
I’m glad to see that the following code now works correctly with 93u+m. It was not working with ksh2020 on Ubuntu 20.04 either.
That’s impressive! I could not find a workaround on the shell script side of it.
Thank you for rebooting the development of ksh!
Bug 4 (side effects to exit status) is an easy fix:
Confirmed also on ksh 93u+, but not ksh2020. There may be a fix to backport.edit: in fact, ksh2020 has the bug, the crash is just intermittent. If it doesn’t crash, it leaks. See: https://github.com/ksh93/ksh/issues/155#issuecomment-766216838So, at least this 93u+m fork didn’t introduce any of these bugs 😉. Downside of that is that these longstanding bugs will probably be difficult to track down.