From 2509c38e6a6c20592c2ea86fef9dba86deddf446 Mon Sep 17 00:00:00 2001 From: Shivshankar-Reddy Date: Thu, 2 May 2024 17:29:03 +0000 Subject: [PATCH] Make WITHMATCHLEN or MINMATCHLEN options accepted when used with IDX option Signed-off-by: Shivshankar-Reddy --- src/t_string.c | 9 ++++++++- tests/unit/type/string.tcl | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/t_string.c b/src/t_string.c index 1a36871233..1ffd60546b 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -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]); @@ -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); @@ -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"); diff --git a/tests/unit/type/string.tcl b/tests/unit/type/string.tcl index 381cc4a693..3a101daa3e 100644 --- a/tests/unit/type/string.tcl +++ b/tests/unit/type/string.tcl @@ -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