commit 900ff494ec2f7acf846920a576ac7a701f941899
parent 0f4df1ecbd2a1b0067ae779b28599668c7a27167
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 23 Jan 2018 14:49:30 +0000
[as] Add number parsing
Until this moment we were using atoi() to convert strings
to numbers. This patch adds a function that does this work
and it also verifies that there isn't any overflow.
Diffstat:
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/as/expr.c b/as/expr.c
@@ -1,6 +1,7 @@
static char sccsid[] = "@(#) ./as/node.c";
#include <ctype.h>
+#include <limits.h>
#include <stdlib.h>
#include <string.h>
@@ -210,13 +211,27 @@ out_loop:
static int
number(void)
{
- int c;
+ int c, base = 10;
char *p;
+ TUINT n;
- while (isxdigit(*endp))
+ if (*endp == '0') {
+ base = 8;
++endp;
+ if (*endp == 'x') {
+ base = 16;
+ ++endp;
+ }
+ }
+ for (n = 0; (c = *endp) && isxdigit(c); n += c) {
+ n *= base;
+ c -= '0';
+ if (n >= TUINT_MAX - c*base)
+ error("overflow in number");
+ endp++;
+ }
tok2str();
- yylval.sym = tmpsym(atoi(yytext)); /* TODO: parse the string */
+ yylval.sym = tmpsym(n);
return NUMBER;
}