CVSROOT:        /cvs
Module name:    src
Changes by:     k...@cvs.openbsd.org    2022/10/10 08:57:48

Modified files:
        bin/ksh        : exec.c 
Added files:
        regress/bin/ksh: trap.t 

Log message:
Trigger ERR trap on permanent I/O redirection failure

The following three cases behave identical in bash(1), but our ksh
(ksh93 also) fails to run the trap in the last case:

(non-zero exit code is trigger, no redirection)
$ ksh -c 'trap "echo ERR" ERR ; false'
ERR

(failed redirection is trigger, 'echo' was not executed)
$ ksh -c 'trap "echo ERR" ERR ; echo >/'
ksh: cannot create /: Is a directory
ERR

(failed redirection, no execution, trap was NOT triggered)
$ ksh -c 'trap "echo ERR" ERR ; exec >/'
ksh: cannot create /: Is a directory

bash(1) prints "ERR" in all three cases, as expected.
ksh93 behaves like our ksh(1).

In ksh `exec' is a builtin (CSHELL), but also special (SPEC_BI):
$ type alias
alias is a shell builtin
$ type exec
exec is a special shell builtin

Without command and redirection alone, `exec' permanently redirects I/O for
the shell itself, not executing anything;  it is the only (special) builtin
with such a special use-case, implemented as c_sh.c:c_exec().

This corner-case is overlooked in exec.c:execute() which handles iosetup()
failure for all commands, incl. builtins.

Exclude c_exec() from the rest of special builtins to ensure it runs the
ERR trap as expected:

$ ./obj/ksh -c 'trap "echo ERR" ERR ; exec >/'
ksh: cannot create /: Is a directory
ERR

Also add three new regress cases covering this;  rest keep passing.

OK millert

Reply via email to