fix(sql): clamp pageNo/pageSize to prevent paging offset int overflow - #2433
Merged
openai0229 merged 4 commits intoAug 3, 2026
Merged
Conversation
…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>
There was a problem hiding this comment.
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
pageNoto>= 1and to a computed maximum (Integer.MAX_VALUE / pageSize) to keep(pageNo - 1) * pageSizewithinint. - Clamp
pageSizeto 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
approved these changes
Aug 3, 2026
openai0229
left a comment
Contributor
There was a problem hiding this comment.
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.
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.
What
executeSQL/executeSQLStreamingcomputedoffset = (pageNo - 1) * pageSizewithintarithmetic and no bounds, so a large client-controlledpageNowrapped to a small or negativeint:setMaxRows(negative)threwSQLException(500), orsetMaxRows(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,pageSizeto[1, MAX_PAGE_SIZE], andpageNoto<= Integer.MAX_VALUE / pageSizeso(pageNo - 1) * pageSizeandoffset + countfit inint. Applied to both methods (identical block).Verification
grep -c= 2).Fixes #2432
🤖 Generated with Claude Code