Skip to content

fix(sql): clamp pageNo/pageSize to prevent paging offset int overflow - #2433

Merged
openai0229 merged 4 commits into
OtterMind:mainfrom
Aias00:fix/paging-offset-overflow-clamp-2432
Aug 3, 2026
Merged

openai0229 merged 4 commits into
OtterMind:mainfrom
Aias00:fix/paging-offset-overflow-clamp-2432

Conversation

@Aias00

@Aias00 Aias00 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

What

executeSQL/executeSQLStreaming computed offset = (pageNo - 1) * pageSize with int arithmetic and no bounds, so a large client-controlled pageNo wrapped to a small or negative int: setMaxRows(negative) threw SQLException (500), or setMaxRows(smallPositive) silently capped and returned first-page data for a huge page number.

Location

chat2db-community-spi/.../DefaultSQLExecutor.java:836-839 (executeSQL), :913-916 (executeSQLStreaming); setMaxRows at :247-249/:1001-1003/:1081-1085.

Fix

Clamp pageNo >= 1, pageSize to [1, MAX_PAGE_SIZE], and pageNo to <= Integer.MAX_VALUE / pageSize so (pageNo - 1) * pageSize and offset + count fit in int. Applied to both methods (identical block).

Verification

  • Both occurrences replaced (grep -c = 2).
  • No new imports; valid page numbers unchanged.

Fixes #2432

🤖 Generated with Claude Code

…OtterMind#2432)

executeSQL/executeSQLStreaming computed offset = (pageNo - 1) * pageSize with
int arithmetic and no bounds, so a large client-controlled pageNo wrapped to
a small or negative int: setMaxRows(negative) threw SQLException (500), or
setMaxRows(smallPositive) silently capped and returned first-page data for a
huge page number. Clamp pageNo>=1, pageSize to [1, MAX_PAGE_SIZE], and pageNo
to <= Integer.MAX_VALUE/pageSize so the product fits int.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: liuhy <liuhongyu@apache.org>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens SQL paging in DefaultSQLExecutor by validating/clamping client-controlled pageNo/pageSize to prevent int overflow in offset/max-rows calculations during executeSQL and executeSQLStreaming.

Changes:

  • Clamp pageNo to >= 1 and to a computed maximum (Integer.MAX_VALUE / pageSize) to keep (pageNo - 1) * pageSize within int.
  • Clamp pageSize to a safe range (1..MAX_PAGE_SIZE) to prevent invalid paging inputs from causing wrapped/negative offsets.
  • Apply the same clamping logic to both non-streaming and streaming execution paths.
Suppressed comments (1)

chat2db-community-server/chat2db-community-spi/src/main/java/ai/chat2db/spi/DefaultSQLExecutor.java:932

  • Add a regression test for executeSQLStreaming verifying extreme client-controlled pageNo/pageSize values are clamped so offset and offset+count cannot overflow int (preventing SQLException from setMaxRows and avoiding wrapped offsets).
        // Clamp client-controlled pageNo/pageSize so (pageNo - 1) * pageSize cannot
        // overflow int (which previously made setMaxRows throw on a negative, or
        // silently cap to first-page data on a wrapped small positive).
        if (pageNo < 1) {
            pageNo = 1;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@openai0229 openai0229 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.

The overflow fix is now centralized for synchronous and streaming execution, preserves the final safe page instead of clamping one page early, and has focused boundary tests that pass locally.

@openai0229
openai0229 merged commit 5f76e26 into OtterMind:main Aug 3, 2026
15 of 16 checks passed
@openai0229 openai0229 moved this from In Review to Done in Chat2DB Community Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug(sql): paging offset int overflow throws or silently returns first page

3 participants