summaryrefslogtreecommitdiffhomepage
path: root/src/parser.c
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-27 17:26:45 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-27 17:26:45 -0600
commit90e25207c41010a5307beff7fd7a5a9e4be3653e (patch)
treef257cce6d24590e5e41af525dcb85931ab39002d /src/parser.c
parente8f3a27f981f2d13cf6ccc9f9a08bebe3d44ac60 (diff)
downloadzash-90e25207c41010a5307beff7fd7a5a9e4be3653e.tar.gz
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.
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/parser.c b/src/parser.c
index 0677b37..228acb2 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -12,6 +12,8 @@
#include "charset.h"
#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
#include <stdlib.h>
#include <string.h>
@@ -279,7 +281,12 @@ static param *new_param(const char *name, size_t nlen) {
pm->op = PS_NORMAL;
if (isdigit((unsigned char)name[0])) {
pm->type = PT_POS;
- pm->pos = atoi(name);
+ /* A number too large for any parameter list is simply unset; INT_MAX
+ * is not a valid index, so the range check in param_value rejects it.
+ */
+ errno = 0;
+ long v = strtol(name, nullptr, 10);
+ pm->pos = errno == 0 && v <= INT_MAX ? (int)v : INT_MAX;
} else if (is_special_param((unsigned char)name[0])) {
pm->type = PT_SPECIAL;
pm->special = name[0];
@@ -934,6 +941,11 @@ static void lex_token(void) {
tok.kw = KW_NONE;
switch (c) {
case PEOF:
+ /* A here-document with no body before the end of input has an empty
+ * body, not the delimiter as its body. Read it so the delimiter
+ * word never stands in for one. */
+ if (here_head)
+ read_heredocs();
tok.t = T_EOF;
return;
case '\n':
@@ -1029,7 +1041,7 @@ static bool alias_expand(void) {
if (!alias_count || !tok.bare)
return false;
alias *a = alias_find(tok.word->parts->u.lit.s);
- if (!a || a->in_use)
+ if (!a || input_alias_active(a))
return false;
tok_pushed = false;
input_push_alias(a);
@@ -2118,7 +2130,7 @@ typedef struct parser_state {
} parser_state;
static_assert(sizeof(parser_state) <= PARSER_STATE_SIZE,
- "PARSER_STATE_SIZE in parser.h is too small");
+ "PARSER_STATE_SIZE in parser.h is too small");
/**
* @brief Save the parser's state and reset it.