diff --git a/src/t_zset.c b/src/t_zset.c index 07581673e..6147674ff 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -2906,6 +2906,12 @@ void genericZrangebyscoreCommand(client *c, int reverse) { if ((zobj = lookupKeyReadOrReply(c,key,shared.emptyarray)) == NULL || checkType(c,zobj,OBJ_ZSET)) return; + /* For invalid offset, return directly. */ + if (offset > 0 && offset >= (long)zsetLength(zobj)) { + addReply(c,shared.emptyarray); + return; + } + if (zobj->encoding == OBJ_ENCODING_ZIPLIST) { unsigned char *zl = zobj->ptr; unsigned char *eptr, *sptr; diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index d8fccd33c..c1406797b 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -432,6 +432,7 @@ start_server {tags {"zset"}} { create_default_zset assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES] assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES] + assert_equal {} [r zrangebyscore zset 2 5 LIMIT 12 13 WITHSCORES] } test "ZRANGEBYSCORE with non-value min or max" {