fix unexpected behavior on an out of range end index for LRANGE and LTRIM

This commit is contained in:
Pieter Noordhuis 2010-06-24 15:12:42 -07:00
parent 4774a53b24
commit f483ce5ffe
2 changed files with 15 additions and 4 deletions

View File

@ -5404,9 +5404,9 @@ static void lrangeCommand(redisClient *c) {
if (start < 0) start = llen+start; if (start < 0) start = llen+start;
if (end < 0) end = llen+end; if (end < 0) end = llen+end;
if (start < 0) start = 0; if (start < 0) start = 0;
if (end < 0) end = 0;
/* indexes sanity checks */ /* Invariant: start >= 0, so this test will be true when end < 0.
* The range is empty when start > end or start >= length. */
if (start > end || start >= llen) { if (start > end || start >= llen) {
/* Out of range start or start > end result in empty list */ /* Out of range start or start > end result in empty list */
addReply(c,shared.emptymultibulk); addReply(c,shared.emptymultibulk);
@ -5444,9 +5444,9 @@ static void ltrimCommand(redisClient *c) {
if (start < 0) start = llen+start; if (start < 0) start = llen+start;
if (end < 0) end = llen+end; if (end < 0) end = llen+end;
if (start < 0) start = 0; if (start < 0) start = 0;
if (end < 0) end = 0;
/* indexes sanity checks */ /* Invariant: start >= 0, so this test will be true when end < 0.
* The range is empty when start > end or start >= length. */
if (start > end || start >= llen) { if (start > end || start >= llen) {
/* Out of range start or start > end result in empty list */ /* Out of range start or start > end result in empty list */
ltrim = llen; ltrim = llen;

View File

@ -340,6 +340,12 @@ start_server {
create_$type mylist {1 2 3} create_$type mylist {1 2 3}
assert_equal {1 2 3} [r lrange mylist -1000 1000] assert_equal {1 2 3} [r lrange mylist -1000 1000]
} }
test "LRANGE out of range negative end index - $type" {
create_$type mylist {1 2 3}
assert_equal {1} [r lrange mylist 0 -3]
assert_equal {} [r lrange mylist 0 -4]
}
} }
test {LRANGE against non existing key} { test {LRANGE against non existing key} {
@ -369,6 +375,11 @@ start_server {
assert_equal {1 2 3 4 5} [trim_list $type 0 10] assert_equal {1 2 3 4 5} [trim_list $type 0 10]
} }
test "LTRIM out of range negative end index - $type" {
assert_equal {1} [trim_list $type 0 -5]
assert_equal {} [trim_list $type 0 -6]
}
tags {"slow"} { tags {"slow"} {
test "LTRIM stress testing - $type" { test "LTRIM stress testing - $type" {
set mylist {} set mylist {}