Skip to content

fix: remove capacity pre-allocation in mergeFetchRefs to avoid overflow (CodeQL #555)#22774

Merged
pelikhan merged 3 commits into
mainfrom
copilot/fix-code-scanning-alert-555
Mar 24, 2026
Merged

fix: remove capacity pre-allocation in mergeFetchRefs to avoid overflow (CodeQL #555)#22774
pelikhan merged 3 commits into
mainfrom
copilot/fix-code-scanning-alert-555

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 24, 2026

mergeFetchRefs used len(existing)+len(newRefs) directly as a make capacity argument. On pathologically large inputs this addition overflows int, producing a negative capacity and a runtime panic.

Change

Removed the capacity pre-allocation entirely:

// before
result := make([]string, 0, len(existing)+len(newRefs))

// after
result := make([]string, 0)

append grows 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.

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
Copilot AI requested a review from pelikhan March 24, 2026 21:56
// 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))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot do not pre-allocate size

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 6c2f7df — replaced make([]string, 0, capacity) with make([]string, 0) and removed the math import.

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
Copilot AI requested a review from pelikhan March 24, 2026 22:06
@pelikhan pelikhan marked this pull request as ready for review March 24, 2026 22:15
Copilot AI review requested due to automatic review settings March 24, 2026 22:15
@pelikhan pelikhan merged commit 030787c into main Mar 24, 2026
72 of 73 checks passed
@pelikhan pelikhan deleted the copilot/fix-code-scanning-alert-555 branch March 24, 2026 22:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) from make(..., cap) in mergeFetchRefs to prevent int overflow.
  • Rely on append to 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)
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copilot uses AI. Check for mistakes.
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.

3 participants