Repository navigation
fix(interpreter): guard INT_MIN / -1 signed-overflow crash in division and modulo - #16
Merged
zombocoder merged 1 commit intoOct 26, 2026
Conversation
…n and modulo Integer division `INT_MIN / -1` (and the 128-bit `LONG_MIN / -1`) overflows the signed range: the result is not representable, which is undefined behavior in C++ and raises SIGFPE on x86, aborting the interpreter with a core dump. The divide/modulo paths in BinaryOpNode only guarded the zero-divisor case. Add the missing guard next to the existing divide-by-zero check: division by -1 at the type minimum now throws a catchable EvaluationError, and `x % -1` (which is mathematically 0 for every x but undefined at the minimum) returns 0. Covered by new IntegrationTest cases and a runnable regression example (examples/test_int_min_div_overflow.obq). Signed-off-by: svaiml <88979468+svaiml@users.noreply.github.com>
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.
Problem
Dividing the minimum
Int(64-bit) orLong(128-bit) by-1overflows the signed range. In C++ thatis undefined behavior, and on x86 the CPU traps, so the interpreter process dies instead of reporting an
evaluation error.
BinaryOpNodeguards division by zero but not this case.x % -1hits the same trapwhen
xis the minimum, although its mathematical result is simply0.Reproduced on current
main(Windows 10, MSVC 2022, Release): running the example added here(
examples/test_int_min_div_overflow.obq) with the unpatched interpreter terminates with exit code0xC0000095(STATUS_INTEGER_OVERFLOW) and no output. On Linux and macOS the same trap isSIGFPE.Fix
In the
/and%paths ofBinaryOpNode, next to the existing divide-by-zero check, for bothIntandLong:MIN / -1throws a catchableEvaluationError: "Integer overflow in division: minimum value dividedby -1".
x % -1returns0without performing the faulting division.Every other division and modulo, including ordinary division by
-1, is unchanged.Tests
IntegrationTest.IntMinDivideByNegativeOneIsCaughtIntegrationTest.IntMinModuloNegativeOneIsZeroIntegrationTest.DivideAndModuloByNegativeOneStillWorkexamples/test_int_min_div_overflow.obq:try/catcharoundmin / -1, plus the%case and ordinary/ -1and% -1[Unreleased],FixedVerification
o2l_tests512/512 passing.0with the fix and0xC0000095without it.clang-format(repository.clang-format) reports nothing on the changed lines.