shithub: scc

Download patch

ref: ae8167e3913f12644ac71c6a0d9e6b9376b2ad37
parent: 5b9d9b6057b4ebdb7d4590c849607f6d0765c00c
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Jan 23 09:49:30 EST 2018

[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.

--- 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 @@
 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;
 }