-
Notifications
You must be signed in to change notification settings - Fork 503
Guard allocation size calculations flagged by CodeQL #55480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pelikhan
merged 7 commits into
main
from
copilot/uk-ai-resilience-fix-allocation-size-overflow
Aug 24, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
21e2dda
Initial plan
Copilot 65f1a09
Guard allocation capacity calculations
Copilot 223b133
Move allocation capacity helper to typeutil
Copilot de8aa1f
Retain allocation helper edge tests
Copilot e7f5100
Merge branch 'main' into copilot/uk-ai-resilience-fix-allocation-size…
github-actions[bot] 3d2f4a8
Address PR follow-up checks
Copilot 38b0a59
Address non-blocking review feedback
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # ADR-55480: Centralize Safe Allocation Capacity Calculation in typeutil | ||
|
|
||
| **Date**: 2026-08-24 | ||
| **Status**: Accepted | ||
| **Deciders**: Copilot | ||
|
|
||
| --- | ||
|
|
||
| ### Context | ||
|
|
||
| CodeQL flagged several `go/allocation-size-overflow` paths where summed `len(...)` values were passed directly as allocation capacity hints. If extremely large or malformed inputs caused those sums to overflow `int`, the resulting capacity could become negative or otherwise unsafe before reaching `make`. | ||
|
|
||
| The affected call sites were in multiple packages, including `pkg/workflow` and `pkg/cli`, so a package-private helper would either duplicate the overflow logic or leave future call sites without a shared convention. | ||
|
|
||
| ### Decision | ||
|
|
||
| We will provide `typeutil.SafeAllocationCapacity(parts ...int) int` as the shared helper for allocation capacity hints built from multiple integer parts. The helper returns the summed capacity when every part is non-negative and the addition does not overflow; otherwise it returns zero so callers still allocate correctly without unsafe preallocation. | ||
|
|
||
| Call sites that previously used direct additive capacity expressions will use this shared helper when summing length-derived allocation hints across packages. | ||
|
|
||
| ### Alternatives Considered | ||
|
|
||
| #### Alternative 1: Keep package-local helpers | ||
|
|
||
| Keeping separate helpers in `pkg/workflow` and `pkg/cli` avoids a new shared API, but it duplicates security-sensitive overflow handling and lets behavior drift between packages. | ||
|
|
||
| #### Alternative 2: Inline overflow checks at each allocation site | ||
|
|
||
| Inlining checks keeps each call site self-contained, but it makes the overflow policy harder to audit and increases the chance that a future allocation hint misses one of the required checks. | ||
|
|
||
| #### Alternative 3: Remove capacity hints entirely | ||
|
|
||
| Removing all summed capacity hints would also avoid overflow, but it discards useful preallocation for normal inputs and obscures the intended size relationship between the source collections and the destination allocation. | ||
|
|
||
| ### Consequences | ||
|
|
||
| #### Positive | ||
|
|
||
| - CodeQL-flagged allocation capacity calculations now use a single overflow-safe helper. | ||
| - The zero-capacity fallback preserves correctness while avoiding unsafe preallocation on overflow or negative input. | ||
| - Future callers have one reusable helper for length-derived allocation hints. | ||
|
|
||
| #### Negative | ||
|
|
||
| - `pkg/typeutil` gains a small public API that should keep its current overflow semantics stable. | ||
|
|
||
| #### Neutral | ||
|
|
||
| - Valid inputs preserve the existing capacity hint behavior. | ||
| - Overflow and negative inputs may allocate with default growth instead of the original precomputed capacity. | ||
|
|
||
| --- | ||
|
|
||
| *ADR finalized after implementation and CodeQL review.* |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package typeutil | ||
|
|
||
| import "math" | ||
|
|
||
| // SafeAllocationCapacity returns the summed capacity hint when it fits in int. | ||
| // When the total would overflow, it falls back to 0 so callers can skip | ||
| // preallocation without changing correctness. The helper is intentionally | ||
| // side-effect free so utility callers do not inherit logging dependencies. | ||
| func SafeAllocationCapacity(parts ...int) int { | ||
| total := 0 | ||
| for _, part := range parts { | ||
| if part < 0 || total > math.MaxInt-part { | ||
| return 0 | ||
| } | ||
| total += part | ||
| } | ||
| return total | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| //go:build !integration | ||
|
|
||
| package typeutil | ||
|
|
||
| import ( | ||
| "math" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestSafeAllocationCapacity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("handles zero inputs", func(t *testing.T) { | ||
| assert.Zero(t, SafeAllocationCapacity()) | ||
| assert.Zero(t, SafeAllocationCapacity(0, 0)) | ||
| assert.Equal(t, 5, SafeAllocationCapacity(0, 5)) | ||
| assert.Equal(t, 5, SafeAllocationCapacity(5, 0)) | ||
| }) | ||
|
|
||
| t.Run("sums sizes when the result fits in int", func(t *testing.T) { | ||
| assert.Equal(t, 5, SafeAllocationCapacity(2, 3)) | ||
| assert.Equal(t, 6000, SafeAllocationCapacity(1000, 5000)) | ||
| assert.Equal(t, math.MaxInt, SafeAllocationCapacity(math.MaxInt-1, 1)) | ||
| assert.Equal(t, math.MaxInt, SafeAllocationCapacity(math.MaxInt-2, 1, 1)) | ||
| }) | ||
|
|
||
| t.Run("returns zero when the sum would overflow int", func(t *testing.T) { | ||
| assert.Zero(t, SafeAllocationCapacity(math.MaxInt, 1)) | ||
| assert.Zero(t, SafeAllocationCapacity(math.MaxInt-1, 2)) | ||
| assert.Zero(t, SafeAllocationCapacity(math.MaxInt-2, 2, 1)) | ||
| }) | ||
|
|
||
| t.Run("returns zero for negative parts", func(t *testing.T) { | ||
| assert.Zero(t, SafeAllocationCapacity(-1)) | ||
| assert.Zero(t, SafeAllocationCapacity(2, -1)) | ||
| }) | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/typeutil/allocation.go:L8: yagni: exported varargs helper for a single overflow-check pattern. Inline the small sum/overflow guard in the few sites that need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept the shared helper because the maintainer requested moving this overflow guard into a helper package, and the pattern now has multiple call sites. Added ADR-55480 to document the decision and tradeoff.