Fix numeric constant overflow checks

This commit is contained in:
ISSOtm
2020-08-16 02:48:41 +02:00
parent 08867b3cec
commit 3f5f9bcaf0
5 changed files with 14 additions and 11 deletions

View File

@@ -920,7 +920,7 @@ static void readNumber(int radix, int32_t baseValue)
if (c < '0' || c > '0' + radix - 1) if (c < '0' || c > '0' + radix - 1)
break; break;
if (value > UINT32_MAX / radix) if (value > (UINT32_MAX - (c - '0')) / radix)
warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n"); warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n");
value = value * radix + (c - '0'); value = value * radix + (c - '0');
@@ -940,7 +940,7 @@ static void readFractionalPart(void)
if (c < '0' || c > '9') if (c < '0' || c > '9')
break; break;
if (divisor > UINT32_MAX / 10) { if (divisor > (UINT32_MAX - (c - '0')) / 10) {
warning(WARNING_LARGE_CONSTANT, warning(WARNING_LARGE_CONSTANT,
"Precision of fixed-point constant is too large\n"); "Precision of fixed-point constant is too large\n");
/* Discard any additional digits */ /* Discard any additional digits */
@@ -948,6 +948,7 @@ static void readFractionalPart(void)
shiftChars(1); shiftChars(1);
break; break;
} }
value = value * 10 + (c - '0');
} }
if (yylval.nConstValue > INT16_MAX || yylval.nConstValue < INT16_MIN) if (yylval.nConstValue > INT16_MAX || yylval.nConstValue < INT16_MIN)
@@ -972,6 +973,8 @@ static void readBinaryNumber(void)
/* TODO: handle `-b`'s dynamic chars */ /* TODO: handle `-b`'s dynamic chars */
if (c != '0' && c != '1') if (c != '0' && c != '1')
break; break;
if (value > (UINT32_MAX - (c - '0')) / 2)
warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n");
value = value * 2 + (c - '0'); value = value * 2 + (c - '0');
shiftChars(1); shiftChars(1);
@@ -998,7 +1001,7 @@ static void readHexNumber(void)
else else
break; break;
if (value > UINT32_MAX / 16) if (value > (UINT32_MAX - c) / 16)
warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n"); warning(WARNING_LARGE_CONSTANT, "Integer constant is too large\n");
value = value * 16 + c; value = value * 16 + c;
@@ -1030,16 +1033,16 @@ static void readGfxConstant(void)
bp0 = bp0 << 1 | (pixel & 1); bp0 = bp0 << 1 | (pixel & 1);
bp1 = bp1 << 1 | (pixel >> 1); bp1 = bp1 << 1 | (pixel >> 1);
} }
if (width <= 8) if (width < 9)
width++; width++;
shiftChars(1); shiftChars(1);
} }
if (width == 0) if (width == 0)
error("Invalid gfx constant, no digits after '`'\n"); error("Invalid graphics constant, no digits after '`'\n");
else if (width == 8) else if (width == 9)
warning(WARNING_LARGE_CONSTANT, 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; yylval.nConstValue = bp1 << 8 | bp0;
} }

View File

@@ -0,0 +1 @@
Success!

View File

@@ -2,7 +2,6 @@
; file doesn't cause a segfault. ; file doesn't cause a segfault.
bar: MACRO bar: MACRO
WARN ""
ENDM ENDM
foo: bar baz\ foo: bar baz\

View File

@@ -1 +1 @@
$0

View File

@@ -3,6 +3,6 @@ warning: overflow.asm(24): [-Wdiv]
warning: overflow.asm(25): [-Wdiv] warning: overflow.asm(25): [-Wdiv]
Division of -2147483648 by -1 yields -2147483648 Division of -2147483648 by -1 yields -2147483648
warning: overflow.asm(39): [-Wlarge-constant] warning: overflow.asm(39): [-Wlarge-constant]
Integer constant '4294967296' is too large Integer constant is too large
warning: overflow.asm(42): [-Wlarge-constant] warning: overflow.asm(42): [-Wlarge-constant]
Graphics constant '`333333333' is too long Graphics constant is too long, only 8 first pixels considered