fix: remove capacity pre-allocation in mergeFetchRefs to avoid overflow (CodeQL #555)#22774
Merged
Conversation
#555) Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Agent-Logs-Url: https://github.com/github/gh-aw/sessions/08ecb67e-e16b-48ba-9a20-73856514d5bd
Copilot
AI
changed the title
[WIP] Fix code scanning alerts flagged in the repository
fix: guard integer overflow in mergeFetchRefs allocation (CodeQL #555)
Mar 24, 2026
pelikhan
reviewed
Mar 24, 2026
| // mergeFetchRefs unions two sets of fetch ref patterns preserving insertion order. | ||
| func mergeFetchRefs(existing []string, newRefs []string) []string { | ||
| seen := make(map[string]bool, len(existing)) | ||
| result := make([]string, 0, len(existing)+len(newRefs)) |
Closed
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Agent-Logs-Url: https://github.com/github/gh-aw/sessions/edc80db7-def1-413a-b5f4-54788eeddd86
Copilot
AI
changed the title
fix: guard integer overflow in mergeFetchRefs allocation (CodeQL #555)
fix: remove capacity pre-allocation in mergeFetchRefs to avoid overflow (CodeQL #555)
Mar 24, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Removes slice capacity pre-allocation in mergeFetchRefs to eliminate an int overflow risk that could cause a runtime panic on extreme input sizes (CodeQL #555).
Changes:
- Remove
len(existing)+len(newRefs)frommake(..., cap)inmergeFetchRefsto preventintoverflow. - Rely on
appendto grow the result slice dynamically.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func mergeFetchRefs(existing []string, newRefs []string) []string { | ||
| seen := make(map[string]bool, len(existing)) | ||
| result := make([]string, 0, len(existing)+len(newRefs)) | ||
| result := make([]string, 0) |
There was a problem hiding this comment.
Dropping pre-allocation entirely fixes the overflow, but it also guarantees extra realloc/copy work for typical inputs. You can keep the fix and still reduce allocations by preallocating to len(existing) (safe since it doesn’t involve an addition) or by using an overflow-checked sum before setting capacity.
Suggested change
| result := make([]string, 0) | |
| result := make([]string, 0, len(existing)) |
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.
mergeFetchRefsusedlen(existing)+len(newRefs)directly as amakecapacity argument. On pathologically large inputs this addition overflowsint, producing a negative capacity and a runtime panic.Change
Removed the capacity pre-allocation entirely:
appendgrows the slice as needed, eliminating the overflow risk without any guard logic.💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.