Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LSC command does not accept WITHMATCHLEN or MINMATCHLEN options when used without IDX, as it does not make sense. #420

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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