Skip to content

[Cluster] Validate MIGRATE SLOTSRANGE bounds before loop to prevent int overflow hang - #2466

Open
Nguyễn Phúc Hưng Thịnh (nphungthinh2601) wants to merge 2 commits into
microsoft:mainfrom
nphungthinh2601:fix/migrate-slotsrange-overflow
Open

Nguyễn Phúc Hưng Thịnh (nphungthinh2601) wants to merge 2 commits into
microsoft:mainfrom
nphungthinh2601:fix/migrate-slotsrange-overflow

Conversation

@nphungthinh2601

Problem

In ClusterSession.NetworkTryMIGRATE (libs/cluster/Session/MigrateCommand.cs), the SLOTSRANGE parser validates ClusterConfig.OutOfRange(slot) inside for (var slot = slotStart; slot <= slotEnd; slot++) rather than before entering the loop (unlike ClusterSession.TryParseSlots in ClusterCommands.cs).

When slotEnd == int.MaxValue (2147483647) and the target resolves to a known peer node, slot++ overflows from int.MaxValue to int.MinValue, causing the loop to spin indefinitely on the session thread while holding the cluster epoch. Additionally, continue on SLOTNOTLOCAL and MULTISLOTREF inside the range loop overwrote earlier errors in the same range instead of reporting the first offending slot.

Solution

  • libs/cluster/Session/MigrateCommand.cs:
    • Check ClusterConfig.OutOfRange(slotStart) and ClusterConfig.OutOfRange(slotEnd) immediately before the for loop (preserving SLOTOUTOFRANGE, slotParseError, and syntax validation of any subsequent range pairs).
    • Change continue to break on SLOTNOTLOCAL and MULTISLOTREF inside the range loop so SLOTSRANGE consistently reports the first offending slot, matching SLOTS and TryParseSlots.

Testing

  • Added 5 boundary and int.MinValue/int.MaxValue cases (-1 0, 0 16384, 16384 2147483647, 0 2147483647, -2147483648 2147483647) to ClusterMigrateRejectsInvalidTransfer in test/cluster/Garnet.test.cluster.migrate/ClusterMigrateTests.cs.
  • Verified in Release (net10.0, 0 warnings / 0 errors):
    dotnet test test/cluster/Garnet.test.cluster.migrate/Garnet.test.cluster.migrate.csproj -c Release --filter "FullyQualifiedName~ClusterMigrateRejectsInvalidTransfer"
    Passed! - Failed: 0, Passed: 12, Skipped: 0, Total: 12
    

Copilot AI balanced review requested due to automatic review settings December 6, 2026 15:09

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

The new boundary tests do not exercise either changed break, leaving first-offending-slot reporting and zero migration tasks unverified for multiple non-local slots and overlapping local ranges.

1 open issue
What changed in this PR

Prevents integer-overflow hangs in Garnet’s MIGRATE SLOTSRANGE parser.

Changes:

  • Validates range endpoints before iteration.
  • Stops at the first non-local or duplicate slot.
  • Adds five boundary and extreme-integer regression cases.
File Description
test/​cluster/​Garnet.test.cluster.migrate/​ClusterMigrateTests.cs Adds invalid-range regression cases.
libs/​cluster/​Session/​MigrateCommand.cs Checks bounds before looping and preserves the first slot error.

Review effort: Balanced

Comment thread libs/cluster/Session/MigrateCommand.cs Resolved

This branch has not been deployed

No deployments
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