Skip to content

Avoid int overflow in sorted-set numkeys argument-count checks - #2112

Open
Tristan Su (foobar) wants to merge 1 commit into
microsoft:mainfrom
foobar:fix/numkeys-overflow-zmpop-zunion
Open

Avoid int overflow in sorted-set numkeys argument-count checks#2112
Tristan Su (foobar) wants to merge 1 commit into
microsoft:mainfrom
foobar:fix/numkeys-overflow-zmpop-zunion

Conversation

@foobar

Copy link
Copy Markdown
Contributor

Follow-up to #2031, which fixed this for ZINTERSTORE. The same additive
argument-count checks remained in ZMPOP, ZUNION, ZUNIONSTORE, ZINTER, and
ZINTERCARD — a numkeys near int.MaxValue wraps the check negative and
slips past, so a single command reads the parse state out of bounds:

ZMPOP 2147483647 key MIN, ZUNION 2147483647 key,
ZUNIONSTORE dst 2147483646 key   -> GetArgSliceByRef at a wild index
                                    (process crash)
ZINTER / ZINTERCARD              -> Span.Slice throw (connection drop)

Repro on a default deployment: ZMPOP 2147483647 k MIN.

This PR applies the same subtraction form to all five guards. Behavior is
unchanged whenever the addition did not overflow. Adds
CanDoSortedSetCommandsWithBadNumKeysLC, mirroring
CanDoZInterStoreWithBadNumKeysLC: each command with
numkeys = 2147483647 followed by PING must get a clean syntax error
with the connection intact.

Copilot AI balanced review requested due to automatic review settings September 9, 2026 01:52

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.

🟢 Approval recommended

The focused validation changes are correct and covered by appropriate regression tests.

Pull request overview

Prevents integer overflow in sorted-set numkeys validation, avoiding out-of-bounds parsing and connection termination.

Changes:

  • Replaces overflow-prone addition checks with safe subtraction checks.
  • Adds regression coverage for five affected commands and verifies connection survival.
File summaries
File Description
libs/server/Resp/Objects/SortedSetCommands.cs Safely validates argument counts for five sorted-set commands.
test/standalone/Garnet.test.collections/RespSortedSetTests.cs Tests int.MaxValue inputs and subsequent PING responses.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@foobar

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Follow-up to microsoft#2031, which hardened ZINTERSTORE's guard as
'parseState.Count - 2 < nKeys' but left the additive form in five sibling
handlers. A numkeys near int.MaxValue wraps the check negative, so a
single command passes validation and reads parseState out of bounds:

    ZMPOP 2147483647 key MIN, ZUNION 2147483647 key,
    ZUNIONSTORE dst 2147483646 key   -> GetArgSliceByRef at a wild index
                                        (process crash)
    ZINTER / ZINTERCARD              -> Span.Slice throw (connection drop)

Use the subtraction form everywhere. Behavior is unchanged whenever the
addition did not overflow.
@foobar
Tristan Su (foobar) force-pushed the fix/numkeys-overflow-zmpop-zunion branch from d1eb1e9 to c68ba74 Compare September 9, 2026 01:58
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.

2 participants