From 90e25207c41010a5307beff7fd7a5a9e4be3653e Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Fri, 27 Nov 2026 17:26:45 -0600 Subject: fix: the review findings, each with a regression test Notes in notes/review-2026-11-27.md, one item per finding: 1. error unwinding read dead stack frames: the scope/redirection/input snapshot now lives in jmploc and is restored before the longjmp 2. `for` without `in` iterated over parameters that `set --` could free: the parameter block is reference-counted 3. `${4294967295}` and `${99999999999999999999}` indexed past the end: strtol, clamped to 1..$# 4. a trap stopped running after its own action raised: the re-entry flag is cleared before the action runs 5. `wait $pid` returned 127 once the job had been reaped 6. every external command failed with 127 when SIGCHLD was ignored at startup: dispositions are reset before the first child 7. a failed `exec` killed an interactive shell and skipped its EXIT trap: the shell returns 126/127, keeping the traps and restoring fds 0-9 8. `set -C` opened with O_TRUNC after a stat: O_EXCL, with the non-regular-file case worked around 9. a here-doc cut off by EOF used the delimiter as its body 10. `cd -- dir` failed and PWD came from the failed logical path 11. deep arithmetic recursion overflowed the stack: a depth check 12. the CSI type byte was converted twice on EBCDIC: guarded by ZASH_ASCII 13. a script that could not be read looked like an empty script: reported, with 2 at top level and 126/127 for the script; a directory is refused up front, because z/OS would otherwise read it as empty input Minors: `trap -p SIG`, `read -t` clamping and NaN, ksh `base#n` arithmetic, `${#@}`/`${#*}` as `$#`, `jobs` no longer mutating its table from a pipeline, the arena_grow contract, no pipe2 on macOS, and alias text copied into the input so redefining an alias while it is being read is safe. --- src/exec.c | 164 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 97 insertions(+), 67 deletions(-) (limited to 'src/exec.c') diff --git a/src/exec.c b/src/exec.c index c0267a8..b1db90b 100644 --- a/src/exec.c +++ b/src/exec.c @@ -9,6 +9,7 @@ * parent: its arguments, its environment and a plan for its descriptors. */ #include "exec.h" +#include "var.h" #include #include @@ -488,26 +489,6 @@ void exec_init(void) { var_find("OPTIND", 6, true)->hook = getopts_reset; } -/* ---- unwinding ------------------------------------------------------- */ - -/** - * @brief Record the state an exception handler must put back. - */ -void unwind_save(unwind *u) { - u->scope = var_scope; - u->redir = redir_top; - u->input = input; -} - -/** - * @brief Undo redirections, variable scopes and inputs back to a saved state. - */ -void unwind_restore(const unwind *u) { - redir_unwind(u->redir); - scope_unwind(u->scope); - input_unwind(u->input); -} - /* ---- processes ------------------------------------------------------- */ /** @@ -546,7 +527,7 @@ pid_t fork_shell(void) { [[noreturn]] static void child_eval(node *n) { jmploc jl; if (setjmp(jl.buf) == 0) { - exc_handler = &jl; + exc_catch(&jl); eval_node(n, EV_EXIT); } exit_shell(exitstatus); @@ -627,7 +608,8 @@ static int exec_error(const char *name, int err) { */ [[noreturn]] static void exec_path(const char *path, char **argv, char **envp) { flush_all(); - trap_reset_child(); + trap_reset_exec(); /* not trap_reset_child: see below, a failed exec runs + * the EXIT trap */ COVERAGE_FLUSH(); execve(path, argv, envp); if (errno == ENOEXEC) { @@ -637,28 +619,62 @@ static int exec_error(const char *name, int err) { } int status = exec_error(argv[0], errno); flush_all(); - _exit(status); + exit_shell(status); /* runs the EXIT trap */ } /** - * @brief The `exec` builtin with a command: find it and replace the shell. + * @brief Find the program `exec` should run. + * @return Its path, or nullptr if it is not found. */ -void shell_exec(char **argv, char **envp) { +static const char *exec_lookup(char **argv) { cmdinfo ci; find_command(argv[0], &ci, FC_NOFUNC); - const char *path = ci.kind == C_EXTERNAL ? ci.path : nullptr; - if (!path && !strchr(argv[0], '/')) { - const char *search = var_get("PATH"); - path = path_search(argv[0], search ? search : default_path, true); - } + if (ci.kind == C_EXTERNAL) + return ci.path; + if (strchr(argv[0], '/')) + return nullptr; + const char *search = var_get("PATH"); + return path_search(argv[0], search ? search : default_path, true); +} + +/** + * @brief The `exec` builtin with a command: find it and replace the shell. + */ +void shell_exec(char **argv, char **envp) { + const char *path = exec_lookup(argv); if (!path) { int status = exec_error(argv[0], ENOENT); flush_all(); - _exit(status); + exit_shell(status); } exec_path(path, argv, envp); } +/** + * @brief `exec` for a shell that must survive a failure. + * + * POSIX 2.8.1: a special built-in that fails does not end an interactive + * shell, so the error is reported and returned. The program is exec'd + * without resetting the shell's traps first, since it may still be here + * afterwards. + * + * @return The status to report if the exec did not happen. + */ +int shell_exec_try(char **argv, char **envp) { + const char *path = exec_lookup(argv); + if (!path) + return exec_error(argv[0], ENOENT); + flush_all(); + COVERAGE_FLUSH(); + execve(path, argv, envp); + if (errno == ENOEXEC) { + char **nv = script_argv(path, argv); + if (nv) + execve(nv[0], nv, envp); + } + return exec_error(argv[0], errno); +} + /** * @brief spawn() a program. * @@ -814,14 +830,11 @@ static void simple_prep(node *n, simple_cmd *sc) { static bool guarded_prep(node *n, simple_cmd *sc) { jmploc jl; jmploc *prev = exc_handler; - unwind u; bool ok = true; - unwind_save(&u); if (setjmp(jl.buf) == 0) { - exc_handler = &jl; + exc_catch(&jl); simple_prep(n, sc); } else { - unwind_restore(&u); ok = false; } exc_handler = prev; @@ -1004,8 +1017,6 @@ static int finish_builtin(int status) { static int run_special_guarded(const builtin *bi, simple_cmd *sc) { jmploc jl; jmploc *prev = exc_handler; - unwind u; - unwind_save(&u); arena_marker mark = arena_mark(&scratch); int saved_loop = loop_depth; int saved_dot = dot_depth; @@ -1014,7 +1025,6 @@ static int run_special_guarded(const builtin *bi, simple_cmd *sc) { exc_handler = prev; if (exc_code == EXC_EXIT) exc_raise(EXC_EXIT); - unwind_restore(&u); arena_release(&scratch, mark); loop_depth = saved_loop; dot_depth = saved_dot; @@ -1022,7 +1032,7 @@ static int run_special_guarded(const builtin *bi, simple_cmd *sc) { skip_kind = SKIP_NONE; return exitstatus ? exitstatus : 2; } - exc_handler = &jl; + exc_catch(&jl); int status = bi->fn(sc->argc, sc->argv); exc_handler = prev; return status; @@ -1059,8 +1069,10 @@ static int run_builtin(simple_cmd *sc) { status = finish_builtin(sc->via_command && (bi->flags & BI_SPECIAL) ? run_special_guarded(bi, sc) : bi->fn(sc->argc, sc->argv)); + /* `exec >file` keeps the redirections; a failed `exec cmd` must put + * the shell's own descriptors back. */ if (n->redirs) - redir_pop(bi->fn == builtin_exec); + redir_pop(bi->fn == builtin_exec && sc->argc <= 1); } if (scoped) scope_pop(); @@ -1089,7 +1101,7 @@ int call_function(func *fn, int argc, char **argv, unsigned flags) { loop_depth = 0; shellparam = (shparam){argc - 1, argv + 1, nullptr}; if (setjmp(jl.buf) == 0) { - exc_handler = &jl; + exc_catch(&jl); eval_node(fn->body, flags & EV_TESTED); } else { raised = true; @@ -1177,7 +1189,7 @@ static int simple_run(simple_cmd *sc, unsigned flags) { [[noreturn]] static void child_simple(simple_cmd *sc) { jmploc jl; if (setjmp(jl.buf) == 0) { - exc_handler = &jl; + exc_catch(&jl); exitstatus = simple_run(sc, EV_EXIT); } exit_shell(exitstatus); @@ -1203,16 +1215,13 @@ static int run_pure(simple_cmd *sc, output *o) { output saved = out1; jmploc jl; jmploc *prev = exc_handler; - unwind u; int status; - unwind_save(&u); out1 = *o; if (setjmp(jl.buf) == 0) { - exc_handler = &jl; + exc_catch(&jl); status = sc->cmd.bi->fn(sc->argc, sc->argv); } else { - unwind_restore(&u); status = exitstatus ? exitstatus : 2; } exc_handler = prev; @@ -1455,7 +1464,8 @@ static int eval_pipe(node *n, unsigned flags, strbuf *cap) { * every stage is a process, the last included: a stage running inside * the shell could not be stopped with the rest of the job. */ - job *jb = !cap && jc_active() && !job_launching ? job_begin(n, true) : nullptr; + job *jb = + !cap && jc_active() && !job_launching ? job_begin(n, true) : nullptr; varscope vs; scope_push(&vs, true, false); bool any_deferred = false; @@ -1488,11 +1498,9 @@ static int eval_pipe(node *n, unsigned flags, strbuf *cap) { */ jmploc jl; jmploc *prev = exc_handler; - unwind u; volatile bool raised = false; - unwind_save(&u); if (setjmp(jl.buf) == 0) { - exc_handler = &jl; + exc_catch(&jl); bool last_started = false; if (lc->kind == N_SIMPLE) { simple_prep(lc, &last->sc); @@ -1526,8 +1534,15 @@ static int eval_pipe(node *n, unsigned flags, strbuf *cap) { redir_pop(false); } } else { - unwind_restore(&u); - raised = true; + /* + * A fatal error in the stage ends the stage, not the script: ksh93 + * and dash both run on after `echo x | echo "${unset?}"`. Only + * `exit` in the last stage ends the shell. + */ + if (exc_code == EXC_EXIT) + raised = true; + else + last->status = exitstatus; } exc_handler = prev; @@ -1792,22 +1807,40 @@ static int eval_loop(node *n, unsigned flags) { static int eval_for(node *n, unsigned flags) { int count; char **words; + shparam held = {0, nullptr, nullptr}; if (n->u.for_.has_in) { words = expand_words(n->u.for_.words, &count); } else { - count = shellparam.n; + /* A `set --` in the body replaces the parameters, so the words this + * loop iterates over need a reference of their own. */ + held = shellparam; + params_hold(&held); + count = held.n; words = arena_alloc(&scratch, ((size_t)count + 1) * sizeof *words); - memcpy(words, shellparam.p, (size_t)count * sizeof *words); + memcpy(words, held.p, (size_t)count * sizeof *words); } - int status = 0; - loop_depth++; - for (int i = 0; i < count; i++) { - var_set(n->u.for_.var, words[i], 0); - status = eval_node(n->u.for_.body, flags & EV_TESTED); - if (loop_should_stop()) - break; + volatile int status = 0; + jmploc jl; + jmploc *prev = exc_handler; + volatile bool raised = false; + if (setjmp(jl.buf) == 0) { + exc_catch(&jl); + loop_depth++; + for (int i = 0; i < count; i++) { + var_set(n->u.for_.var, words[i], 0); + status = eval_node(n->u.for_.body, flags & EV_TESTED); + if (loop_should_stop()) + break; + } + loop_depth--; + } else { + loop_depth--; + raised = true; } - loop_depth--; + exc_handler = prev; + params_free(&held); + if (raised) + exc_raise(exc_code); return status; } @@ -2020,12 +2053,10 @@ int run_input(unsigned flags) { arena_marker mark = arena_mark(&scratch); jmploc jl; jmploc *prev = exc_handler; - unwind u; int saved_loop = loop_depth; bool recover = ((flags & RI_TOP) && opt.interactive) || (flags & RI_RECOVER); - unwind_save(&u); if (stack_low()) { arena_unref(pa); sh_error("eval nested too deeply"); @@ -2042,7 +2073,7 @@ int run_input(unsigned flags) { arena_unref(pa); exc_raise(exc_code); } - exc_handler = &jl; + exc_catch(&jl); } for (;;) { if (recover) { @@ -2053,7 +2084,6 @@ int run_input(unsigned flags) { arena_unref(pa); exc_raise(EXC_EXIT); } - unwind_restore(&u); arena_release(&scratch, mark); parser_reset(); input_discard_line(); @@ -2062,7 +2092,7 @@ int run_input(unsigned flags) { dot_depth = 0; job_launching = nullptr; } - exc_handler = &jl; + exc_catch(&jl); if (flags & RI_TOP) prompt_state = 1; } -- cgit