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

Fix LPOS rank passing LLONG_MIN overflow issue #1687

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/commands/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -697,16 +697,19 @@ class CommandLPos : public Commander {
"RANK can't be zero: use 1 to start from "
"the first match, 2 from the second ... "
"or use negative to start from the end of the list"};
} else if (spec_.rank == LLONG_MIN) {
// Negating LLONG_MIN will cause an overflow, and is effectively be the same as passing -1.
return {Status::RedisParseErr, "rank would overflow"};
}
} else if (parser.EatEqICase("count")) {
spec_.count = GET_OR_RET(parser.TakeInt());
if (spec_.count < 0) {
return {Status::RedisExecErr, "COUNT can't be negative"};
return {Status::RedisParseErr, "COUNT can't be negative"};
}
} else if (parser.EatEqICase("maxlen")) {
spec_.max_len = GET_OR_RET(parser.TakeInt());
if (spec_.max_len < 0) {
return {Status::RedisExecErr, "MAXLEN can't be negative"};
return {Status::RedisParseErr, "MAXLEN can't be negative"};
}
} else {
return {Status::RedisParseErr, errInvalidSyntax};
Expand Down
5 changes: 5 additions & 0 deletions tests/gocase/unit/type/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,11 @@ func TestList(t *testing.T) {
require.Equal(t, "bar", rdb.LRange(ctx, "target", 0, -1).Val()[0])
})

t.Run("LPOS rank negation overflow", func(t *testing.T) {
require.NoError(t, rdb.Del(ctx, "mylist").Err())
util.ErrorRegexp(t, rdb.Do(ctx, "LPOS", "mylist", "foo", "RANK", "-9223372036854775808").Err(), ".*rank would overflow.*")
})

for listType, large := range largeValue {
t.Run(fmt.Sprintf("LPOS basic usage - %s", listType), func(t *testing.T) {
createList("mylist", []string{"a", "b", "c", large, "2", "3", "c", "c"})
Expand Down