]> git.saurik.com Git - redis.git/commitdiff
fix unexpected behavior on an out of range end index for LRANGE and LTRIM
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 24 Jun 2010 22:12:42 +0000 (15:12 -0700)
committerPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 24 Jun 2010 22:12:42 +0000 (15:12 -0700)
redis.c
tests/unit/type/list.tcl

diff --git a/redis.c b/redis.c
index 6ac410d0e523f8d16be9bc4e1b84320c1dd78d9a..085d35532a8f4bc1ed3dec83a9829b8fdb072b99 100644 (file)
--- a/redis.c
+++ b/redis.c
@@ -5404,9 +5404,9 @@ static void lrangeCommand(redisClient *c) {
     if (start < 0) start = llen+start;
     if (end < 0) end = llen+end;
     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) {
         /* Out of range start or start > end result in empty list */
         addReply(c,shared.emptymultibulk);
@@ -5444,9 +5444,9 @@ static void ltrimCommand(redisClient *c) {
     if (start < 0) start = llen+start;
     if (end < 0) end = llen+end;
     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) {
         /* Out of range start or start > end result in empty list */
         ltrim = llen;
index ecae5d228f09f92e1b0fd6bce01d8bb1eea43864..8a7ea278bee4b3c5ec4e9ff17c375c2a46fdadbb 100644 (file)
@@ -340,6 +340,12 @@ start_server {
             create_$type mylist {1 2 3}
             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} {
@@ -369,6 +375,11 @@ start_server {
             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"} {
             test "LTRIM stress testing - $type" {
                 set mylist {}