Skip to content

Fix gh skill search panic when page or limit overflows - #15007

Closed
shounakjoshi88-a11y wants to merge 2 commits into
cli:trunkfrom
shounakjoshi88-a11y:fix/skill-search-pagination-overflow
Closed

shounakjoshi88-a11y wants to merge 2 commits into
cli:trunkfrom
shounakjoshi88-a11y:fix/skill-search-pagination-overflow

Conversation

@shounakjoshi88-a11y

Copy link
Copy Markdown

Fixes #14985.

gh skill search panicked with slice bounds out of range when --page or --limit were given values large enough to overflow int. The page and limit values are multiplied to compute slice bounds in three places:

  • truncateForProcessing: page * limit * 3
  • paginate: (page - 1) * limit
  • fetchPrimaryPages: displayPage * displayLimit * 3

An overflowing product wraps around to a negative number, which was then used as a slice bound:

$ gh skill search terraform --page 9223372036854775807 --limit 2
panic: runtime error: slice bounds out of range [:-6]

The fix

  • A new checkedProduct helper multiplies non-negative values and reports whether the result overflows int.
  • The three call sites use it: truncateForProcessing keeps every result when the working set does not fit in an int, paginate treats an unrepresentable start index as past the end of the result set, and fetchPrimaryPages falls back to fetching the maximum number of API pages.
  • paginate also computes the page count and the end index without overflowing.
  • The command rejects a page and limit combination that would overflow before doing any work, instead of failing later with a panic:
$ gh skill search terraform --page 9223372036854775807 --limit 2
invalid page and limit: page 9223372036854775807 with limit 2 is out of range

Tests

  • TestNewCmdSearch covers the three reported combinations: a large --page, a large --limit, and the --page/--limit pair that overflows the second calculation.
  • TestTruncateForProcessing_Overflow and TestPaginate_Overflow cover the helpers directly.

go build ./... is clean and go test ./pkg/cmd/skills/... passes.

@shounakjoshi88-a11y
shounakjoshi88-a11y requested review from a team as code owners November 10, 2026 04:49
@shounakjoshi88-a11y
shounakjoshi88-a11y requested review from tidy-dev and a balanced review from Copilot November 10, 2026 04:49
@github-actions github-actions Bot added external pull request originating outside of the CLI core team needs-triage needs to be reviewed unmet-requirements and removed needs-triage needs to be reviewed labels Nov 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your pull request! Unfortunately, it doesn't meet the requirements for review:

  • None of the referenced issues have the help wanted label

Please update your PR to address the above. This PR will be automatically closed in 4 days if these requirements are not met.

Full contribution requirements
  1. Include a detailed description of what this PR does
  2. Link to an issue with the help wanted label (use Fixes #123 or Closes #123)

@shounakjoshi88-a11y shounakjoshi88-a11y changed the title Fix \gh skill search\ panic when page or limit overflows Fix gh skill search panic when page or limit overflows Nov 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

API-page rounding can still overflow and silently omit results, while new regression tests fail validation or compilation on supported 32-bit targets.

3 open issues
What changed in this PR

This PR addresses #14985 by guarding pagination arithmetic in gh skill search against integer overflow, replacing reported panics with an early validation error.

Changes:

  • Adds checked multiplication for command validation, result truncation, pagination, and API-page calculations.
  • Reworks pagination page-count and end-index calculations to avoid overflow.
  • Adds command and helper regression tests for large page and limit values.
File Description
pkg/​cmd/​skills/​search/​search.go Adds overflow validation and guards pagination arithmetic.
pkg/​cmd/​skills/​search/​search_test.go Adds regression coverage for overflowing page and limit inputs.

Review effort: Balanced

Comment thread pkg/cmd/skills/search/search_test.go Outdated
Comment on lines +105 to +106
args: "terraform --page 9223372036854775807 --limit 2",
wantErr: "invalid page and limit: page 9223372036854775807 with limit 2 is out of range",

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch - those literals do not fit in a 32-bit int. Fixed in 25a8054: the three command cases now build the --page/--limit values from math.MaxInt at runtime and derive the expected message with fmt.Sprintf, so they overflow on 32-bit targets too and the file compiles everywhere.

Comment thread pkg/cmd/skills/search/search_test.go Outdated
Comment on lines +743 to +749
assert.Len(t, truncateForProcessing(skills, 2026277576509488133, 7), 5)
}

func TestPaginate_Overflow(t *testing.T) {
skills := make([]skillResult, 5)

got, totalPages := paginate(skills, 2026277576509488133, 7)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch - those literals do not fit in a 32-bit int. Fixed in 25a8054: the helper tests now use math.MaxInt instead of a 64-bit-only literal. go vet with GOARCH=386 is clean.

Comment thread pkg/cmd/skills/search/search.go Outdated
needed, overflowed := checkedProduct(displayPage, displayLimit, 3)
numPages := maxAPIPages
if !overflowed {
numPages = max((needed+searchPageSize-1)/searchPageSize, 1)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch - the ceiling division could still overflow even when the product itself fits. Fixed in 25a8054: the page count now comes from a new apiPageCount helper that computes the ceiling as (needed-1)/searchPageSize+1, which cannot overflow, and TestAPIPageCount covers the math.MaxInt/3 boundary case (the value that overflows in int64 on a 64-bit target overflows in int32 on a 32-bit target).

@shounakjoshi88-a11y

Copy link
Copy Markdown
Author

The CI workflows on this pull request (Unit and Integration Tests, Lint, Code Scanning) are sitting in �ction_required, which I believe needs a maintainer to approve them before they can run on a fork PR. Could someone approve them when convenient?

Both commits are green locally: go build ./... is clean, go test ./pkg/cmd/skills/... passes, gofmt reports no diffs, and go vet is clean for GOARCH=386 as well as the host architecture.

@shounakjoshi88-a11y

Copy link
Copy Markdown
Author

The PR triage bot requires the linked issue to have the help wanted label, and #14985 does not carry it yet. I have asked on the issue for the label to be added - if it is easier to add the label directly, that would unblock the review.

@shounakjoshi88-a11y

Copy link
Copy Markdown
Author

The fix for this is ready and the branch is still up (fix/skill-search-pagination-overflow on my fork), so I would like to get this one reviewed.

The only missing piece is the PR triage checklist: the linked issue #14985 does not carry the help wanted label, so the bot closed this pull request. I have asked on the issue for the label to be added.

For the record, the change is:

  • pkg/cmd/skills/search/search.go: a new checkedProduct helper multiplies the page and limit values and reports whether the result overflows int. The three places that multiply them (truncateForProcessing, paginate, fetchPrimaryPages) use it, so an overflowing value can no longer wrap around into a negative slice bound.
  • The command now rejects an overflowing page and limit combination up front, with invalid page and limit: page %d with limit %d is out of range, instead of panicking later.
  • paginate also computes the page count and the end index without overflowing.
  • Tests: three cases in TestNewCmdSearch for the reported combinations (large --page, large --limit, and the --page/--limit pair that overflows the second calculation), plus TestTruncateForProcessing_Overflow, TestPaginate_Overflow and TestAPIPageCount.

go build ./... is clean, go test ./pkg/cmd/skills/... passes, and go vet is clean for GOARCH=386 as well as the host architecture.

If adding the label to #14985 is not the right move, I am happy to open a fresh pull request once it is there - the branch is already pushed. And if the approach itself needs changing, please say so and I will rework it.

AI tools were used to assist with the investigation and the initial patch; I reviewed and verified every line, and the test suite is run locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team unmet-requirements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh skill search panics with a slice bounds error for very large --page or --limit values

3 participants