Repository navigation
Avoid signed overflow in RGBLINK's +, -, and * - #2060
Merged
Rangi42 merged 1 commit intoSep 7, 2026
Merged
Conversation
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.
Rangi42
self-requested a review
September 7, 2026 19:55
Rangi42
approved these changes
Sep 7, 2026
Rangi42
left a comment
Contributor
There was a problem hiding this comment.
Confirmed that the UBSan error exists and that this fixes it. The change looks good and matches our pre-existing code style. Nice catch, thank you!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RGBLINK's RPN evaluator computed
RPN_ADD,RPN_SUB, andRPN_MULdirectly onint32_t, atsrc/link/patch.cpplines 117, 121, and 124. Signed overflow is UB in C++, so any link-time expression whose result leaves the 32-bit signed range was undefined. RGBASM already handles this:Expression::makeBinaryOpinsrc/asm/rpn.cppcast both operands touint32_tfor exactly those three operators and cast the result back. RGBLINK never got the same treatment.The
developpreset turns on UBSan, so it shows up right away:I moved the three operators into
opmath.cppasop_add,op_sub, andop_mul, next toop_negand the others that are already there to avoid C++'s behavior, and called them from both RGBASM and RGBLINK, so the two evaluators cannot drift apart again.RPN_ADDandRPN_MULstill pop both operands in one expression, which stays safe because both operations are commutative.Values are unchanged on every platform RGBDS supports, so no existing expected output moved. The new
test/link/patch-wraparoundtest covers all three operators: its.out.binpins the wrapped results$80000000,$7FFFFFFF, and$7FFFFFFD, and its empty.outfails without the fix, because the three UBSan reports land on stderr.Built with
cmake -B build --preset developandcmake --build build -j 4on macOS 26 with Apple clang 17, then rantest/run-tests.sh --only-internal: 864 RGBASM, 115 RGBLINK, 441 RGBFIX, and 245 RGBGFX tests pass. I did not run the external-project tests.