Skip to contentSkip to conversation

bug: 64bit arithmetic/number parsing

PATCH New Szász Gergely 2 participants 2 messages Feb 13, 2025 – Sep 14, 2025

lore
Share & tools
lore
#0 lore raw

Hello,

$ dash
$ echo $((0x8000000000000000))
9223372036854775807
$ printf %x\\n $((0x8000000000000000))
7fffffffffffffff
$ printf "%x %x\n" $((0x8000000000000000)) $((0x8000000000000000-1))
7fffffffffffffff 7ffffffffffffffe
$ echo $((-9223372036854775808))
-9223372036854775807
$ x=0x8000000000000000
$ echo $((x))
dash: 6: Illegal number: 0x8000000000000000
$ exit
$ bash
$ echo $((0x8000000000000000))
-9223372036854775808
$ printf %x\\n $((0x8000000000000000))
8000000000000000
$ printf "%x %x\n" $((0x8000000000000000)) $((0x8000000000000000-1))
8000000000000000 7fffffffffffffff
$ echo $((-9223372036854775808))
-9223372036854775808
$ x=0x8000000000000000
$ echo $((x))
-9223372036854775808

Thanks,
Gergely

#1 lore raw

PATCH 1/1 mystring: Ignore ERANGE errors in atomax

Quoted 15 lines · Szász Gergely

Hello,

$ dash
$ echo $((0x8000000000000000))
9223372036854775807
$ printf %x\\n $((0x8000000000000000))
7fffffffffffffff
$ printf "%x %x\n" $((0x8000000000000000)) $((0x8000000000000000-1))
7fffffffffffffff 7ffffffffffffffe
$ echo $((-9223372036854775808))
-9223372036854775807
$ x=0x8000000000000000
$ echo $((x))
dash: 6: Illegal number: 0x8000000000000000
$ exit

So the only inconsistency here is that $((x)) does a range check while the others don't. I looked into the history and the range check came from the built-in test(1) utility. In that case other shells (ksh93) don't necessarily do a range check.

So I've decided to simply remove the range check for consistency.

---8<---
Remove ERANGE check in atomax for consistency as other direct calls to strtoimax do not perform the range check.

src/mystring.c +0 -3
index ca0cd39..97e240c 100644
@@ -129,9 +129,6 @@ intmax_t atomax(const char *s, int base)
 	errno = 0;
 	r = strtoimax(s, &p, base);
 
-	if (errno == ERANGE)
-		badnum(s);
-
 	/*
 	 * Disallow completely blank strings in non-arithmetic (base != 0)
 	 * contexts.
Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt