mirror of
https://github.com/gbdev/rgbds.git
synced 2026-09-23 14:17:06 +00:00
Avoid signed overflow in RGBLINK's +, -, and * (#2060)
RGBASM computes these with unsigned arithmetic and casts back, since signed overflow is UB in C++, but RGBLINK's RPN evaluator used `int32_t` directly, so `src/link/patch.cpp` tripped UBSan on lines 117, 121, and 124. Share the three operators through `opmath.cpp` so both evaluators stay in step.
This commit is contained in:
@@ -8,6 +8,23 @@
|
||||
|
||||
#include "helpers.hpp" // assume, clz, ctz
|
||||
|
||||
// Signed overflow is UB, so these compute with unsigned arithmetic, which wraps around.
|
||||
// Casting back is OK because the types implement two's complement behavior.
|
||||
|
||||
int32_t op_add(int32_t augend, int32_t addend) {
|
||||
return static_cast<int32_t>(static_cast<uint32_t>(augend) + static_cast<uint32_t>(addend));
|
||||
}
|
||||
|
||||
int32_t op_sub(int32_t minuend, int32_t subtrahend) {
|
||||
return static_cast<int32_t>(static_cast<uint32_t>(minuend) - static_cast<uint32_t>(subtrahend));
|
||||
}
|
||||
|
||||
int32_t op_mul(int32_t multiplicand, int32_t multiplier) {
|
||||
return static_cast<int32_t>(
|
||||
static_cast<uint32_t>(multiplicand) * static_cast<uint32_t>(multiplier)
|
||||
);
|
||||
}
|
||||
|
||||
int32_t op_divide(int32_t dividend, int32_t divisor) {
|
||||
assume(divisor != 0); // Division by 0 is UB
|
||||
assume(dividend != INT32_MIN || divisor != -1); // INT32_MIN / -1 is UB
|
||||
|
||||
Reference in New Issue
Block a user