Skip to content

fix: add integer overflow checks for arithmetic operations - #2482

Open
mmustafasenoglu wants to merge 2 commits into
apache:masterfrom
mmustafasenoglu:fix/integer-overflow-checks
Open

fix: add integer overflow checks for arithmetic operations#2482
mmustafasenoglu wants to merge 2 commits into
apache:masterfrom
mmustafasenoglu:fix/integer-overflow-checks

Conversation

@mmustafasenoglu

Copy link
Copy Markdown
Contributor

Summary

Adds overflow/underflow detection for 64-bit integer arithmetic in agtype operations.

Problem

Integer arithmetic silently wraps around on overflow instead of raising an error:

Both PostgreSQL and Neo4j reject these with errors.

Fix

Uses GCC/Clang intrinsics to detect overflow in:

  • (+)
  • (-)
  • (*)
  • (unary -)
  • (/)
  • (%)

Each raises on overflow.

Tests

Added 6 regression tests covering each overflow case.

Fixes #2471

Add overflow/underflow detection for 64-bit integer arithmetic in
agtype_add, agtype_sub, agtype_mul, agtype_neg, agtype_div, and
agtype_mod operations.

Previously, operations like 9223372036854775807 + 1 silently wrapped
around to -9223372036854775808 instead of raising an error. This
matches Neo4j and PostgreSQL behavior which reject integer overflow.

Fixes apache#2471

Signed-off-by: Mustafa Senoglu <mmustafasenoglu0@gmail.com>
@mmustafasenoglu
mmustafasenoglu force-pushed the fix/integer-overflow-checks branch from 7e5450e to f62d6ab Compare July 29, 2026 12:37
…rflow

The previous check compared float_value == (double)PG_INT64_MIN,
but the parser stores 9223372036854775808 as a positive float
(AGTV_FLOAT), so the comparison with the negative PG_INT64_MIN
was always false.

Fix: compare float_value == -(double)PG_INT64_MIN to match the
positive float representation.

Also adds division and modulo overflow checks for INT64_MIN / -1.

Signed-off-by: Mustafa Senoglu <mmustafasenoglu0@gmail.com>
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.

64-bit integer arithmetic overflow silently wraps around instead of failing

1 participant