Skip to content

fix(interpreter): guard INT_MIN / -1 signed-overflow crash in division and modulo - #16

Merged
zombocoder merged 1 commit into
zombocoder:mainfrom
Craftegy-NextGen:fix/int-min-div-overflow
Oct 26, 2026
Merged

zombocoder merged 1 commit into
zombocoder:mainfrom
Craftegy-NextGen:fix/int-min-div-overflow

Conversation

@sashml

@sashml sashml commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Problem

Dividing the minimum Int (64-bit) or Long (128-bit) by -1 overflows the signed range. In C++ that
is undefined behavior, and on x86 the CPU traps, so the interpreter process dies instead of reporting an
evaluation error. BinaryOpNode guards division by zero but not this case. x % -1 hits the same trap
when x is the minimum, although its mathematical result is simply 0.

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 code
0xC0000095 (STATUS_INTEGER_OVERFLOW) and no output. On Linux and macOS the same trap is SIGFPE.

Fix

In the / and % paths of BinaryOpNode, next to the existing divide-by-zero check, for both Int and
Long:

  • MIN / -1 throws a catchable EvaluationError: "Integer overflow in division: minimum value divided
    by -1".
  • x % -1 returns 0 without performing the faulting division.

Every other division and modulo, including ordinary division by -1, is unchanged.

Tests

  • IntegrationTest.IntMinDivideByNegativeOneIsCaught
  • IntegrationTest.IntMinModuloNegativeOneIsZero
  • IntegrationTest.DivideAndModuloByNegativeOneStillWork
  • Runnable example examples/test_int_min_div_overflow.obq: try/catch around min / -1, plus the
    % case and ordinary / -1 and % -1
  • CHANGELOG entry under [Unreleased], Fixed

Verification

  • Windows 10, MSVC 2022, Release, Ninja, GoogleTest from vcpkg: build OK, o2l_tests 512/512 passing.
  • The example exits 0 with the fix and 0xC0000095 without it.
  • Linux and macOS: covered by CI.
  • clang-format (repository .clang-format) reports nothing on the changed lines.

…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>
@zombocoder
zombocoder merged commit 385eb46 into zombocoder:main Oct 26, 2026
6 checks passed
@sashml
sashml deleted the fix/int-min-div-overflow branch October 27, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants