summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 22:02:44 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 22:02:44 -0600
commit3ded9a80de9a487bcae31c695740d82d1a0e13fd (patch)
treea140e848fef1f7524563d673a6d95a901160b81c
parent59825a1acf69acb5b47f0471ff8929294c48b942 (diff)
downloadzash-3ded9a80de9a487bcae31c695740d82d1a0e13fd.tar.gz
Add regression tests for audit fixes and clear clang-tidy findings
- Add tests/zash/audit-fixes.{sh,exp} covering uppercase bases, closed-fd duplication, [[ ... ]] and nested here-docs, quoted braces, 'for x; in', environment deduplication, PS4 expansion, pure subshell write errors, pipeline unwind loop_depth, 'set +o', POSIX 1-4 arg test rules, print -u bounds, whence -p, printf overflow and bare %b NULs, time/select keywords, '--' handling, unalias -- -a, kill --, and read -t 0. - Clear remaining clang-tidy warnings on changed lines in edit_builtin.c, sys_posix.c, test.c, and var.c. Verified on Linux (./zash and ./zash-asan); z/OS CBT verification next.
-rw-r--r--src/edit_builtin.c4
-rw-r--r--src/sys_posix.c3
-rw-r--r--src/test.c19
-rw-r--r--src/var.c2
-rw-r--r--tests/zash/audit-fixes.exp39
-rw-r--r--tests/zash/audit-fixes.sh83
6 files changed, 137 insertions, 13 deletions
diff --git a/src/edit_builtin.c b/src/edit_builtin.c
index d9d7873..137d19a 100644
--- a/src/edit_builtin.c
+++ b/src/edit_builtin.c
@@ -514,7 +514,7 @@ static int ctrl_byte(unsigned char d) {
d = (unsigned char)toupper(d);
#if ZASH_ASCII
if (d >= '@' && d <= '_')
- return d & 0x1f;
+ return (int)((unsigned)d & 0x1fu);
return -1;
#else
static const char letters[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
@@ -1017,7 +1017,7 @@ static void edit_external(void) {
ssize_t nl = -1;
while (off == ed.len && (nl = write(fd, "\n", 1)) < 0 && errno == EINTR)
;
- bool wrote = off == ed.len && nl == 1;
+ bool wrote = (off == ed.len) && (nl == 1);
close(fd);
const char *editor = var_get("VISUAL");
if (!editor || !*editor)
diff --git a/src/sys_posix.c b/src/sys_posix.c
index b50d830..8ce561d 100644
--- a/src/sys_posix.c
+++ b/src/sys_posix.c
@@ -408,7 +408,8 @@ static void put_name(unsigned char *p, const char *name, size_t n) {
if (len > n)
len = n;
memset(p, ' ', n);
- memcpy(p, name, len);
+ memcpy(p, name, len); // NOLINT(bugprone-not-null-terminated-result):
+ // fixed-width MVS field
mvs_to_ebcdic((char *)p, n);
}
diff --git a/src/test.c b/src/test.c
index f2764b3..501f571 100644
--- a/src/test.c
+++ b/src/test.c
@@ -88,7 +88,7 @@ bool test_unary(char op, const char *arg) {
case 't': {
long fd;
return parse_long(arg, &fd) && fd >= 0 && fd <= INT_MAX &&
- isatty((int)fd);
+ isatty((int)fd) != 0;
}
case 'r':
return access(arg, R_OK) == 0;
@@ -291,14 +291,14 @@ static bool eval_2(test_state *t, const char *a, const char *b) {
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, "!"))
+ if (strcmp(a, "!") == 0)
return !eval_2(t, b, c);
- if (!strcmp(a, "(") && !strcmp(c, ")"))
+ if (strcmp(a, "(") == 0 && strcmp(c, ")") == 0)
return *b != '\0';
- if (!strcmp(b, "-a"))
- return *a != '\0' && *c != '\0';
- if (!strcmp(b, "-o"))
- return *a != '\0' || *c != '\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;
@@ -332,9 +332,10 @@ int builtin_test(int argc, char **argv) {
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], "!")) {
+ } 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], "(") && !strcmp(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);
diff --git a/src/var.c b/src/var.c
index df8bbba..686c6f2 100644
--- a/src/var.c
+++ b/src/var.c
@@ -420,7 +420,7 @@ char **var_environ_with(char **extra, int nextra) {
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);
+ shadowed = strncmp(extra[i], extra[j], nlen) == 0;
if (!shadowed)
env[k++] = extra[i];
}
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"; }'