From 4b54b1e53830cf90df0df97b39ff1595f2268c1c Mon Sep 17 00:00:00 2001 From: Matt Stancliff Date: Mon, 21 Jul 2014 17:31:21 -0400 Subject: [PATCH] Fix key extraction for SORT We only want to use the last STORE key, but we have to record we actually found a STORE key so we can increment the final return key count. Test added to prevent further regression. Closes #1883, #1645, #1647 --- src/db.c | 5 +++-- tests/unit/sort.tcl | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/db.c b/src/db.c index c83ab2ee6a48..cd2cb1a20c74 100644 --- a/src/db.c +++ b/src/db.c @@ -1076,7 +1076,7 @@ int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) * follow in SQL-alike style. Here we parse just the minimum in order to * correctly identify keys in the "STORE" option. */ int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { - int i, j, num, *keys; + int i, j, num, *keys, found_store = 0; REDIS_NOTUSED(cmd); num = 0; @@ -1107,12 +1107,13 @@ int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) /* Note: we don't increment "num" here and continue the loop * to be sure to process the *last* "STORE" option if multiple * ones are provided. This is same behavior as SORT. */ + found_store = 1; keys[num] = i+1; /* */ break; } } } - *numkeys = num; + *numkeys = num + found_store; return keys; } diff --git a/tests/unit/sort.tcl b/tests/unit/sort.tcl index f48f88b5de84..54b0cc7e2581 100644 --- a/tests/unit/sort.tcl +++ b/tests/unit/sort.tcl @@ -95,6 +95,14 @@ start_server { assert_encoding ziplist sort-res } + test "SORT extracts STORE correctly" { + r command getkeys sort abc store def + } {abc def} + + test "SORT extracts multiple STORE correctly" { + r command getkeys sort abc store invalid store stillbad store def + } {abc def} + test "SORT DESC" { assert_equal [lsort -decreasing -integer $result] [r sort tosort DESC] }