Skip to content

Commit

Permalink
Make WITHMATCHLEN or MINMATCHLEN options accepted when used with IDX …
Browse files Browse the repository at this point in the history
…option

Signed-off-by: Shivshankar-Reddy <[email protected]>
  • Loading branch information
Shivshankar-Reddy committed May 2, 2024
1 parent 443d80f commit 2509c38
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/t_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ void lcsCommand(client *c) {
uint32_t i, j;
long long minmatchlen = 0;
sds a = NULL, b = NULL;
int getlen = 0, getidx = 0, withmatchlen = 0;
int getlen = 0, getidx = 0, withmatchlen = 0, minmatchlenflag = 0;
robj *obja = NULL, *objb = NULL;

obja = lookupKeyRead(c->db,c->argv[1]);
Expand Down Expand Up @@ -773,6 +773,7 @@ void lcsCommand(client *c) {
if (getLongLongFromObjectOrReply(c,c->argv[j+1],&minmatchlen,NULL)
!= C_OK) goto cleanup;
if (minmatchlen < 0) minmatchlen = 0;
minmatchlenflag = 1;
j++;
} else {
addReplyErrorObject(c,shared.syntaxerr);
Expand All @@ -787,6 +788,12 @@ void lcsCommand(client *c) {
goto cleanup;
}

/* Complain if user passed WITHMATCHLEN or MINMATCHLEN without IDX */
if ((withmatchlen || minmatchlenflag) && !getidx) {
addReplyError(c, "WITHMATCHLEN and MINMATCHLEN can only be used with IDX.");
goto cleanup;
}

/* Detect string truncation or later overflows. */
if (sdslen(a) >= UINT32_MAX-1 || sdslen(b) >= UINT32_MAX-1) {
addReplyError(c, "String too long for LCS");
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/type/string.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,16 @@ if {[string match {*jemalloc*} [s mem_allocator]]} {
dict get [r LCS virus1{t} virus2{t} IDX WITHMATCHLEN MINMATCHLEN 5] matches
} {{{1 222} {13 234} 222}}

test {LCS with LEN and IDX option} {
assert_error "ERR If you want both the length and indexes, please just use IDX." {r LCS virus1{t} virus2{t} LEN IDX}
}

test {LCS with WITHMATCHLEN and MINMATCHLEN option without IDX option} {
assert_error "*ERR*WITHMATCHLEN*MINMATCHLEN*can only be used with IDX*" {r LCS virus1{t} virus2{t} WITHMATCHLEN}
assert_error "*ERR*WITHMATCHLEN*MINMATCHLEN*can only be used with IDX*" {r LCS virus1{t} virus2{t} MINMATCHLEN 5}
assert_error "*ERR*WITHMATCHLEN*MINMATCHLEN*can only be used with IDX*" {r LCS virus1{t} virus2{t} LEN WITHMATCHLEN MINMATCHLEN 5}
}

test {SETRANGE with huge offset} {
foreach value {9223372036854775807 2147483647} {
catch {[r setrange K $value A]} res
Expand Down

0 comments on commit 2509c38

Please sign in to comment.