summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 21:58:27 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 21:58:27 -0600
commiteb8f9331cd2f0c240ba1323ef136b31611215f29 (patch)
treefd242d98a3933fdb991d714c560b7274536ddbcd /src
parentb97207f7e5dd3d8d147da525ede1ba3609f5c983 (diff)
downloadzash-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')
-rw-r--r--src/arith.c5
-rw-r--r--src/exec.c42
-rw-r--r--src/parser.c37
-rw-r--r--src/redir.c45
-rw-r--r--src/var.c10
5 files changed, 116 insertions, 23 deletions
diff --git a/src/arith.c b/src/arith.c
index 5097134..e4f9a54 100644
--- a/src/arith.c
+++ b/src/arith.c
@@ -282,7 +282,10 @@ static intmax_t parse_based(arith *a, const char *p, const char *hash) {
uintmax_t v = 0;
const char *q = hash + 1;
for (; *q; q++) {
- const char *d = strchr(digits, *q);
+ int ch = (unsigned char)*q;
+ if (base <= 36)
+ ch = tolower(ch);
+ const char *d = strchr(digits, ch);
if (!d)
break;
uintmax_t dv = (uintmax_t)(d - digits);
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);
}
diff --git a/src/parser.c b/src/parser.c
index 98d4c0b..aa3b9d4 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -414,12 +414,24 @@ static void parse_braced_param(pbuild *b, bool q, bool dq) {
static node *parse_cmdsub(void) {
token saved = tok;
bool saved_pushed = tok_pushed;
+ bool saved_cond = in_cond;
+ bool saved_alias = alias_ended_blank;
+ heredoc *saved_head = here_head;
+ heredoc **saved_tail = here_tail == &here_head ? nullptr : here_tail;
tok_pushed = false;
+ in_cond = false;
+ alias_ended_blank = false;
+ here_head = nullptr;
+ here_tail = &here_head;
node *n = parse_clist();
if (tok.t != T_RP || !tok_pushed)
syntax_error("missing ')'");
tok = saved;
tok_pushed = saved_pushed;
+ in_cond = saved_cond;
+ alias_ended_blank = saved_alias;
+ here_head = saved_head;
+ here_tail = saved_tail ? saved_tail : &here_head;
return n;
}
@@ -730,8 +742,6 @@ static void read_parts(pbuild *b, lex_mode mode, bool litq) {
return;
if (mode == M_HERE || mode == M_ARITH)
break;
- if (mode == M_BRACE_DQ && !pattern)
- continue; /* "${x:-"y"}": the inner quotes are redundant */
read_double_quoted(b);
continue;
case '$':
@@ -1403,10 +1413,13 @@ static node *parse_for(bool menu) {
if (tok.t != T_WORD || !tok.word->plain || !is_name(tok.word->plain))
syntax_error("bad for loop variable");
n->u.for_.var = tok.word->plain;
- if (peek() == T_SEMI)
+ bool had_semi = false;
+ if (peek() == T_SEMI) {
+ had_semi = true;
consume();
+ }
skip_newlines();
- if (accept_kw(KW_IN)) {
+ if (!had_semi && accept_kw(KW_IN)) {
n->u.for_.has_in = true;
word **tail = &n->u.for_.words;
while (peek() == T_WORD) {
@@ -2089,7 +2102,15 @@ node *parse_command(arena *a, bool *eof) {
static node *parse_substring(const char *s, size_t len) {
token saved = tok;
bool saved_pushed = tok_pushed;
+ bool saved_cond = in_cond;
+ bool saved_alias = alias_ended_blank;
+ heredoc *saved_head = here_head;
+ heredoc **saved_tail = here_tail == &here_head ? nullptr : here_tail;
tok_pushed = false;
+ in_cond = false;
+ alias_ended_blank = false;
+ here_head = nullptr;
+ here_tail = &here_head;
input_push_string(s, len);
node *n = parse_clist();
if (peek() != T_EOF)
@@ -2097,6 +2118,10 @@ static node *parse_substring(const char *s, size_t len) {
input_pop();
tok = saved;
tok_pushed = saved_pushed;
+ in_cond = saved_cond;
+ alias_ended_blank = saved_alias;
+ here_head = saved_head;
+ here_tail = saved_tail ? saved_tail : &here_head;
return n;
}
@@ -2152,8 +2177,8 @@ static_assert(sizeof(parser_state) <= PARSER_STATE_SIZE,
*/
void parser_save(void *state) {
parser_state *ps = state;
- *ps = (parser_state){pa, tok, tok_pushed, here_head, here_tail, in_cond,
- alias_ended_blank};
+ *ps = (parser_state){pa, tok, tok_pushed, here_head,
+ here_tail, in_cond, alias_ended_blank};
if (here_tail == &here_head)
ps->here_tail = nullptr;
parser_reset();
diff --git a/src/redir.c b/src/redir.c
index edfc212..8ff14e0 100644
--- a/src/redir.c
+++ b/src/redir.c
@@ -269,14 +269,23 @@ static bool is_saved(const redirsave *sv, int fd) {
* @param sv The save set.
* @param fd The descriptor about to be changed.
* @param was_closed It is not open now, so "putting it back" means closing.
+ * @return false if the descriptor could not be duplicated (e.g. EMFILE).
*/
-static void save_fd(redirsave *sv, int fd, bool was_closed) {
+static bool save_fd(redirsave *sv, int fd, bool was_closed) {
if (is_saved(sv, fd))
- return;
- int copy = was_closed ? -1 : sys_dupfd(fd, PRIVATE_FD);
+ return true;
+ int copy = -1;
+ if (!was_closed) {
+ copy = sys_dupfd(fd, PRIVATE_FD);
+ if (copy < 0 && errno != EBADF) {
+ sh_warn("%d: cannot save descriptor: %s", fd, strerror(errno));
+ return false;
+ }
+ }
sv->e[sv->n].fd = fd;
sv->e[sv->n].copy = copy;
sv->n++;
+ return true;
}
/**
@@ -312,12 +321,16 @@ bool redir_push(const redir *list, redirsave *sv) {
if (k == RK_FD && src == r->fd) {
/* open() returned the very descriptor wanted, so it was
* closed before. It only needs close-on-exec cleared. */
- save_fd(sv, r->fd, true);
+ (void)save_fd(sv, r->fd, true);
fcntl(src, F_SETFD, 0);
note_fd(r->fd, true);
continue;
}
- save_fd(sv, r->fd, false);
+ if (!save_fd(sv, r->fd, false)) {
+ if (k == RK_FD)
+ close(src);
+ goto fail;
+ }
if (k == RK_CLOSE) {
close(r->fd);
note_fd(r->fd, false);
@@ -347,7 +360,11 @@ fail:
*/
void redir_push_fd(redirsave *sv, int fd, int src) {
begin_save(sv, 1);
- save_fd(sv, fd, false);
+ if (!save_fd(sv, fd, false)) {
+ close(src);
+ redir_pop(false);
+ sh_error("%d: cannot redirect descriptor", fd);
+ }
dup2(src, fd);
close(src);
note_fd(fd, true);
@@ -430,6 +447,13 @@ bool plan_redirs(fdplan *p, const redir *list) {
/* Keep plan sources distinct from the slots they fill, so
* "inherit descriptor n" never means a temporary. */
int moved = sys_dupfd(src, PRIVATE_FD);
+ if (moved < 0) {
+ int err = errno;
+ close(src);
+ sh_warn("%d: cannot duplicate descriptor: %s", r->fd,
+ strerror(err));
+ return false;
+ }
close(src);
src = moved;
}
@@ -437,6 +461,13 @@ bool plan_redirs(fdplan *p, const redir *list) {
p->map[r->fd] = src;
break;
case RK_DUP:
+ if (src == r->fd)
+ break;
+ if (p->map[src] == FD_CLOSED ||
+ (p->map[src] == src && fcntl(src, F_GETFD) < 0)) {
+ sh_warn("%d: bad file descriptor", src);
+ return false;
+ }
p->map[r->fd] = p->map[src];
break;
case RK_CLOSE:
@@ -470,6 +501,8 @@ void plan_apply(fdplan *p) {
if (src == i || src < 0 || src >= SYS_NFD)
continue;
int moved = sys_dupfd(src, PRIVATE_FD);
+ if (moved < 0)
+ continue;
for (int j = i; j < SYS_NFD; j++) {
if (p->map[j] == src)
p->map[j] = moved;
diff --git a/src/var.c b/src/var.c
index 810ebd9..df8bbba 100644
--- a/src/var.c
+++ b/src/var.c
@@ -416,8 +416,14 @@ char **var_environ_with(char **extra, int nextra) {
n++;
char **env = arena_alloc(&scratch, (n + (size_t)nextra + 1) * sizeof *env);
size_t k = 0;
- for (int i = 0; i < nextra; i++)
- env[k++] = extra[i];
+ for (int i = 0; i < nextra; i++) {
+ size_t nlen = (size_t)(strchr(extra[i], '=') - extra[i]) + 1;
+ bool shadowed = false;
+ for (int j = i + 1; j < nextra && !shadowed; j++)
+ shadowed = !strncmp(extra[i], extra[j], nlen);
+ if (!shadowed)
+ env[k++] = extra[i];
+ }
for (size_t i = 0; i < n; i++) {
size_t nlen = (size_t)(strchr(base[i], '=') - base[i]) + 1;
bool shadowed = false;