fix(diff): bound the Myers n+m sum so it can't overflow (CodeQL)#3773
Merged
Conversation
CodeQL go/allocation-size-overflow (high) flagged `n + m` flowing into `make([]int, 2*maxD+1)`: a pathological line count could overflow the addition before the downstream clamp. The result was already safe (the clamp caught a wrapped-negative maxD), but the addition itself still tripped the query. Restructure to maxD = min(n+m, maxDiffEdits) with the sum taken only when n and m are within the cap — no overflow at the operation, identical bound.
| // maxD = min(n+m, maxDiffEdits); take the sum only when it can't overflow int. | ||
| maxD := maxDiffEdits // bound the trace's O(D²) footprint | ||
| if n <= maxDiffEdits && m <= maxDiffEdits-n { | ||
| maxD = n + m |
esengine
added a commit
that referenced
this pull request
Jun 10, 2026
…tion (#3775) Follow-up to #3773: that cleared the n+m addition alert, but CodeQL still flagged the 2*maxD allocation (go/allocation-size-overflow) because it doesn't propagate maxD's bound through the guarded sum. Clamp each line count to maxDiffEdits before summing, so the sum and the 2*maxD allocation both have constant-bounded operands. maxD stays min(n+m, maxDiffEdits). Co-authored-by: reasonix <reasonix@deepseek.com>
SuMuxi66
pushed a commit
to SuMuxi66/DeepSeek-Reasonix
that referenced
this pull request
Jun 10, 2026
…sengine#3773) CodeQL go/allocation-size-overflow (high) flagged `n + m` flowing into `make([]int, 2*maxD+1)`: a pathological line count could overflow the addition before the downstream clamp. The result was already safe (the clamp caught a wrapped-negative maxD), but the addition itself still tripped the query. Restructure to maxD = min(n+m, maxDiffEdits) with the sum taken only when n and m are within the cap — no overflow at the operation, identical bound. Co-authored-by: reasonix <reasonix@deepseek.com>
SuMuxi66
pushed a commit
to SuMuxi66/DeepSeek-Reasonix
that referenced
this pull request
Jun 10, 2026
…tion (esengine#3775) Follow-up to esengine#3773: that cleared the n+m addition alert, but CodeQL still flagged the 2*maxD allocation (go/allocation-size-overflow) because it doesn't propagate maxD's bound through the guarded sum. Clamp each line count to maxDiffEdits before summing, so the sum and the 2*maxD allocation both have constant-bounded operands. maxD stays min(n+m, maxDiffEdits). Co-authored-by: reasonix <reasonix@deepseek.com>
dorokuma
pushed a commit
to dorokuma/DeepSeek-Reasonix
that referenced
this pull request
Jun 10, 2026
…sengine#3773) CodeQL go/allocation-size-overflow (high) flagged `n + m` flowing into `make([]int, 2*maxD+1)`: a pathological line count could overflow the addition before the downstream clamp. The result was already safe (the clamp caught a wrapped-negative maxD), but the addition itself still tripped the query. Restructure to maxD = min(n+m, maxDiffEdits) with the sum taken only when n and m are within the cap — no overflow at the operation, identical bound. Co-authored-by: reasonix <reasonix@deepseek.com>
dorokuma
pushed a commit
to dorokuma/DeepSeek-Reasonix
that referenced
this pull request
Jun 10, 2026
…tion (esengine#3775) Follow-up to esengine#3773: that cleared the n+m addition alert, but CodeQL still flagged the 2*maxD allocation (go/allocation-size-overflow) because it doesn't propagate maxD's bound through the guarded sum. Clamp each line count to maxDiffEdits before summing, so the sum and the 2*maxD allocation both have constant-bounded operands. maxD stays min(n+m, maxDiffEdits). Co-authored-by: reasonix <reasonix@deepseek.com>
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.
Resolves the two open CodeQL
go/allocation-size-overflow(high) alerts ininternal/diff/diff.go(the Myers line-diff).maxD := n + mflows intomake([]int, 2*maxD+1). The value was already safe at the allocation —maxDis clamped tomaxDiffEdits(2000), and a prior fix added amaxD < 0guard for a wrapped sum — but CodeQL flags the addition itself as a potential overflow, which the post-hoc clamp doesn't clear.This restructures to
maxD = min(n+m, maxDiffEdits), taking the sum only whenn <= maxDiffEdits && m <= maxDiffEdits-n, son + mis evaluated only when provably within the cap. Identical bound, no overflow at the operation.In practice the inputs are file line counts, so the sum could never overflow a real
intanyway (you'd OOM building the slices first) — but this clears the alert cleanly rather than dismissing it.go build+go test ./internal/diff/pass locally.