mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-22 16:18:28 -05:00
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:
parent
f80f3f492a
commit
9204a9b2c2
@ -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);
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user