Fix LRANGE returning the head element when stop is more negative than the list length - #2029
Merged
kevin-montrose merged 3 commits intoAug 7, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes Garnet’s LRANGE index normalization for lists so that ranges with a stop that remains negative after adding the list length correctly return an empty array (matching Redis behavior), instead of incorrectly returning the head element.
Changes:
- Removed the
stop < 0clamp inListObjectImpl.ListRangeso the existingstart > stopguard can return an empty array for out-of-range negativestopvalues. - Extended
CanDoLRANGEcorrectwith regression cases coveringstopvalues more negative than-listLength(and nearby boundaries).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| libs/server/Objects/List/ListObjectImpl.cs | Adjusts LRANGE stop-index normalization to avoid clamping still-negative stop to 0, allowing empty-range detection to work. |
| test/standalone/Garnet.test.collections/RespListTests.cs | Adds regression tests ensuring LRANGE returns an empty array for overly-negative stop values on 3- and 5-element lists. |
hexonal (hexonal)
force-pushed
the
fix-lrange-negative-stop-clamp
branch
from
August 7, 2026 05:25
54ba508 to
c22d7f0
Compare
kevin-montrose
approved these changes
Aug 7, 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.
LRANGEover a range whosestopis more negative than the list length returns the first element instead of an empty array.LRANGE lk 0 -3is the last range that still addresses an element (a). Anything past it addresses nothing and should come back empty.Root cause
libs/server/Objects/List/ListObjectImpl.cs:147A
stopthat is still negative after addinglist.Countaddresses nothing. Clamping it to0turns it into the head index, and sincestartwas already clamped to0two lines above, thestart > stopguard no longer fires. The range collapses to[0, 0]and one element is written.Fix
Delete the clamp.
startis guaranteed>= 0, so leavingstopnegative lets the existingstart > stopguard fire and write the empty array.This matches how sorted sets already do it:
ZRANGEby index inlibs/server/Objects/SortedSet/SortedSetObjectImpl.cs:478-500normalizes a negativemaxIndexwithout clamping it to0, and clampsminIndexto0only after its(minIndex < 0 && maxIndex < 0) || (minIndex > maxIndex)emptiness check has run.ListTrimin the same file (ListObjectImpl.cs:185) likewise keepsend < 0as its own empty-range term rather than clamping it away.The
stop >= list.Countclamp stays where it is, above thestart > stopcheck: unlike Redis, Garnet's guard carries nostart >= list.Countterm, so that clamp is what makesLRANGE lk 5 100empty.Tests
Extended
CanDoLRANGEcorrectintest/standalone/Garnet.test.collections/RespListTests.cs.Fail on
maintoday, pass with the fix — each returns["a"]where an empty array is expected:LRANGE key3 0 -4,LRANGE key3 0 -5,LRANGE key3 -5 -4on a 3-element listLRANGE key5 0 -6,LRANGE key5 0 -7on a 5-element listAlready pass on
main, added as regression guards for the boundary on either side:LRANGE key3 0 -3->["a"], the laststopthat still addresses an elementLRANGE key3 1 -10-> empty, whichmaingets right only by accident becausestart = 1exceeded the clampedstop = 0Existing coverage that pins the clamp order stays green:
CanDoLRANGEbasicassertsLRANGE key -100 100-> 3 elements andLRANGE key 5 100-> empty on a 3-element list, andCanReturnEmptyArrayinListLCassertsLRANGE mylist 5 10->*0\r\non the wire.