summaryrefslogtreecommitdiffhomepage
path: root/src/exec.c
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 14:16:41 -0600
committerGitHub <noreply@github.com>2026-11-28 14:16:41 -0600
commit95d31d2d7d5fd7de81d6d5e2caedcdc369ca0fcf (patch)
tree4959b8d43268945543be4061b8fccd95d04592b1 /src/exec.c
parent625908ab56c7c554506d61c83c7001bca19a05f6 (diff)
parent6fe2aff8d6611bf45704ba86120efa23950bfa80 (diff)
downloadzash-95d31d2d7d5fd7de81d6d5e2caedcdc369ca0fcf.tar.gz
Merge pull request #46 from skurtz97/fix/review-2026-11-28
Fixes from the 2026-11-28 code review
Diffstat (limited to 'src/exec.c')
-rw-r--r--src/exec.c42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/exec.c b/src/exec.c
index 1b04b25..e7365ba 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -957,7 +957,7 @@ static int run_external(simple_cmd *sc, unsigned flags, int in, int out,
plan_close(&plan);
return 2;
}
- if ((flags & EV_EXIT) && !async && !trap_has_exit()) {
+ if ((flags & EV_EXIT) && !async && !trap_after_last()) {
/* Nothing comes after this command: become it. */
plan_apply(&plan);
exec_path(sc->cmd.path, sc->argv, envp);
@@ -1248,7 +1248,12 @@ static bool subshell_in_process(const node *n) {
return false;
cmdinfo ci;
find_command(b->u.simple.args->plain, &ci, 0);
- return ci.kind == C_BUILTIN && (ci.bi->flags & BI_PURE);
+ if (ci.kind != C_BUILTIN)
+ return false;
+ if (ci.bi->flags & BI_PURE)
+ return true;
+ /* cd / chdir: can run in-process if the cwd is saved and restored */
+ return !strcmp(ci.bi->name, "cd") || !strcmp(ci.bi->name, "chdir");
}
/* ---- pipelines ------------------------------------------------------- */
@@ -1367,7 +1372,7 @@ static void run_deferred(stage *st, int nc, int i, bool stream) {
s->out = -1;
for (int j = 0; j < nc; j++)
close_stage(&st[j]); /* or we would never see EPIPE */
- signal(SIGPIPE, SIG_DFL);
+ trap_sig_default(SIGPIPE);
output direct = {o.buf, o.len, o.len, out, 0};
out_flush(&direct);
_exit(0);
@@ -1903,7 +1908,7 @@ static int eval_case(node *n, unsigned flags) {
* the shell was about to exit anyway.
*/
static int eval_subshell(node *n, unsigned flags) {
- if (!(flags & EV_EXIT) || trap_has_exit()) {
+ if (!(flags & EV_EXIT) || trap_after_last()) {
if (!job_launching && !opt.errexit && subshell_in_process(n)) {
simple_cmd sc;
varscope vs;
@@ -1915,6 +1920,23 @@ static int eval_subshell(node *n, unsigned flags) {
scope_pop();
return status;
}
+ /* cd / chdir: save cwd, run in-process, restore */
+ if (ok && sc.cmd.kind == C_BUILTIN &&
+ (!strcmp(sc.cmd.bi->name, "cd") ||
+ !strcmp(sc.cmd.bi->name, "chdir"))) {
+ int cwd_fd = open(".", O_RDONLY | O_CLOEXEC);
+ if (cwd_fd < 0) {
+ scope_pop();
+ return 1;
+ }
+ int status = sc.cmd.bi->fn(sc.argc, sc.argv);
+ if (fchdir(cwd_fd) < 0)
+ sh_error("cannot restore working directory");
+ close(cwd_fd);
+ scope_pop();
+ curdir_invalidate();
+ return status;
+ }
scope_pop();
if (!ok)
return exitstatus;
@@ -2088,6 +2110,7 @@ int eval_node(node *n, unsigned flags) {
* @return The status of the last command.
*/
int run_input(unsigned flags) {
+ alignas(max_align_t) char pstate[PARSER_STATE_SIZE];
arena *volatile pa = arena_new();
arena_marker mark = arena_mark(&scratch);
jmploc jl;
@@ -2100,6 +2123,14 @@ int run_input(unsigned flags) {
arena_unref(pa);
sh_error("eval nested too deeply");
}
+ /*
+ * A trap, or the vi `v` command, runs eval_string() while the outer
+ * parse is half way through a command; its parse_command() moves the
+ * static arena pointer and parser_reset() drops the pending tokens.
+ * Every way out of this function restores what the caller was in the
+ * middle of, so no nested eval can leave it parsing a freed arena.
+ */
+ parser_save(pstate);
if (!recover) {
/*
* An error or exit unwinding through here drops this level's
@@ -2110,6 +2141,7 @@ int run_input(unsigned flags) {
if (setjmp(jl.buf)) {
exc_handler = prev;
arena_unref(pa);
+ parser_restore(pstate);
exc_raise(exc_code);
}
exc_catch(&jl);
@@ -2121,6 +2153,7 @@ int run_input(unsigned flags) {
if (exc_code == EXC_EXIT) {
exc_handler = prev;
arena_unref(pa);
+ parser_restore(pstate);
exc_raise(EXC_EXIT);
}
arena_release(&scratch, mark);
@@ -2160,6 +2193,7 @@ int run_input(unsigned flags) {
}
exc_handler = prev;
arena_unref(pa);
+ parser_restore(pstate);
return exitstatus;
}