diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 21:58:27 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 21:58:27 -0600 |
| commit | eb8f9331cd2f0c240ba1323ef136b31611215f29 (patch) | |
| tree | fd242d98a3933fdb991d714c560b7274536ddbcd /src/exec.c | |
| parent | b97207f7e5dd3d8d147da525ede1ba3609f5c983 (diff) | |
| download | zash-eb8f9331cd2f0c240ba1323ef136b31611215f29.tar.gz | |
Fix redirection, parser, arithmetic, environment, and exec bugs
- Fail cleanly in save_fd() when sys_dupfd() fails with errno != EBADF,
check moved < 0 in plan_redirs()/plan_apply(), and reject duplicating
from closed descriptors in plan_redirs().
- Accept uppercase digits in parse_based() for bases <= 36 while keeping
ksh case-sensitive digits for bases 37..64.
- Save and restore in_cond, alias_ended_blank, and here_head/here_tail in
parse_cmdsub() and parse_substring(); parse inner double quotes inside
"${x:-...}" via read_double_quoted(); reject 'for x; in ...'.
- Deduplicate repeated temporary assignments in var_environ_with().
- Use access(X_OK) in path_search(), expand PS4 in xtrace() with recursion
guarded, report stdout write errors in run_pure()/eval_subshell(), and
restore loop/dot/func depth and skip_kind on eval_pipe() error unwind.
Verified locally on Linux (make, make zos-lint, make check: 128/129 pass).
Diffstat (limited to 'src/exec.c')
| -rw-r--r-- | src/exec.c | 42 |
1 files changed, 34 insertions, 8 deletions
@@ -358,7 +358,7 @@ char *path_search(const char *name, const char *path, bool executable) { } struct stat st; if (stat(full, &st) == 0 && S_ISREG(st.st_mode) && - (!executable || (st.st_mode & 0111))) + (!executable || access(full, X_OK) == 0)) return full; if (!colon) return nullptr; @@ -744,11 +744,26 @@ typedef struct simple_cmd { } simple_cmd; /** + * @brief Expand PS4 for `set -x` without recursively tracing the expansion. + */ +static const char *xtrace_prefix(void) { + const char *ps4 = var_get("PS4"); + if (!ps4) + return "+ "; + if (!strpbrk(ps4, "$`\\")) + return ps4; + bool saved = opt.xtrace; + opt.xtrace = false; + const char *out = expand_quietly(ps4, false); + opt.xtrace = saved; + return out; +} + +/** * @brief Print a command for `set -x`. */ static void xtrace(const simple_cmd *sc) { - const char *ps4 = var_get("PS4"); - out_puts(&out2, ps4 ? ps4 : "+ "); + out_puts(&out2, xtrace_prefix()); for (int i = 0; i < sc->argc; i++) { if (i) out_putc(&out2, ' '); @@ -1217,7 +1232,8 @@ static int run_pure(simple_cmd *sc, output *o) { jmploc *prev = exc_handler; int status; - out1 = *o; + if (o != &out1) + out1 = *o; if (setjmp(jl.buf) == 0) { exc_catch(&jl); status = sc->cmd.bi->fn(sc->argc, sc->argv); @@ -1225,6 +1241,8 @@ static int run_pure(simple_cmd *sc, output *o) { status = exitstatus ? exitstatus : 2; } exc_handler = prev; + if (o == &out1) + return finish_builtin(status); out_flush(&out1); *o = out1; out1 = saved; @@ -1502,6 +1520,9 @@ static int eval_pipe(node *n, unsigned flags, strbuf *cap) { */ jmploc jl; jmploc *prev = exc_handler; + int saved_loop = loop_depth; + int saved_dot = dot_depth; + int saved_func = func_depth; volatile bool raised = false; if (setjmp(jl.buf) == 0) { exc_catch(&jl); @@ -1543,10 +1564,15 @@ static int eval_pipe(node *n, unsigned flags, strbuf *cap) { * and dash both run on after `echo x | echo "${unset?}"`. Only * `exit` in the last stage ends the shell. */ - if (exc_code == EXC_EXIT) + if (exc_code == EXC_EXIT) { raised = true; - else + } else { last->status = exitstatus; + loop_depth = saved_loop; + dot_depth = saved_dot; + func_depth = saved_func; + skip_kind = SKIP_NONE; + } } exc_handler = prev; @@ -1922,6 +1948,7 @@ static int eval_subshell(node *n, unsigned flags) { if (ok && (sc.cmd.bi->flags & BI_CD)) { int status; builtin_cd_isolated(sc.argc, sc.argv, &status); + status = finish_builtin(status); scope_pop(); return status; } @@ -2055,8 +2082,7 @@ int eval_node(node *n, unsigned flags) { cur_lineno = n->lineno; if (opt.xtrace) { char *text = node_text(n); - out_printf(&out2, "%s%s\n", var_get("PS4") ? var_get("PS4") : "+ ", - text); + out_printf(&out2, "%s%s\n", xtrace_prefix(), text); out_flush(&out2); free(text); } |
