mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-23 03:22:08 +00:00
Fix signed integer overflow issues
It seemed that the consensus in our discussions of signed integer overflow, which invokes undefined behavior in C, was that integer arithmetic should be two's complement and there should be no warning for overflows. I have implemented that by converting values to unsigned types when appropriate. These changes will mostly preserve existing behavior, except for a few cases that were being handled incorrectly before. The case of dividing INT_MIN by -1 previously resulted in a CPU exception and program termination. Now, that case is detected and results in a warning and a value of INT_MIN. Similarly, INT_MIN % -1 would have resulted in a CPU exception. Since this is a mathematically valid operation with a result of 0, it now simply gives that result without a warning. I noticed that in rpn.c, there were attempts in certain operation handlers to validate the nVal members of the source expressions even when the expressions may have been relocatable expressions with meaningless numbers for the nVal member. This could have caused spurious errors/warnings, so I made those handlers confirm that isReloc is false before validating nVal. Also, integer constants that are too large now result in a warning. The post-conversion values have not been changed, in order to preserve backward compatibility.
This commit is contained in:
@@ -71,8 +71,9 @@ typedef int32_t(*x2bin) (char ch);
|
||||
|
||||
static int32_t ascii2bin(char *s)
|
||||
{
|
||||
int32_t radix = 10;
|
||||
int32_t result = 0;
|
||||
char *start = s;
|
||||
uint32_t radix = 10;
|
||||
uint32_t result = 0;
|
||||
x2bin convertfunc = char2bin;
|
||||
|
||||
switch (*s) {
|
||||
@@ -101,6 +102,9 @@ static int32_t ascii2bin(char *s)
|
||||
break;
|
||||
}
|
||||
|
||||
const uint32_t max_q = UINT32_MAX / radix;
|
||||
const uint32_t max_r = UINT32_MAX % radix;
|
||||
|
||||
if (*s == '\0') {
|
||||
/*
|
||||
* There are no digits after the radix prefix
|
||||
@@ -108,15 +112,39 @@ static int32_t ascii2bin(char *s)
|
||||
*/
|
||||
yyerror("Invalid integer constant");
|
||||
} else if (radix == 4) {
|
||||
int32_t size = 0;
|
||||
int32_t c;
|
||||
|
||||
while (*s != '\0') {
|
||||
c = convertfunc(*s++);
|
||||
result = result * 2 + ((c & 2) << 7) + (c & 1);
|
||||
size++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extending a graphics constant longer than 8 pixels,
|
||||
* the Game Boy tile width, produces a nonsensical result.
|
||||
*/
|
||||
if (size > 8) {
|
||||
warning("Graphics constant '%s' is too long",
|
||||
start);
|
||||
}
|
||||
} else {
|
||||
while (*s != '\0')
|
||||
result = result * radix + convertfunc(*s++);
|
||||
bool overflow = false;
|
||||
|
||||
while (*s != '\0') {
|
||||
int32_t digit = convertfunc(*s++);
|
||||
|
||||
if (result > max_q
|
||||
|| (result == max_q && digit > max_r)) {
|
||||
overflow = true;
|
||||
}
|
||||
result = result * radix + digit;
|
||||
}
|
||||
|
||||
if (overflow)
|
||||
warning("Integer constant '%s' is too large",
|
||||
start);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user