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
```
This commit is contained in:
Valentino Geron 2020-08-20 18:48:09 +03:00 committed by Oran Agra
parent f80f3f492a
commit 9204a9b2c2
2 changed files with 9 additions and 2 deletions

View File

@ -571,13 +571,14 @@ void lposCommand(client *c) {
li = listTypeInitIterator(o,direction == LIST_HEAD ? -1 : 0,direction);
listTypeEntry entry;
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)) {
if (listTypeEqual(&entry,ele)) {
matches++;
matchindex = (direction == LIST_TAIL) ? index : llen - index - 1;
if (matches >= rank) {
if (arraylenptr) {
arraylen++;
addReplyLongLong(c,matchindex);
if (count && matches-rank+1 >= count) break;
} else {
@ -593,7 +594,7 @@ void lposCommand(client *c) {
/* Reply to the client. Note that arraylenptr is not NULL only if
* the COUNT option was selected. */
if (arraylenptr != NULL) {
setDeferredArrayLen(c,arraylenptr,matches-rank+1);
setDeferredArrayLen(c,arraylenptr,arraylen);
} else {
if (matchindex != -1)
addReplyLongLong(c,matchindex);

View File

@ -50,6 +50,12 @@ start_server {
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} {
# first lpush then rpush
assert_equal 1 [r lpush myziplist1 aa]