summaryrefslogtreecommitdiffhomepage
path: root/src/exec.c
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-29 10:09:25 -0600
committerGitHub <noreply@github.com>2026-11-29 10:09:25 -0600
commit99474ce7c2c94f0dde4b244b45b305ebc06b03b5 (patch)
treea140e848fef1f7524563d673a6d95a901160b81c /src/exec.c
parent3a8fab2c0e0fdabe8063ee86df7d2e17020ea9b9 (diff)
parent3ded9a80de9a487bcae31c695740d82d1a0e13fd (diff)
downloadzash-99474ce7c2c94f0dde4b244b45b305ebc06b03b5.tar.gz
Merge pull request #49 from skurtz97/fix/audit-2026-11-28
Fix code audit findings across z/OS layer, parser, redirections, and builtins
Diffstat (limited to 'src/exec.c')
-rw-r--r--src/exec.c42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/exec.c b/src/exec.c
index 0c7cede..2bbbcad 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -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);
}