diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 21:58:27 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 21:58:27 -0600 |
| commit | 59825a1acf69acb5b47f0471ff8929294c48b942 (patch) | |
| tree | a41c0580632a312da1148ec12a4e7e9e907f5ada | |
| parent | eb8f9331cd2f0c240ba1323ef136b31611215f29 (diff) | |
| download | zash-59825a1acf69acb5b47f0471ff8929294c48b942.tar.gz | |
Fix builtin, signal, test, printf, option, and job-control bugs
- Skip startup-only options in options_print() for 'set +o'.
- Preserve ignored SIGINT traps in trap_reset_child()/trap_reset_exec(),
rescan signals from 1 on trap_poll() unwind, and accept '--' after the
signal operand in kill.
- Implement POSIX 1-4 argument test/[ rules and bound-check -t fds.
- Bound-check 'print -u' against SYS_NFD and require regular files in
'whence -p' with '/'.
- Report ERANGE overflow in printf integer/float conversions and preserve
embedded NUL bytes in bare %b.
- Use exc_catch() in builtin_cd_isolated(), check sys_dupfd() in '.',
include time/select in is_keyword(), poll with timeout 0 for 'read -t 0',
accept '--' in eval/export/readonly/umask/unalias/wait/fg/bg, return on
trapped signal during operand wait, and reap jobs in fg/bg.
Verified locally on Linux (make, make zos-lint, make check: 128/129 pass).
| -rw-r--r-- | src/alias.c | 32 | ||||
| -rw-r--r-- | src/builtin.c | 36 | ||||
| -rw-r--r-- | src/builtin_ext.c | 22 | ||||
| -rw-r--r-- | src/jobs.c | 20 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rw-r--r-- | src/printf.c | 33 | ||||
| -rw-r--r-- | src/test.c | 60 | ||||
| -rw-r--r-- | src/trap.c | 16 |
8 files changed, 167 insertions, 54 deletions
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/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; } @@ -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/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; @@ -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); } 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, "!")) + return !eval_2(t, b, c); + if (!strcmp(a, "(") && !strcmp(c, ")")) + return *b != '\0'; + if (!strcmp(b, "-a")) + return *a != '\0' && *c != '\0'; + if (!strcmp(b, "-o")) + 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,27 @@ 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], "!")) { + v = !eval_3(&t, argv[2], argv[3], argv[4]); + } else if (n == 4 && !strcmp(argv[1], "(") && !strcmp(argv[4], ")")) { + 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"); |
