Commit 9204a9b2 authored by Valentino Geron's avatar Valentino Geron Committed by Oran Agra
Browse files

Fix LPOS command when RANK is greater than matches

When calling to LPOS command when RANK is higher than matches,
the return value is non valid response. For example:
```
LPUSH l a
:1
LPOS l b RANK 5 COUNT 10
*-4
```
It may break client-side parser.

Now, we count how many replies were replied in the array.
```
LPUSH l a
:1
LPOS l b RANK 5 COUNT 10
*0
```
parent f80f3f49
...@@ -571,13 +571,14 @@ void lposCommand(client *c) { ...@@ -571,13 +571,14 @@ void lposCommand(client *c) {
li = listTypeInitIterator(o,direction == LIST_HEAD ? -1 : 0,direction); li = listTypeInitIterator(o,direction == LIST_HEAD ? -1 : 0,direction);
listTypeEntry entry; listTypeEntry entry;
long llen = listTypeLength(o); long llen = listTypeLength(o);
long index = 0, matches = 0, matchindex = -1; long index = 0, matches = 0, matchindex = -1, arraylen = 0;
while (listTypeNext(li,&entry) && (maxlen == 0 || index < maxlen)) { while (listTypeNext(li,&entry) && (maxlen == 0 || index < maxlen)) {
if (listTypeEqual(&entry,ele)) { if (listTypeEqual(&entry,ele)) {
matches++; matches++;
matchindex = (direction == LIST_TAIL) ? index : llen - index - 1; matchindex = (direction == LIST_TAIL) ? index : llen - index - 1;
if (matches >= rank) { if (matches >= rank) {
if (arraylenptr) { if (arraylenptr) {
arraylen++;
addReplyLongLong(c,matchindex); addReplyLongLong(c,matchindex);
if (count && matches-rank+1 >= count) break; if (count && matches-rank+1 >= count) break;
} else { } else {
...@@ -593,7 +594,7 @@ void lposCommand(client *c) { ...@@ -593,7 +594,7 @@ void lposCommand(client *c) {
/* Reply to the client. Note that arraylenptr is not NULL only if /* Reply to the client. Note that arraylenptr is not NULL only if
* the COUNT option was selected. */ * the COUNT option was selected. */
if (arraylenptr != NULL) { if (arraylenptr != NULL) {
setDeferredArrayLen(c,arraylenptr,matches-rank+1); setDeferredArrayLen(c,arraylenptr,arraylen);
} else { } else {
if (matchindex != -1) if (matchindex != -1)
addReplyLongLong(c,matchindex); addReplyLongLong(c,matchindex);
......
...@@ -50,6 +50,12 @@ start_server { ...@@ -50,6 +50,12 @@ start_server {
assert {[r LPOS mylist c COUNT 0 MAXLEN 7 RANK 2] == {6}} assert {[r LPOS mylist c COUNT 0 MAXLEN 7 RANK 2] == {6}}
} }
test {LPOS when RANK is greater than matches} {
r DEL mylist
r LPUSH l a
assert {[r LPOS mylist b COUNT 10 RANK 5] eq {}}
}
test {LPUSH, RPUSH, LLENGTH, LINDEX, LPOP - ziplist} { test {LPUSH, RPUSH, LLENGTH, LINDEX, LPOP - ziplist} {
# first lpush then rpush # first lpush then rpush
assert_equal 1 [r lpush myziplist1 aa] assert_equal 1 [r lpush myziplist1 aa]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment