Skip to content

optimizer: avoid intermediate overflow in sumRange arithmetic series - #1035

Open
xgbtxy wants to merge 1 commit into
expr-lang:masterfrom
xgbtxy:fix/optimizer-sum-range-overflow
Open

xgbtxy wants to merge 1 commit into
expr-lang:masterfrom
xgbtxy:fix/optimizer-sum-range-overflow

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Nov 27, 2026

Copy link
Copy Markdown

Summary

Fixes silent integer overflow in optimizer/sum_range.go when folding sum(m..n), sum(m..n, pred), and reduce(m..n, # + #acc [, init]) with large integer bounds.

Root Cause

sumRange computed the arithmetic series sum as count * (m + n) / 2 in signed int. Whenever m + n or count * (m + n) overflows int prior to / 2, dividing after wraparound drops the high carry bit and produces a wrong constant that disagrees with unoptimized execution (expr.Optimize(false)).

For example, on 64-bit platforms:

  • sum(4611686018427387904..4611686018427387904) (1 element) returned -4611686018427387904 instead of 4611686018427387904.
  • sum(4611686018427387903..4611686018427387904) (2 elements) returned -1 instead of 9223372036854775807 (math.MaxInt).
  • reduce(4611686018427387903..4611686018427387904, # + #acc) returned -1 instead of 9223372036854775807.

Fix

Added rangeSum(m, n int) (int, int) using unsigned difference diff := uint(n) - uint(m) to divide the even factor by 2 before multiplying:

  • When diff is odd (count = diff + 1 is even): halfCount := (diff >> 1) + 1, sum = int(halfCount * (uint(m) + uint(n))).
  • When diff is even (m + n is even): halfSum := uint(m) + (diff >> 1), sum = int((diff + 1) * halfSum).

Because multiplication and addition preserve congruence modulo 2^bits.UintSize, the folded constant matches unoptimized runtime addition bit-for-bit across all m <= n (including both non-overflowing large bounds and wrapping sums) on 32-bit and 64-bit platforms.

Verification

  • Added TestOptimize_sum_range_overflow in optimizer/sum_range_test.go comparing optimized vs expr.Optimize(false) execution across 1-, 2-, and 3-element large positive/negative ranges, wrapping sums, predicates, and reduce with/without initial value; verified 8/10 overflow subtests fail on master and 10/10 pass with the fix.
  • Ran:
    • gofmt -l optimizer/sum_range.go optimizer/sum_range_test.go (clean)
    • go vet ./... (clean)
    • go test -v -race -count=1 -run TestOptimize_sum_range ./optimizer (pass)
    • go test -race -count=1 ./... (pass)
    • go test -tags=expr_debug -run=TestDebugger -v ./vm (pass)

sumRange folded sum(m..n), sum(m..n, pred), and reduce(m..n, # + #acc)
with count * (m + n) / 2 in signed int. When m + n or count * (m + n)
overflows before dividing by 2, the high carry bit is lost and the
folded constant diverges from unoptimized evaluation (for example,
sum(4611686018427387903..4611686018427387904) returned -1 instead of
math.MaxInt64, and even single-element sum(4611686018427387904..4611686018427387904)
returned -4611686018427387904).

Divide the even factor by 2 using unsigned differences before
multiplying so the folded result matches two's-complement runtime
addition across all m <= n.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 06:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

2 participants