mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 02:32:06 +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:
@@ -66,7 +66,7 @@ void constexpr_UnaryOp(struct ConstExpression *expr,
|
||||
result = value;
|
||||
break;
|
||||
case T_OP_SUB:
|
||||
result = -value;
|
||||
result = -(uint32_t)value;
|
||||
break;
|
||||
case T_OP_NOT:
|
||||
result = ~value;
|
||||
@@ -155,10 +155,10 @@ void constexpr_BinaryOp(struct ConstExpression *expr,
|
||||
result = value1 != value2;
|
||||
break;
|
||||
case T_OP_ADD:
|
||||
result = value1 + value2;
|
||||
result = (uint32_t)value1 + (uint32_t)value2;
|
||||
break;
|
||||
case T_OP_SUB:
|
||||
result = value1 - value2;
|
||||
result = (uint32_t)value1 - (uint32_t)value2;
|
||||
break;
|
||||
case T_OP_XOR:
|
||||
result = value1 ^ value2;
|
||||
@@ -181,7 +181,7 @@ void constexpr_BinaryOp(struct ConstExpression *expr,
|
||||
fatalerror("Shift by too big value: %d",
|
||||
value2);
|
||||
|
||||
result = value1 << value2;
|
||||
result = (uint32_t)value1 << value2;
|
||||
break;
|
||||
case T_OP_SHR:
|
||||
if (value2 < 0)
|
||||
@@ -194,17 +194,25 @@ void constexpr_BinaryOp(struct ConstExpression *expr,
|
||||
result = value1 >> value2;
|
||||
break;
|
||||
case T_OP_MUL:
|
||||
result = value1 * value2;
|
||||
result = (uint32_t)value1 * (uint32_t)value2;
|
||||
break;
|
||||
case T_OP_DIV:
|
||||
if (value2 == 0)
|
||||
fatalerror("Division by zero");
|
||||
result = value1 / value2;
|
||||
if (value1 == INT32_MIN && value2 == -1) {
|
||||
warning("Division of min value by -1");
|
||||
result = INT32_MIN;
|
||||
} else {
|
||||
result = value1 / value2;
|
||||
}
|
||||
break;
|
||||
case T_OP_MOD:
|
||||
if (value2 == 0)
|
||||
fatalerror("Division by zero");
|
||||
result = value1 % value2;
|
||||
if (value1 == INT32_MIN && value2 == -1)
|
||||
result = 0;
|
||||
else
|
||||
result = value1 % value2;
|
||||
break;
|
||||
case T_OP_FDIV:
|
||||
result = math_Div(value1, value2);
|
||||
|
||||
Reference in New Issue
Block a user