diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-29 10:09:25 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-29 10:09:25 -0600 |
| commit | 99474ce7c2c94f0dde4b244b45b305ebc06b03b5 (patch) | |
| tree | a140e848fef1f7524563d673a6d95a901160b81c | |
| parent | 3a8fab2c0e0fdabe8063ee86df7d2e17020ea9b9 (diff) | |
| parent | 3ded9a80de9a487bcae31c695740d82d1a0e13fd (diff) | |
| download | zash-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
| -rw-r--r-- | .gitattributes | 1 | ||||
| -rw-r--r-- | C-PROGRAMMING-STANDARDS.md | 6 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | src/alias.c | 32 | ||||
| -rw-r--r-- | src/arith.c | 5 | ||||
| -rw-r--r-- | src/builtin.c | 36 | ||||
| -rw-r--r-- | src/builtin_ext.c | 22 | ||||
| -rw-r--r-- | src/complete.c | 6 | ||||
| -rw-r--r-- | src/edit_builtin.c | 69 | ||||
| -rw-r--r-- | src/exec.c | 42 | ||||
| -rw-r--r-- | src/history.c | 2 | ||||
| -rw-r--r-- | src/jobs.c | 20 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rw-r--r-- | src/parser.c | 37 | ||||
| -rw-r--r-- | src/printf.c | 33 | ||||
| -rw-r--r-- | src/redir.c | 45 | ||||
| -rw-r--r-- | src/sys_posix.c | 13 | ||||
| -rw-r--r-- | src/sys_zos.c | 20 | ||||
| -rw-r--r-- | src/test.c | 61 | ||||
| -rw-r--r-- | src/trap.c | 16 | ||||
| -rw-r--r-- | src/var.c | 10 | ||||
| -rw-r--r-- | tests/zash/audit-fixes.exp | 39 | ||||
| -rw-r--r-- | tests/zash/audit-fixes.sh | 83 |
23 files changed, 494 insertions, 108 deletions
diff --git a/.gitattributes b/.gitattributes index e1f0af5..2b0be7a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -15,6 +15,7 @@ formal/zash-spec.typ zos-working-tree-encoding=UTF-8 formal/zash_spec/ZashSpec.lean zos-working-tree-encoding=UTF-8 formal/zash_spec/ZashSpec/Patterns.lean zos-working-tree-encoding=UTF-8 notes/investigation.md zos-working-tree-encoding=UTF-8 +notes/review-2026-11-27.md zos-working-tree-encoding=UTF-8 formal/zash-spec.pdf binary -zos-working-tree-encoding packaging/zash.ico binary -zos-working-tree-encoding tests/posix/printf-ext.exp binary -zos-working-tree-encoding diff --git a/C-PROGRAMMING-STANDARDS.md b/C-PROGRAMMING-STANDARDS.md index 6196875..ed23daf 100644 --- a/C-PROGRAMMING-STANDARDS.md +++ b/C-PROGRAMMING-STANDARDS.md @@ -142,8 +142,8 @@ Formatting is strictly enforced via `.clang-format` based on LLVM style: ### 5.1 Dual-Encoding Awareness (ASCII and EBCDIC) Projects build in ASCII mode (`-fzos-le-char-mode=ascii`, CCSID 819 / ISO8859-1) or native EBCDIC mode (CCSID 1047): -* No in-band control markers: Never use byte values in the `0x80`–`0xFF` range as internal state flags; in IBM-1047, `0x81` represents the letter `'a'`. -* Non-contiguous alphabet safety: In EBCDIC, letters `a`–`z` and `A`–`Z` are not contiguous. Never perform pointer or integer arithmetic assuming sequential alphabetic values. Character classification must always use standard `<ctype.h>` functions (`isalpha`, `isdigit`, `isspace`). +* No in-band control markers: Never use byte values in the `0x80`-`0xFF` range as internal state flags; in IBM-1047, `0x81` represents the letter `'a'`. +* Non-contiguous alphabet safety: In EBCDIC, letters `a`-`z` and `A`-`Z` are not contiguous. Never perform pointer or integer arithmetic assuming sequential alphabetic values. Character classification must always use standard `<ctype.h>` functions (`isalpha`, `isdigit`, `isspace`). * Character literals: Always write character literals (`'a'`, `'\n'`), never hexadecimal ASCII numeric literals. * Control codes: Where terminal or protocol escapes are required, abstract the values (e.g., via `charset.h`) so appropriate ASCII (`0x1B`) or EBCDIC sequences are selected at compile time. @@ -151,7 +151,7 @@ Projects build in ASCII mode (`-fzos-le-char-mode=ascii`, CCSID 819 / ISO8859-1) * Tagging: On z/OS UNIX, source code and text files are tagged ISO8859-1 (`chtag -t -c ISO8859-1`). * Runtime Conversion: Use `fcntl(fd, F_CONTROL_CVT, ...)` to manage programmatic conversion. * ASCII Build Tagging Rule: In ASCII builds, newly created text files, pipes, and inherited stdout must be explicitly tagged IBM-1047 with programmatic conversion enabled so standard z/OS tools read them correctly. -* zopen conventions: Wherever possible, character conversion follows the zopen community's guidance — a file created in ASCII mode is tagged ISO8859-1 (CCSID 819) at open time via `setccsid()`/`__fchattr()`, the call is double-guarded with `#ifdef __MVS__` and `#if (__CHARSET_LIB == 1)`, and the same tagging is applied from the shell with `chtag -tc819`. See [zopen community — C pipe, C open do not tag newly created file descriptors](https://zopen.community/Guides/CommonSolutions#c-pipe-c-open-do-not-tag-newly-created-file-descriptors). +* zopen conventions: Wherever possible, character conversion follows the zopen community's guidance -- a file created in ASCII mode is tagged ISO8859-1 (CCSID 819) at open time via `setccsid()`/`__fchattr()`, the call is double-guarded with `#ifdef __MVS__` and `#if (__CHARSET_LIB == 1)`, and the same tagging is applied from the shell with `chtag -tc819`. See [zopen community -- C pipe, C open do not tag newly created file descriptors](https://zopen.community/Guides/CommonSolutions#c-pipe-c-open-do-not-tag-newly-created-file-descriptors). ### 5.3 System Data Areas and Control Blocks * Reading MVS Control Blocks: @@ -376,7 +376,7 @@ common area, which had 255 MB free. ### zopen / ASCII builds: avoid double pipe conversion An ASCII build tags pipes IBM-1047 (EBCDIC) so that EBCDIC readers see -EBCDIC. Between two ASCII programs that costs two conversions per byte — +EBCDIC. Between two ASCII programs that costs two conversions per byte -- write through the tag to 1047, then read through it back to ASCII. `set +o pipetag` avoids both conversions when no EBCDIC tool is in the pipeline. The setting can go in `.profile` or a per-script `set +o diff --git a/src/alias.c b/src/alias.c index da40661..6a88f9e 100644 --- a/src/alias.c +++ b/src/alias.c @@ -192,16 +192,30 @@ int builtin_alias(int argc, char **argv) { * @return 0, or 1 if a name was not an alias. */ int builtin_unalias(int argc, char **argv) { - int status = 0; - for (int i = 1; i < argc; i++) { + bool all = false; + int i = 1; + for (; i < argc && argv[i][0] == '-' && argv[i][1]; i++) { + if (!strcmp(argv[i], "--")) { + i++; + break; + } if (!strcmp(argv[i], "-a")) { - for (size_t b = 0; b < ATAB_SIZE; b++) { - while (atab[b]) - alias_remove(atab[b]->name); - } - } else if (!strcmp(argv[i], "--")) { - continue; - } else if (!alias_remove(argv[i])) { + all = true; + } else { + sh_warn("unalias: %s: bad option", argv[i]); + return 2; + } + } + if (all) { + for (size_t b = 0; b < ATAB_SIZE; b++) { + while (atab[b]) + alias_remove(atab[b]->name); + } + return 0; + } + int status = 0; + for (; i < argc; i++) { + if (!alias_remove(argv[i])) { sh_warn("unalias: %s: not found", argv[i]); status = 1; } 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/builtin.c b/src/builtin.c index e5ff809..9382a85 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -154,12 +154,15 @@ static int b_continue(int argc, char **argv) { * @brief The `eval` builtin: join the arguments with spaces and run the result. */ static int b_eval(int argc, char **argv) { - if (argc < 2) + int i = 1; + if (i < argc && !strcmp(argv[i], "--")) + i++; + if (i >= argc) return 0; strbuf sb; sb_init(&sb, &scratch); - for (int i = 1; i < argc; i++) { - if (i > 1) + for (int first = i; i < argc; i++) { + if (i > first) sb_putc(&sb, ' '); sb_puts(&sb, argv[i]); } @@ -186,6 +189,8 @@ static int b_dot(int argc, char **argv) { sh_error(".: %s: not found", argv[1]); int high = sys_dupfd(fd, SYS_NFD); close(fd); + if (high < 0) + sh_error(".: %s: %s", argv[1], strerror(errno)); input_push_fd(high, false); dot_depth++; @@ -225,8 +230,15 @@ int builtin_exec(int argc, char **argv) { */ static int export_or_readonly(int argc, char **argv, unsigned flag) { int i = 1; - if (i < argc && !strcmp(argv[i], "-p")) - i++; + for (; i < argc && argv[i][0] == '-' && argv[i][1]; i++) { + if (!strcmp(argv[i], "--")) { + i++; + break; + } + if (!strcmp(argv[i], "-p")) + continue; + sh_error("%s: %s: bad option", argv[0], argv[i]); + } if (i >= argc) { var_print_all(flag, flag == V_EXPORT ? "export " : "readonly "); return 0; @@ -434,8 +446,8 @@ static int b_read(int argc, char **argv) { int ready = 1; if (!always_ready) ready = - ms > 0 ? poll(&pfd, 1, ms < INT_MAX ? (int)ms : INT_MAX) - : 0; + poll(&pfd, 1, + ms > 0 ? (ms < INT_MAX ? (int)ms : INT_MAX) : 0); if (ready == 0) return 128 + SIGALRM; if (ready < 0 && errno == EINTR) { @@ -838,7 +850,7 @@ void builtin_cd_isolated(int argc, char **argv, int *status) { int result; if (setjmp(jl.buf) == 0) { - exc_handler = &jl; + exc_catch(&jl); result = b_cd(argc, argv); } else { /* b_cd raised; the directory may still have moved. */ @@ -892,9 +904,9 @@ static int b_pwd(int argc, char **argv) { */ static bool is_keyword(const char *s) { static const char *const words[] = { - "if", "then", "else", "elif", "fi", "do", "done", - "case", "esac", "while", "until", "for", "in", "{", - "}", "!", "function", "[[", "]]", + "if", "then", "else", "elif", "fi", "do", "done", + "case", "esac", "while", "until", "for", "in", "{", + "}", "!", "function", "[[", "]]", "time", "select", }; for (size_t i = 0; i < ARRAY_LEN(words); i++) { if (!strcmp(s, words[i])) @@ -1110,6 +1122,8 @@ static int b_umask(int argc, char **argv) { symbolic = true; i++; } + if (i < argc && !strcmp(argv[i], "--")) + i++; mode_t mask = umask(0); umask(mask); if (i >= argc) { diff --git a/src/builtin_ext.c b/src/builtin_ext.c index 8983e5a..a1279c6 100644 --- a/src/builtin_ext.c +++ b/src/builtin_ext.c @@ -14,6 +14,7 @@ #include <stdlib.h> #include <string.h> #include <sys/resource.h> +#include <sys/stat.h> #include <unistd.h> #include "exec.h" @@ -55,7 +56,7 @@ int builtin_print(int argc, char **argv) { raw = stop = true; } else if (*p == 'u') { const char *num = p[1] ? p + 1 : i + 1 < argc ? argv[++i] : ""; - if (!parse_long(num, &fd) || fd < 0) { + if (!parse_long(num, &fd) || fd < 0 || fd >= SYS_NFD) { sh_warn("print: -u: bad file descriptor"); return 2; } @@ -144,10 +145,15 @@ int builtin_whence(int argc, char **argv) { continue; } const char *path = var_get("PATH"); - const char *full = - strchr(argv[i], '/') - ? (access(argv[i], X_OK) == 0 ? argv[i] : nullptr) - : path_search(argv[i], path ? path : "", true); + const char *full = nullptr; + if (strchr(argv[i], '/')) { + struct stat st; + if (stat(argv[i], &st) == 0 && S_ISREG(st.st_mode) && + access(argv[i], X_OK) == 0) + full = argv[i]; + } else { + full = path_search(argv[i], path ? path : "", true); + } if (full) out_printf(&out1, "%s\n", full); else @@ -379,8 +385,7 @@ int builtin_ulimit(int argc, char **argv) { for (size_t k = 0; k < ARRAY_LEN(limits); k++) { const limit *e = &limits[k]; if (e->unit) - out_printf(&out1, "%-20s (%s, -%c) ", e->name, e->unit, - e->opt); + out_printf(&out1, "%-20s (%s, -%c) ", e->name, e->unit, e->opt); else out_printf(&out1, "%-20s (-%c) ", e->name, e->opt); limit_print(e, hard && !soft); @@ -419,8 +424,7 @@ int builtin_ulimit(int argc, char **argv) { return 2; } /* n * scale may not wrap, and may not quietly mean "unlimited". */ - if (n > RLIM_INFINITY / l->scale || - n * l->scale == RLIM_INFINITY) { + if (n > RLIM_INFINITY / l->scale || n * l->scale == RLIM_INFINITY) { sh_warn("ulimit: %s: limit out of range", argv[i]); return 1; } diff --git a/src/complete.c b/src/complete.c index ea12197..d675180 100644 --- a/src/complete.c +++ b/src/complete.c @@ -190,9 +190,9 @@ static bool hlq_ok(const char *p, size_t len) { if (len == 0 || len > 8) return false; for (size_t i = 0; i < len; i++) { - char c = p[i]; - if (!((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '@' || - c == '#' || c == '$' || c == '-')) + unsigned char c = (unsigned char)p[i]; + if (!(isupper(c) || isdigit(c) || c == '@' || c == '#' || c == '$' || + c == '-')) return false; } return true; diff --git a/src/edit_builtin.c b/src/edit_builtin.c index bc66b5a..137d19a 100644 --- a/src/edit_builtin.c +++ b/src/edit_builtin.c @@ -499,26 +499,49 @@ static int bind_key(int k, const char *seq, size_t n) { } /** + * @brief Decode the character after `^` in a `bind` key specification. + * + * `^[` is Escape, `^?` is Delete, and `^@`..`^_` (letters case-insensitive) + * map to their control codes in the build's character set. + * + * @return The control byte, or -1 if `d` does not name one. + */ +static int ctrl_byte(unsigned char d) { + if (d == '[') + return CH_ESC; + if (d == '?') + return CH_DEL; + d = (unsigned char)toupper(d); +#if ZASH_ASCII + if (d >= '@' && d <= '_') + return (int)((unsigned)d & 0x1fu); + return -1; +#else + static const char letters[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"; + const char *at = strchr(letters, (char)d); + if (!at) + return -1; + return mvs_a2e[(size_t)(at - letters)]; +#endif +} + +/** * @brief Turn the key notation of a `bind` line into bytes. * * `^X` is a control key, `^[` is Escape and `^?` is Delete. Anything else - * stands for itself. The `& 0x3f` that makes `^A` into 0x01 also works on - * an EBCDIC letter, where 'A' is 0xC1. + * stands for itself. * - * @return 0, or -1 if the notation does not fit BIND_SEQ_MAX bytes. + * @return 0, or -1 if the notation is invalid or does not fit BIND_SEQ_MAX. */ static int bind_keys(const char *s, size_t len, char *out, size_t *outlen) { size_t n = 0; for (size_t i = 0; i < len; i++) { unsigned char c = (unsigned char)s[i]; if (c == '^' && i + 1 < len) { - unsigned char d = (unsigned char)s[++i]; - if (d == '[') - c = CH_ESC; - else if (d == '?') - c = CH_DEL; - else - c = (unsigned char)(d & 0x3f); + int cb = ctrl_byte((unsigned char)s[++i]); + if (cb < 0) + return -1; + c = (unsigned char)cb; } if (n == BIND_SEQ_MAX) return -1; @@ -572,8 +595,7 @@ int edit_bind(const char *spec) { void edit_bind_list(void) { for (size_t i = 0; i < sizeof bind_actions / sizeof bind_actions[0]; i++) - printf("%s\n", bind_actions[i].name); - fflush(stdout); + out_printf(&out1, "%s\n", bind_actions[i].name); } /* ---- reading keys ---------------------------------------------------- */ @@ -637,7 +659,8 @@ static int read_key_raw(void) { if (n < BIND_SEQ_MAX) seq[n++] = (char)c; if (isdigit(c)) { - args[np] = args[np] * 10 + (c - '0'); + if (args[np] < 100000) + args[np] = args[np] * 10 + (c - '0'); } else if (c == ';') { if (np < 1) np++; @@ -980,14 +1003,21 @@ static void edit_external(void) { ob_flush(); return; } + if (SYS_MVS) + sys_tmp_tag(fd); size_t off = 0; while (off < ed.len) { ssize_t w = write(fd, ed.buf + off, ed.len - off); + if (w < 0 && errno == EINTR) + continue; if (w <= 0) break; off += (size_t)w; } - bool wrote = off == ed.len && write(fd, "\n", 1) == 1; + ssize_t nl = -1; + while (off == ed.len && (nl = write(fd, "\n", 1)) < 0 && errno == EINTR) + ; + bool wrote = (off == ed.len) && (nl == 1); close(fd); const char *editor = var_get("VISUAL"); if (!editor || !*editor) @@ -1025,8 +1055,12 @@ static void edit_external(void) { text = xmalloc(256); size_t cap = 256; ssize_t r; - while (nread + 1 < cap && - (r = read(fd, text + nread, cap - nread - 1)) > 0) { + while (nread + 1 < cap) { + r = read(fd, text + nread, cap - nread - 1); + if (r < 0 && errno == EINTR) + continue; + if (r <= 0) + break; nread += (size_t)r; if (nread + 1 == cap) { cap *= 2; @@ -1208,7 +1242,8 @@ static bool vi_read_pattern(char lead) { static bool vi_command(int key) { int count = 0; while ((key >= '1' && key <= '9') || (count && key == '0')) { - count = count * 10 + (key - '0'); + if (count < 100000) + count = count * 10 + (key - '0'); key = read_key(); } bool counted = count > 0; @@ -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/history.c b/src/history.c index b15d6b8..e42a634 100644 --- a/src/history.c +++ b/src/history.c @@ -281,6 +281,8 @@ static int fc_edit(const char *editor, int lo, int hi) { sh_warn("fc: cannot create a temporary file: %s", strerror(errno)); return 1; } + if (SYS_MVS) + sys_tmp_tag(fd); char buf[1024]; output o = {buf, 0, sizeof buf, fd, 0}; int step = lo <= hi ? 1 : -1; @@ -825,7 +825,10 @@ static int job_wait(job *j) { * that is not a child of this shell. */ int builtin_wait(int argc, char **argv) { - if (argc < 2) { + int i = 1; + if (i < argc && !strcmp(argv[i], "--")) + i++; + if (i >= argc) { for (job *j = job_list, *next; j; j = next) { next = j->next; if (job_get_state(j) == JOB_STOPPED) @@ -840,7 +843,7 @@ int builtin_wait(int argc, char **argv) { return 0; } int status = 0; - for (int i = 1; i < argc; i++) { + for (; i < argc; i++) { job *j = nullptr; long pid = 0; if (argv[i][0] == '%') { @@ -863,6 +866,8 @@ int builtin_wait(int argc, char **argv) { continue; } status = job_wait(j); + if (job_get_state(j) == JOB_RUNNING) + return status; /* interrupted by a trapped signal */ if (job_get_state(j) == JOB_DONE) job_free(j); } @@ -960,9 +965,13 @@ int builtin_fg(int argc, char **argv) { sh_warn("fg: no job control"); return 2; } - job *j = job_find(argc > 1 ? argv[1] : nullptr); + int i = 1; + if (i < argc && !strcmp(argv[i], "--")) + i++; + jobs_reap(); + job *j = job_find(i < argc ? argv[i] : nullptr); if (!j || job_get_state(j) == JOB_DONE) { - sh_warn("fg: %s: no such job", argc > 1 ? argv[1] : "current"); + sh_warn("fg: %s: no such job", i < argc ? argv[i] : "current"); return 1; } job_make_current(j); @@ -991,6 +1000,9 @@ int builtin_bg(int argc, char **argv) { } int status = 0; int i = 1; + if (i < argc && !strcmp(argv[i], "--")) + i++; + jobs_reap(); do { job *j = job_find(i < argc ? argv[i] : nullptr); if (!j || job_get_state(j) == JOB_DONE) { @@ -169,6 +169,8 @@ int option_is_set(const char *name) { */ void options_print(bool as_commands) { for (size_t i = 0; i < ARRAY_LEN(option_table); i++) { + if (as_commands && option_table[i].startup_only) + continue; bool on = *option_table[i].value; if (as_commands) out_printf(&out1, "set %co %s\n", on ? '-' : '+', 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/printf.c b/src/printf.c index fabc0b3..511e185 100644 --- a/src/printf.c +++ b/src/printf.c @@ -6,6 +6,7 @@ #include <ctype.h> #include <errno.h> #include <inttypes.h> +#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -108,7 +109,7 @@ static const char *next_arg(printf_state *pf) { * A leading quote means "the code of the next character". A bad number is * reported and makes the exit status 1, but printing continues. */ -static intmax_t arg_int(printf_state *pf) { +static intmax_t arg_int(printf_state *pf, bool is_unsigned) { const char *s = next_arg(pf); if (!s) return 0; @@ -116,12 +117,14 @@ static intmax_t arg_int(printf_state *pf) { return (unsigned char)s[1]; char *end; errno = 0; - intmax_t v = strtoimax(s, &end, 0); - if (errno == ERANGE && *s != '-') - v = (intmax_t)strtoumax(s, &end, 0); + intmax_t v = + is_unsigned ? (intmax_t)strtoumax(s, &end, 0) : strtoimax(s, &end, 0); if (end == s || *end) { sh_warn("printf: %s: expected numeric value", s); pf->status = 1; + } else if (errno == ERANGE) { + sh_warn("printf: %s: out of range", s); + pf->status = 1; } return v; } @@ -136,10 +139,14 @@ static double arg_double(printf_state *pf) { if (*s == '\'' || *s == '"') return (unsigned char)s[1]; char *end; + errno = 0; double v = strtod(s, &end); if (end == s || *end) { sh_warn("printf: %s: expected numeric value", s); pf->status = 1; + } else if (errno == ERANGE && (v == HUGE_VAL || v == -HUGE_VAL)) { + sh_warn("printf: %s: out of range", s); + pf->status = 1; } return v; } @@ -157,7 +164,7 @@ static double arg_double(printf_state *pf) { static const char *copy_number(printf_state *pf, const char *f, char *spec, size_t *n, size_t dot) { if (*f == '*') { - intmax_t v = arg_int(pf); + intmax_t v = arg_int(pf, false); /* * C99: a negative precision is as if the precision were omitted, * so drop the '.' as well; a negative width is a '-' flag, which @@ -207,7 +214,7 @@ static const char *convert(printf_state *pf, const char *f) { spec[n++] = 'j'; spec[n++] = 'd'; spec[n] = '\0'; - out_printf(&out1, spec, arg_int(pf)); + out_printf(&out1, spec, arg_int(pf, false)); break; case 'u': case 'o': @@ -216,7 +223,7 @@ static const char *convert(printf_state *pf, const char *f) { spec[n++] = 'j'; spec[n++] = *f; spec[n] = '\0'; - out_printf(&out1, spec, (uintmax_t)arg_int(pf)); + out_printf(&out1, spec, (uintmax_t)arg_int(pf, true)); break; case 'e': case 'E': @@ -253,10 +260,14 @@ static const char *convert(printf_state *pf, const char *f) { const char *s = next_arg(pf); output mem = {.fd = OUT_MEM}; bool more = out_escaped(&mem, s ? s : "", true); - out_putc(&mem, '\0'); - spec[n++] = 's'; - spec[n] = '\0'; - out_printf(&out1, spec, mem.buf); + if (n == 1) { + out_write(&out1, mem.buf, mem.len); + } else { + out_putc(&mem, '\0'); + spec[n++] = 's'; + spec[n] = '\0'; + out_printf(&out1, spec, mem.buf); + } free(mem.buf); if (!more) return nullptr; 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/sys_posix.c b/src/sys_posix.c index 2517d3f..8ce561d 100644 --- a/src/sys_posix.c +++ b/src/sys_posix.c @@ -125,6 +125,10 @@ pid_t sys_spawn(const spawn_req *rq) { if (need_fa) { rc = posix_spawn_file_actions_init(&fa); + if (rc) { + errno = rc; + return -1; + } #if HAVE_SPAWN_TCSETPGRP /* * Runs in the child after it has joined its process group and @@ -132,12 +136,13 @@ pid_t sys_spawn(const spawn_req *rq) { * terminal before its first instruction and cannot be stopped by * SIGTTOU for asking. */ - if (!rc && rq->tty_fd >= 0) + if (rq->tty_fd >= 0) rc = posix_spawn_file_actions_addtcsetpgrp_np(&fa, rq->tty_fd); #endif if (!rc) rc = add_fd_actions(&fa, rq->fdmap); if (rc) { + posix_spawn_file_actions_destroy(&fa); errno = rc; return -1; } @@ -399,8 +404,12 @@ static void put32(unsigned char *p, size_t v) { /** @brief Store a name, blank-padded, in EBCDIC. */ static void put_name(unsigned char *p, const char *name, size_t n) { + size_t len = strlen(name); + if (len > n) + len = n; memset(p, ' ', n); - memcpy(p, name, strlen(name)); + memcpy(p, name, len); // NOLINT(bugprone-not-null-terminated-result): + // fixed-width MVS field mvs_to_ebcdic((char *)p, n); } diff --git a/src/sys_zos.c b/src/sys_zos.c index 38908ce..31db09d 100644 --- a/src/sys_zos.c +++ b/src/sys_zos.c @@ -607,7 +607,11 @@ typedef struct csi_req { } csi_req; typedef int iggcsi_fn(void *reason, void *field, void *work); +#ifdef _LP64 #pragma linkage(iggcsi_fn, OS) +#else +#pragma linkage(iggcsi_fn, OS_UPSTACK) +#endif static iggcsi_fn *csi_ep; /**< from fetch(), nullptr until loaded */ static bool csi_dead; /**< fetch failed or a call abended */ @@ -640,9 +644,9 @@ static int csi_call(csi_area *a, void *ctx) { sigaction(sigs[i], &sa, &old[i]); volatile int rc; if (sigsetjmp(csi_jb, 1) == 0) { - /* VL bit on the last address; harmless if the compiler sets it. */ - void *work = (void *)((uintptr_t)a->work | - ((uintptr_t)1 << (sizeof(uintptr_t) * 8 - 1))); + /* VL bit (bit 31) on the last 31-bit address; harmless if the + * compiler sets it. */ + void *work = (void *)((uintptr_t)a->work | (uintptr_t)0x80000000u); rc = csi_ep(a->reason, a->field, work); } else { *abended = true; @@ -680,7 +684,8 @@ bool sys_catalog_list(const char *filter, mvs_name_fn *fn, void *arg) { return false; } } - csi_req *r = malloc(sizeof *r); + /* IGGCSI00 is AMODE 31, so all three areas must live below 2 GB. */ + csi_req *r = __malloc31(sizeof *r); if (!r) return false; /* not sticky: storage may come back */ csi_area a = {r->reason, r->field, r->work, CSI_WORK}; @@ -752,7 +757,11 @@ bool sys_dd_names(mvs_name_fn *fn, void *arg) { /* ---- dynamic allocation ------------------------------------------------ */ typedef int bpxwdyn_fn(char *); +#ifdef _LP64 #pragma linkage(bpxwdyn_fn, OS) +#else +#pragma linkage(bpxwdyn_fn, OS_UPSTACK) +#endif /** * @brief Issue a BPXWDYN request, such as `alloc dd(in) da(x.y) shr`. @@ -779,7 +788,8 @@ int sys_dynalloc(const char *request, char *msg, size_t msglen) { return -1; } size_t len = strlen(request); - char *text = malloc(len + 1); + /* BPXWDY2 is AMODE 31, so the request string must live below 2 GB. */ + char *text = __malloc31(len + 1); if (!text) { snprintf(msg, msglen, "out of storage"); return -1; @@ -4,6 +4,7 @@ */ #include <errno.h> #include <inttypes.h> +#include <limits.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> @@ -86,7 +87,8 @@ bool test_unary(char op, const char *arg) { return *arg == '\0'; case 't': { long fd; - return parse_long(arg, &fd) && isatty((int)fd); + return parse_long(arg, &fd) && fd >= 0 && fd <= INT_MAX && + isatty((int)fd) != 0; } case 'r': return access(arg, R_OK) == 0; @@ -271,6 +273,38 @@ static bool parse_or(test_state *t) { } /** + * @brief Evaluate the POSIX two-argument `test` rule. + */ +static bool eval_2(test_state *t, const char *a, const char *b) { + if (!strcmp(a, "!")) + return *b == '\0'; + if (is_unary_op(a)) + return test_unary(a[1], b); + sh_warn("test: %s: unary operator expected", a); + t->error = true; + return false; +} + +/** + * @brief Evaluate the POSIX three-argument `test` rule (plus XSI `-a`/`-o`). + */ +static bool eval_3(test_state *t, const char *a, const char *b, const char *c) { + if (is_binary_op(b)) + return eval_binary(t, a, b, c); + if (strcmp(a, "!") == 0) + return !eval_2(t, b, c); + if (strcmp(a, "(") == 0 && strcmp(c, ")") == 0) + return *b != '\0'; + if (strcmp(b, "-a") == 0) + return (*a != '\0') && (*c != '\0'); + if (strcmp(b, "-o") == 0) + return (*a != '\0') || (*c != '\0'); + sh_warn("test: %s: unexpected operator", b); + t->error = true; + return false; +} + +/** * @brief The `test` and `[` builtins. * * With up to four arguments the POSIX rules by argument count are @@ -287,13 +321,28 @@ int builtin_test(int argc, char **argv) { } argc--; } - if (argc == 1) + int n = argc - 1; + if (n == 0) return 1; + if (n == 1) + return argv[1][0] != '\0' ? 0 : 1; test_state t = {.argv = argv, .pos = 1, .argc = argc}; - bool v = parse_or(&t); - if (!t.error && remaining(&t) > 0) { - sh_warn("test: %s: unexpected operator", t.argv[t.pos]); - t.error = true; + bool v; + if (n == 2) { + v = eval_2(&t, argv[1], argv[2]); + } else if (n == 3) { + v = eval_3(&t, argv[1], argv[2], argv[3]); + } else if (n == 4 && strcmp(argv[1], "!") == 0) { + v = !eval_3(&t, argv[2], argv[3], argv[4]); + } else if (n == 4 && strcmp(argv[1], "(") == 0 && + strcmp(argv[4], ")") == 0) { + v = eval_2(&t, argv[2], argv[3]); + } else { + v = parse_or(&t); + if (!t.error && remaining(&t) > 0) { + sh_warn("test: %s: unexpected operator", t.argv[t.pos]); + t.error = true; + } } if (t.error) return 2; @@ -140,7 +140,9 @@ static void set_handler(int sig, void (*handler)(int)) { * * @param sig The signal. */ -void trap_sig_default(int sig) { set_handler(sig, SIG_DFL); } +void trap_sig_default(int sig) { + set_handler(sig, SIG_DFL); +} /** * @brief What a signal does in this shell when no trap is set for it. @@ -292,7 +294,7 @@ void trap_poll(void) { } else { exc_handler = prev; running = false; - for (int s = sig; s <= MAXSIG; s++) + for (int s = 1; s <= MAXSIG; s++) if (got_signal[s]) { trap_pending = (sig_atomic_t)s; break; @@ -338,7 +340,8 @@ void trap_reset_child(void) { show_parent = true; trap_err_set = false; if (have_shell_ignored) { - set_handler(SIGINT, SIG_DFL); + if (!trap_action[SIGINT]) + set_handler(SIGINT, SIG_DFL); for (int sig = 1; sig <= MAXSIG; sig++) { if (sigismember(&shell_ignored, sig) == 1 && !trap_action[sig]) set_handler(sig, SIG_DFL); @@ -361,7 +364,8 @@ void trap_reset_child(void) { void trap_reset_exec(void) { if (!have_shell_ignored) return; - set_handler(SIGINT, SIG_DFL); + if (!trap_action[SIGINT]) + set_handler(SIGINT, SIG_DFL); for (int sig = 1; sig <= MAXSIG; sig++) { if (sigismember(&shell_ignored, sig) == 1 && !trap_action[sig]) set_handler(sig, SIG_DFL); @@ -576,11 +580,15 @@ int builtin_kill(int argc, char **argv) { if (i < argc && !strcmp(argv[i], "-s")) { sig = i + 1 < argc ? signal_number(argv[i + 1]) : -1; i += 2; + if (i < argc && !strcmp(argv[i], "--")) + i++; } else if (i < argc && !strcmp(argv[i], "--")) { i++; } else if (i < argc && argv[i][0] == '-' && argv[i][1]) { sig = signal_number(argv[i] + 1); i++; + if (i < argc && !strcmp(argv[i], "--")) + i++; } if (sig < 0 || sig > MAXSIG) { sh_warn("kill: bad signal"); @@ -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) == 0; + 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; diff --git a/tests/zash/audit-fixes.exp b/tests/zash/audit-fixes.exp new file mode 100644 index 0000000..ae837a4 --- /dev/null +++ b/tests/zash/audit-fixes.exp @@ -0,0 +1,39 @@ +base:255 35 35 61 +dup-closed-ext:2 +dup-closed-spawn:2 +cond-cmdsub:0 +outer +qbrace:a}b +for-semi-in:2 +env:2 +1 ++ tag: : ok +subshell-werr:1 +pipe-loop:1 +pipe-loop:2 +set-plus-o:0 +t1:0 +t2:0 +t3:0 +t4:0 +t5:0 +t6:0 +t7:1 +t8:1 +print-u-oob:2 +whence-dir:1 +pf-hi:1 +pf-lo:1 +pf-dbl:1 +3 +time +select +eval-dd:ok +export-dd:ok +ro-dd:ok +0022 +keep=yes +wait-dd:0 +kill-dd:0 +read-t0:0:ready +[status 0] diff --git a/tests/zash/audit-fixes.sh b/tests/zash/audit-fixes.sh new file mode 100644 index 0000000..c17a988 --- /dev/null +++ b/tests/zash/audit-fixes.sh @@ -0,0 +1,83 @@ +# Regression tests for audit fixes across parser, redirection, builtins, +# arithmetic, signals, test, printf, and options. + +# 1. Arithmetic uppercase digits for bases <= 36 (and case-sensitive 37..64) +"$TSH" -c 'echo "base:$((16#FF)) $((36#Z)) $((64#z)) $((64#Z))"' + +# 2. Duplicating from a closed descriptor on an external command fails +"$TSH" -c 'true 3>&- >&3' >/dev/null 2>/dev/null +echo "dup-closed-ext:$?" +"$TSH" -c '/bin/sh -c : 3>&- >&3' >/dev/null 2>/dev/null +echo "dup-closed-spawn:$?" + +# 3. Parser: command substitution inside [[ ... ]], nested here-docs, +# quoted braces inside "${...}", and rejecting "for x; in ..." +"$TSH" -c '[[ -z $(echo hi >/dev/null) ]]; echo "cond-cmdsub:$?"' +"$TSH" -c ' +cat <<OUTER $(cat <<INNER >/dev/null +inner +INNER +) +outer +OUTER +' +"$TSH" -c 'unset x; echo "qbrace:${x:-"a}b"}"' +"$TSH" -c 'for x; in a b; do echo "$x"; done' 2>/dev/null +echo "for-semi-in:$?" + +# 4. Environment deduplication for repeated prefix assignments +"$TSH" -c 'X=1 X=2 /bin/sh -c "echo env:\$X"' +"$TSH" -c 'X=1 X=2 env | grep "^X=" | wc -l | tr -d " "' + +# 5. PS4 expansion in xtrace (including command substitution without recursion) +"$TSH" -c 'PS4="+ \$(echo tag): "; set -x; : ok' 2>&1 + +# 6. Pure subshell reports stdout write failure +"$TSH" -c '( echo hi ) >&-' 2>/dev/null +echo "subshell-werr:$?" + +# 7. Pipeline error unwind restores loop_depth +"$TSH" -c ' +for i in 1 2; do + echo x | eval '\''echo "${unset?}"'\'' 2>/dev/null + echo "pipe-loop:$i" +done +' + +# 8. set +o omits startup-only options so eval "$(set +o)" succeeds +"$TSH" -c 'saved=$(set +o); eval "$saved"; echo "set-plus-o:$?"' + +# 9. POSIX 1-4 argument test/[ rules and -t fd bounds +"$TSH" -c ' +[ "(" -f ")" ]; echo "t1:$?" +[ "(" ! ")" ]; echo "t2:$?" +[ -z -a -z ]; echo "t3:$?" +[ -f -a -f ]; echo "t4:$?" +[ ! "" -a "x" ]; echo "t5:$?" +[ "(" ! "" ")" ]; echo "t6:$?" +[ ! "(" -f ")" ]; echo "t7:$?" +[ -t 4294967297 ]; echo "t8:$?" +' + +# 10. print -u fd upper bound and whence -p regular-file check +"$TSH" -c 'print -u 4294967297 hi' 2>/dev/null +echo "print-u-oob:$?" +"$TSH" -c 'whence -p /tmp' >/dev/null 2>&1 +echo "whence-dir:$?" + +# 11. printf overflow checks and NUL preservation in bare %b +"$TSH" -c 'printf "%d\n" 999999999999999999999999999 >/dev/null 2>/dev/null; echo "pf-hi:$?"' +"$TSH" -c 'printf "%d\n" -999999999999999999999999999 >/dev/null 2>/dev/null; echo "pf-lo:$?"' +"$TSH" -c 'printf "%f\n" 1e99999 >/dev/null 2>/dev/null; echo "pf-dbl:$?"' +"$TSH" -c 'printf "%b" "a\0000b" | wc -c | tr -d " "' + +# 12. Keywords, -- handling, unalias -- -a, kill --, read -t 0 +"$TSH" -c 'command -v time select' +"$TSH" -c 'eval -- echo "eval-dd:ok"' +"$TSH" -c 'export -- Z_DD=ok; echo "export-dd:$Z_DD"' +"$TSH" -c 'readonly -- Z_RO=ok; echo "ro-dd:$Z_RO"' +"$TSH" -c 'umask -- 022; umask' +"$TSH" -c 'alias keep=yes -a=no; unalias -- -a; alias keep' +"$TSH" -c 'wait --; echo "wait-dd:$?"' +"$TSH" -c 'kill -0 -- $$; echo "kill-dd:$?"' +"$TSH" -c 'printf "ready\n" | { sleep 0.1; read -t 0 line; echo "read-t0:$?:$line"; }' |
