diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-27 17:26:45 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-27 17:26:45 -0600 |
| commit | 90e25207c41010a5307beff7fd7a5a9e4be3653e (patch) | |
| tree | f257cce6d24590e5e41af525dcb85931ab39002d /src/input.c | |
| parent | e8f3a27f981f2d13cf6ccc9f9a08bebe3d44ac60 (diff) | |
| download | zash-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/input.c')
| -rw-r--r-- | src/input.c | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/src/input.c b/src/input.c index 3bad009..5ae722c 100644 --- a/src/input.c +++ b/src/input.c @@ -81,18 +81,40 @@ void input_push_string(const char *s, size_t len) { } /** + * @brief Is this alias's text still being read? + * @param a The alias. + * @return true while some input source is reading it. + */ +bool input_alias_active(const alias *a) { + for (const input_src *in = input; in; in = in->prev) { + if (in->alias == a) + return true; + } + return false; +} + +/** * @brief Push the text of an alias as input. * * The input removes itself when it runs out (see input_fill()), so the - * lexer carries straight on with whatever followed the alias name. + * lexer carries straight on with whatever followed the alias name. The + * text is copied: the alias itself may be redefined or removed while it + * is being read. * - * @param a The alias. Marked in use until its text has been read. + * @param a The alias. */ void input_push_alias(alias *a) { - input_push_string(a->value, strlen(a->value)); - input->autopop = true; - input->alias = a; - a->in_use = true; + size_t n = strlen(a->value); + input_push_string(a->value, n); + input_src *in = input; + /* The input owns a copy: this very command line may redefine or remove + * the alias while its text is still being read. */ + in->buf = xmalloc(n + 1); + memcpy(in->buf, a->value, n + 1); + in->p = in->buf; + in->end = in->buf + n; + in->autopop = true; + in->alias = a; } /** @@ -115,9 +137,7 @@ void input_push_back(const char *s, size_t len) { void input_pop(void) { input_src *in = input; input = in->prev; - if (in->alias) - in->alias->in_use = false; - else if (in->autopop && input) + if (!in->alias && in->autopop && input) input->lineno = in->lineno; if (in->fd > 2) /* a script or dot file we opened */ close(in->fd); @@ -142,8 +162,10 @@ int input_fill(void) { if (in->autopop) { /* Alias or pushed-back text has run out: carry on underneath. */ if (in->alias) { - size_t n = strlen(in->alias->value); - char last = n ? in->alias->value[n - 1] : '\0'; + /* POSIX: a value ending in a blank lets the next word be + * expanded too. The text read decides, not the alias it came + * from, which may have been replaced by now. */ + char last = in->end > in->buf ? in->end[-1] : '\0'; alias_ended_blank = last == ' ' || last == '\t'; } input_pop(); @@ -169,6 +191,13 @@ int input_fill(void) { break; trap_poll(); } + if (n < 0) { + /* A read error is not the end of the input. At the outermost level + * nothing can recover from it, so the shell fails. */ + if (!in->prev) + sh_error("cannot read input: %s", strerror(errno)); + sh_warn("cannot read input: %s", strerror(errno)); + } if (n <= 0) { in->eof = true; return PEOF; |
