ref: 3f5f9bcaf01c0f6b3382f6fb85daa3ac7857ebee
parent: 08867b3cec6fbc8bc215a1049055e3c65281751c
author: ISSOtm <eldredhabert0@gmail.com>
date: Sat Aug 15 22:48:41 EDT 2020
Fix numeric constant overflow checks
--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -920,7 +920,7 @@
if (c < '0' || c > '0' + radix - 1)
break;
- if (value > UINT32_MAX / radix)
+ if (value > (UINT32_MAX - (c - '0')) / radix)
warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n");
value = value * radix + (c - '0');
@@ -940,7 +940,7 @@
if (c < '0' || c > '9')
break;
- if (divisor > UINT32_MAX / 10) {
+ if (divisor > (UINT32_MAX - (c - '0')) / 10) {
warning(WARNING_LARGE_CONSTANT,
"Precision of fixed-point constant is too large\n");
/* Discard any additional digits */
@@ -948,6 +948,7 @@
shiftChars(1);
break;
}
+ value = value * 10 + (c - '0');
}
if (yylval.nConstValue > INT16_MAX || yylval.nConstValue < INT16_MIN)
@@ -972,6 +973,8 @@
/* TODO: handle `-b`'s dynamic chars */
if (c != '0' && c != '1')
break;
+ if (value > (UINT32_MAX - (c - '0')) / 2)
+ warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n");
value = value * 2 + (c - '0');
shiftChars(1);
@@ -998,7 +1001,7 @@
else
break;
- if (value > UINT32_MAX / 16)
+ if (value > (UINT32_MAX - c) / 16)
warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n");
value = value * 16 + c;
@@ -1030,16 +1033,16 @@
bp0 = bp0 << 1 | (pixel & 1);
bp1 = bp1 << 1 | (pixel >> 1);
}
- if (width <= 8)
+ if (width < 9)
width++;
shiftChars(1);
}
if (width == 0)
- error("Invalid gfx constant, no digits after '`'\n");
- else if (width == 8)
+ error("Invalid graphics constant, no digits after '`'\n");
+ else if (width == 9)
warning(WARNING_LARGE_CONSTANT,
- "Gfx constant is too large, only 8 first pixels considered\n");
+ "Graphics constant is too long, only 8 first pixels considered\n");
yylval.nConstValue = bp1 << 8 | bp0;
}
--- a/test/asm/equs-nest.out
+++ b/test/asm/equs-nest.out
@@ -1,0 +1,1 @@
+Success!
--- a/test/asm/line-continuation-whitespace.asm
+++ b/test/asm/line-continuation-whitespace.asm
@@ -2,7 +2,6 @@
; file doesn't cause a segfault.
bar: MACRO
- WARN ""
ENDM
foo: bar baz\
--- a/test/asm/local-purge.out
+++ b/test/asm/local-purge.out
@@ -1,1 +1,1 @@
-$0
+
--- a/test/asm/overflow.err
+++ b/test/asm/overflow.err
@@ -3,6 +3,6 @@
warning: overflow.asm(25): [-Wdiv]
Division of -2147483648 by -1 yields -2147483648
warning: overflow.asm(39): [-Wlarge-constant]
- Integer constant '4294967296' is too large
+ Integer constant is too large
warning: overflow.asm(42): [-Wlarge-constant]
- Graphics constant '`333333333' is too long
+ Graphics constant is too long, only 8 first pixels considered