Skip to content

Conversation

sleeg00
Copy link

@sleeg00 sleeg00 commented Jul 30, 2025

Description

Closes: #25006

This PR fixes an integer overflow in the RPC pagination logic that was causing empty results when offset + limit exceeded the maximum uint64.

  • A new helper ClampPageRequestLimit(offset, limit) uint64 that caps limit at MaxUint64 - offset.
  • Integration of ClampPageRequestLimit directly into Paginate, so every pagination call now applies the clamp before slicing.
  • A unit test in types/query/pagination_test.go (TestClampPageRequestLimit) to verify that overly large limits are correctly reduced.

Tests

  • Ran go test ./types/query locally; all existing tests pass.
  • Added and passed TestClampPageRequestLimit covering the new clamp behavior.

Summary by CodeRabbit

  • Bug Fixes

    • Improved pagination to prevent issues when very large limits are requested, ensuring safer and more predictable page results.
  • Tests

    • Added tests to verify that pagination limits are correctly clamped to avoid overflow errors.

Copy link
Contributor

coderabbitai bot commented Jul 30, 2025

📝 Walkthrough

Walkthrough

A new function, ClampPageRequestLimit, is introduced in the pagination logic to ensure that the sum of Offset and Limit in a PageRequest does not exceed the maximum value for a uint64. The Paginate function is updated to invoke this clamping function. Corresponding unit tests are added to verify the overflow handling.

Changes

Cohort / File(s) Change Summary
Pagination Limit Clamping Logic
types/query/pagination.go
Adds ClampPageRequestLimit to clamp Limit in PageRequest to prevent Offset + Limit uint64 overflow; updates Paginate to use this function.
Pagination Overflow Test
types/query/pagination_test.go
Adds TestClampOverflow to test that ClampPageRequestLimit correctly clamps the limit to avoid overflow cases.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Paginate
    participant ClampPageRequestLimit

    Caller->>Paginate: Call Paginate(pageRequest, ...)
    Paginate->>ClampPageRequestLimit: ClampPageRequestLimit(pageRequest)
    ClampPageRequestLimit-->>Paginate: returns clamped pageRequest
    Paginate->>Paginate: Continue pagination logic with clamped values
    Paginate-->>Caller: Return paginated results
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Prevent integer overflow in pagination by ensuring offset + limit does not exceed uint64 max (#25006)
Clamp limit to maximum allowed value minus offset if overflow would occur (#25006)
Add input validation for pagination parameters to avoid overflow-induced empty results (#25006)
Add tests verifying correct clamping and prevention of overflow in pagination (#25006)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected. All modifications directly address the overflow handling and validation requirements described in the linked issue.

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
types/query/pagination_test.go (1)

379-391: Good test coverage for the overflow scenario.

The test correctly verifies that when Offset + Limit would overflow uint64, the limit is properly clamped to PaginationMaxLimit - Offset. The test setup with Offset: 5 and Limit: ^uint64(0) (MaxUint64) creates a clear overflow condition and validates the fix.

Consider adding a few more test cases for comprehensive coverage:

+func (s *paginationTestSuite) TestClampOverflowEdgeCases() {
+	s.T().Log("verify no clamping when no overflow would occur")
+	pageReq := &query.PageRequest{
+		Offset: 10,
+		Limit:  100,
+	}
+	clamped := query.ClampPageRequestLimit(pageReq)
+	s.Require().Equal(uint64(100), clamped.Limit)
+
+	s.T().Log("verify nil input handling")
+	clamped = query.ClampPageRequestLimit(nil)
+	s.Require().Equal(uint64(0), clamped.Offset)
+	s.Require().Equal(uint64(0), clamped.Limit)
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Knowledge Base: Disabled due to Reviews > Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 892f4d8 and b075a82.

📒 Files selected for processing (2)
  • types/query/pagination.go (2 hunks)
  • types/query/pagination_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Summary
🔇 Additional comments (2)
types/query/pagination.go (2)

163-174: LGTM! Mathematically sound overflow prevention.

The ClampPageRequestLimit function correctly prevents uint64 overflow by checking if Limit > PaginationMaxLimit - Offset and clamping the limit accordingly. The implementation properly:

  • Handles nil input gracefully
  • Creates a copy to avoid mutation
  • Uses correct overflow detection logic
  • Sets the safe maximum limit

58-58: Proper integration of overflow protection.

The placement of ClampPageRequestLimit after initPageRequestDefaults is correct, ensuring defaults are established before applying overflow protection.

@aljo242 aljo242 enabled auto-merge August 12, 2025 21:20
auto-merge was automatically disabled August 13, 2025 02:27

Head branch was pushed to by a user without write access

@sleeg00
Copy link
Author

sleeg00 commented Aug 13, 2025

Hi @aljo242, I've just pushed the missing CHANGELOG.md entry that was causing the previous CI check to fail. Would it be possible for you to try merging it again?

@sleeg00
Copy link
Author

sleeg00 commented Sep 3, 2025

could you please review this PR when you have a chance?

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.

Integer overflow in RPC pagination allows empty results
1 participant