commit b4a14737671031ef9d57de428ba3c1a1618c644a
parent 774054fa8b77986acb2fa39f0bfe2a8c642b32c5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 10 Feb 2024 22:56:16 +0100
as: Fix number parsing
Parsing numbers had many errors, including ignoring the base
and wrong conversion from digits to numbers.
Diffstat:
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/cmd/as/parser.c b/src/cmd/as/parser.c
@@ -93,6 +93,7 @@ number(void)
int c, base = 10;
char *p;
TUINT n;
+ static char digits[] = "0123456789ABCDEF";
if (*endp == '0') {
base = 8;
@@ -103,10 +104,13 @@ number(void)
}
}
for (n = 0; (c = *endp) && isxdigit(c); n += c) {
- n *= base;
- c -= '0';
- if (n >= TUINT_MAX - c*base)
+ p = strchr(digits, toupper(c));
+ c = p - digits;
+ if (c > base)
+ error("invalid digit in number");
+ if (n >= TUINT_MAX/base - c)
error("overflow in number");
+ n *= base;
endp++;
}
tok2str();