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:
Max Freedom Pollard
2026-09-07 16:15:07 -04:00
committed by GitHub
parent fdd6cece30
commit 631ef003e7
7 changed files with 35 additions and 7 deletions
+3 -4
View File
@@ -297,7 +297,6 @@ void Expression::makeBinaryOp(RPNCommand op, Expression &&src1, Expression const
if (src1.isKnown() && src2.isKnown()) {
// If both expressions are known, just compute the value
int32_t lval = src1.value(), rval = src2.value();
uint32_t ulval = static_cast<uint32_t>(lval), urval = static_cast<uint32_t>(rval);
switch (op) {
case RPN_LOGOR:
@@ -325,10 +324,10 @@ void Expression::makeBinaryOp(RPNCommand op, Expression &&src1, Expression const
data = lval != rval;
break;
case RPN_ADD:
data = static_cast<int32_t>(ulval + urval);
data = op_add(lval, rval);
break;
case RPN_SUB:
data = static_cast<int32_t>(ulval - urval);
data = op_sub(lval, rval);
break;
case RPN_XOR:
data = lval ^ rval;
@@ -370,7 +369,7 @@ void Expression::makeBinaryOp(RPNCommand op, Expression &&src1, Expression const
data = op_shift_right_unsigned(lval, rval);
break;
case RPN_MUL:
data = static_cast<int32_t>(ulval * urval);
data = op_mul(lval, rval);
break;
case RPN_DIV:
if (rval == 0) {