Repository navigation
Conversation
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.
This branch has not been deployed
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.
Summary
Fixes silent integer overflow in
optimizer/sum_range.gowhen foldingsum(m..n),sum(m..n, pred), andreduce(m..n, # + #acc [, init])with large integer bounds.Root Cause
sumRangecomputed the arithmetic series sum ascount * (m + n) / 2in signedint. Wheneverm + norcount * (m + n)overflowsintprior 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-4611686018427387904instead of4611686018427387904.sum(4611686018427387903..4611686018427387904)(2 elements) returned-1instead of9223372036854775807(math.MaxInt).reduce(4611686018427387903..4611686018427387904, # + #acc)returned-1instead of9223372036854775807.Fix
Added
rangeSum(m, n int) (int, int)using unsigned differencediff := uint(n) - uint(m)to divide the even factor by 2 before multiplying:diffis odd (count = diff + 1is even):halfCount := (diff >> 1) + 1,sum = int(halfCount * (uint(m) + uint(n))).diffis even (m + nis 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 allm <= n(including both non-overflowing large bounds and wrapping sums) on 32-bit and 64-bit platforms.Verification
TestOptimize_sum_range_overflowinoptimizer/sum_range_test.gocomparing optimized vsexpr.Optimize(false)execution across 1-, 2-, and 3-element large positive/negative ranges, wrapping sums, predicates, andreducewith/without initial value; verified 8/10 overflow subtests fail onmasterand 10/10 pass with the fix.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)