Fix GETRANGE DivideByZeroException on empty-string value with negative start#1969
Merged
vazois merged 1 commit intoJul 22, 2026
Merged
Conversation
…e start NormalizeRange's start < 0 branch does start %= len unconditionally. When the key holds an empty string (len == 0) and start is negative with start <= end (e.g. GETRANGE key -1 -1), this divides/mods by zero and crashes the connection instead of returning an empty string, which is what real Redis returns for any range query against an empty value. Fixed with an early len == 0 guard returning (0, 0) - the same sentinel NormalizeRange already returns from its other empty-result paths.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a crash in Garnet’s GETRANGE handling for keys holding an empty string when start is negative (e.g., GETRANGE key -1 -1). The root cause is a modulo-by-zero in NormalizeRange(start, end, len) when len == 0, which previously could drop the client connection with a DivideByZeroException.
Changes:
- Add an early
len == 0guard inNormalizeRangeto return the existing(0, 0)“empty range” sentinel and avoid modulo-by-zero. - Add a regression test that exercises the previously-crashing cases (negative
start) as well as other empty-value GETRANGE calls.
PR Title & Description Alignment
- Title accurately reflects the implemented fix and the failure mode.
- Description matches the actual code change (early guard in
NormalizeRange) and includes concrete repro + test coverage details.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| libs/server/Storage/Functions/MainStore/PrivateMethods.cs | Adds a len == 0 early-return in NormalizeRange to prevent modulo-by-zero for empty string values in GETRANGE. |
| test/standalone/Garnet.test/RespTests.cs | Adds GetRangeEmptyValueTest covering GETRANGE on an empty string with negative start indices and validating empty-string replies. |
vazois
approved these changes
Jul 22, 2026
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.
Summary
GETRANGEon a key holding an empty string, with a negativestartsuch thatstart <= end(e.g.GETRANGE key -1 -1), crashes the connection with aDivideByZeroExceptioninstead of returning an empty string.Root cause
NormalizeRange(int start, int end, int len)inlibs/server/Storage/Functions/MainStore/PrivateMethods.csdoesstart %= lenunconditionally inside itsstart < 0branch. Whenlen == 0(empty string value), this is a modulo by zero.Fix
Add an early guard returning
(0, 0)whenlen == 0— the same sentinelNormalizeRangealready returns from its other "no valid range" branches (e.g.if (start > end) return (0, 0);), so it's consistent with the function's existing convention rather than a new special case.NormalizeRangehas exactly one caller (GETRANGE's handling inCopyRespToWithInput), and(0, 0)correctly resolves to an empty RESP reply there.Tests
Added
GetRangeEmptyValueTesttotest/standalone/Garnet.test/RespTests.cs, covering the crashing case (GETRANGE key -1 -1,GETRANGE key -1 0) alongside non-crashing empty-value cases (GETRANGE key 0 0,GETRANGE key 0 -1) to confirm all correctly return empty string. Confirmed the test reproduces a connection-level failure against the pre-fix code and passes with the fix.