diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:23:42 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:23:42 -0600 |
| commit | ec1cd3eb8e1aa177329f25d134f372d3e999d4a4 (patch) | |
| tree | 75bae4df72bdb54da467dc5b404caa49c0781e0e | |
| parent | 9cdf5f548dc8e1d5c6315b304819bf0fd60f802f (diff) | |
| download | zash-ec1cd3eb8e1aa177329f25d134f372d3e999d4a4.tar.gz | |
fix(arith): parse base#digits without signed overflow
`$((99999999999999999999#1))` multiplied its way through INT_MAX: UBSan
reports "signed integer overflow" at the base loop in parse_based(), and
in a release build the resulting base is whatever the wrap gave.
The base is now built with a bound (anything over 64 is "bad number
base" at once) and the digits are read by hand against ksh's digit set,
so `16#0x10`, `16# f`, `2#2` and `16#` stay errors instead of relying on
strtoumax()'s quirks. The digit set is a string looked up with strchr(),
not arithmetic on character codes: the letters are not contiguous in
EBCDIC, and the same source has to work on both sides.
That also makes bases 37 to 64 work, as they do in ksh:
`$((64#z))` is 35, `64#_` is 63, `64#@` is 62. Values still wrap at
UINTMAX_MAX, which is what the old code did for everything else, so a
literal that does not fit is still "bad number".
Verified: before the change, zash-asan reports src/arith.c:270 signed
integer overflow for the huge base and rejects 64#z; after it, the new
tests pass under ASan+UBSan on Linux. The EBCDIC build is untested here
(the mapping is a string constant, so it does not depend on the charset).
| -rw-r--r-- | src/arith.c | 34 | ||||
| -rw-r--r-- | tests/zash/arith-base.exp | 13 | ||||
| -rw-r--r-- | tests/zash/arith-base.sh | 11 |
3 files changed, 51 insertions, 7 deletions
diff --git a/src/arith.c b/src/arith.c index c7c410f..5097134 100644 --- a/src/arith.c +++ b/src/arith.c @@ -266,16 +266,36 @@ static intmax_t apply(const arith *a, binop op, intmax_t x, intmax_t y) { */ static intmax_t parse_based(arith *a, const char *p, const char *hash) { int base = 0; - for (const char *q = p; q < hash; q++) + for (const char *q = p; q < hash; q++) { base = base * 10 + (*q - '0'); - if (base < 2 || base > 36) + if (base > 64) + arith_error(a, "bad number base"); + } + if (base < 2) arith_error(a, "bad number base"); - char *end; - errno = 0; - uintmax_t v = strtoumax(hash + 1, &end, base); - if (errno || end == hash + 1 || isalnum((unsigned char)*end) || *end == '_') + /* + * The digit set of ksh and bash. Spelling it out beats arithmetic on + * character codes: the letters are not contiguous in EBCDIC. + */ + static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ@_"; + uintmax_t v = 0; + const char *q = hash + 1; + for (; *q; q++) { + const char *d = strchr(digits, *q); + if (!d) + break; + uintmax_t dv = (uintmax_t)(d - digits); + if (dv >= (uintmax_t)base) + break; + if (v > (UINTMAX_MAX - dv) / (uintmax_t)base) + arith_error(a, "bad number"); + v = v * base + dv; + } + /* No digits, or a digit the base does not have (16#0x10, 16# f). */ + if (q == hash + 1 || (*q && strchr(digits, *q))) arith_error(a, "bad number"); - a->p = end; + a->p = q; return (intmax_t)v; } diff --git a/tests/zash/arith-base.exp b/tests/zash/arith-base.exp index 9fb1dd6..93d4055 100644 --- a/tests/zash/arith-base.exp +++ b/tests/zash/arith-base.exp @@ -2,4 +2,17 @@ 8 rc=2 arithmetic: bad number base: "1#0" +rc=2 +arithmetic: bad number base: "99999999999999999999#1" +rc=2 +arithmetic: bad number: "16# f" +rc=2 +arithmetic: bad number: "16#0x10" +rc=2 +arithmetic: bad number: "2#2" +rc=2 +arithmetic: bad number: "36#zzzzzzzzzzzzzz" +rc=2 +arithmetic: bad number: "16#" +35 63 61 62 [status 0] diff --git a/tests/zash/arith-base.sh b/tests/zash/arith-base.sh index 5414e08..c361eae 100644 --- a/tests/zash/arith-base.sh +++ b/tests/zash/arith-base.sh @@ -5,3 +5,14 @@ "$TSH" -c 'echo $((1#0))' >/dev/null 2>err echo "rc=$?" sed 's/^[^:]*: line [0-9]*: //' err +# A base too large to be a base, a missing digit, and a digit the base +# does not have are errors, not an overflow trap: the base is built with a +# bound and every digit is validated against the base. +for e in '99999999999999999999#1' '16# f' '16#0x10' '2#2' \ + '36#zzzzzzzzzzzzzz' '16#'; do + "$TSH" -c "echo \$(($e))" >/dev/null 2>err + echo "rc=$?" + sed 's/^[^:]*: line [0-9]*: //' err +done +# ksh's digit set: 10-35 are a-z, 36-61 are A-Z, 62 is '@', 63 is '_'. +"$TSH" -c 'echo $((64#z)) $((64#_)) $((64#Z)) $((64#@))' |
