Skip to content

Reject overflow while summing array elements - #908

Open
vinsymoyya wants to merge 2 commits into
google:masterfrom
vinsymoyya:fix/sum-overflow
Open

vinsymoyya wants to merge 2 commits into
google:masterfrom
vinsymoyya:fix/sum-overflow

Conversation

@vinsymoyya

@vinsymoyya vinsymoyya commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #905.

Reject an infinite accumulator immediately in the native std.sum, matching the checked addition in the standard library's left-fold implementation. Previously std.sum([1e308, 1e308]) > 0 returned true without a runtime error, and std.avg inherited the overflow.

Dedicated tests in builtin_sum_test.go, registered in the Bazel test target, cover positive and negative overflow, average, a later cancelling value, further comparison, and stopping before a later element is evaluated. Finite, empty, cancelling, and average results are also checked. The original implementation fails all five initially added overflow cases; the fixed regression suite passes.

Validation:

  • Go 1.27.4 Windows/amd64: go test . -run 'TestSumRejectsOverflow|TestSumFinite' -count=1 passes.
  • Go 1.24.13 Linux/amd64, from an immutable archive of this commit: go test -buildvcs=false ./... passes.
  • gofmt and git diff --check pass.

The native Windows broad run encounters pre-existing golden-path separator mismatches (testdata\ versus testdata/); the full Go suite passes in Linux. Bazel and the C/Python-binding integration suite have not been run locally.

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.

Native std.sum silently propagates floating-point overflow

1 participant