Skip to content

fix(diff): bound the Myers n+m sum so it can't overflow (CodeQL)#3773

Merged
esengine merged 1 commit into
main-v2from
fix/diff-alloc-overflow
Jun 10, 2026
Merged

fix(diff): bound the Myers n+m sum so it can't overflow (CodeQL)#3773
esengine merged 1 commit into
main-v2from
fix/diff-alloc-overflow

Conversation

@esengine

Copy link
Copy Markdown
Owner

Resolves the two open CodeQL go/allocation-size-overflow (high) alerts in internal/diff/diff.go (the Myers line-diff).

maxD := n + m flows into make([]int, 2*maxD+1). The value was already safe at the allocation — maxD is clamped to maxDiffEdits (2000), and a prior fix added a maxD < 0 guard 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 when n <= maxDiffEdits && m <= maxDiffEdits-n, so n + m is 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 int anyway (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.

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.
@esengine esengine requested a review from SivanCola as a code owner June 10, 2026 03:39
@github-actions github-actions Bot added the v2 Go rewrite (1.x) — main-v2 branch, active development label Jun 10, 2026
Comment thread internal/diff/diff.go
// 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 esengine merged commit 1fe5f2c into main-v2 Jun 10, 2026
13 of 14 checks passed
@esengine esengine deleted the fix/diff-alloc-overflow branch June 10, 2026 03:43
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Go rewrite (1.x) — main-v2 branch, active development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants