Skip to content

Commit

Permalink
fix: GETRANGE params validation (#3781)
Browse files Browse the repository at this point in the history
fix: getrange params validation
  • Loading branch information
BorysTheDev authored Sep 24, 2024
1 parent 526bce4 commit 987e6fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/server/string_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,27 @@ OpResult<StringValue> OpGetRange(const OpArgs& op_args, string_view key, int32_t
int32_t end) {
auto read = [start, end](std::string_view slice) mutable -> string_view {
int32_t strlen = slice.size();
if (strlen == 0)
return "";

if (start < 0)
if (start < 0) {
if (end < start) {
return "";
}
start = strlen + start;
if (end < 0)
end = strlen + end;
start = max(start, 0);
}

end = min(end, strlen - 1);
if (end < 0) {
end = strlen + end;
end = max(end, 0);
} else {
end = min(end, strlen - 1);
}

if (strlen == 0 || start > end)
if (start > end) {
return "";

start = max(start, 0);
end = max(end, 0);
}

return slice.substr(start, end - start + 1);
};
Expand Down
4 changes: 4 additions & 0 deletions src/server/string_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,16 @@ TEST_F(StringFamilyTest, Range) {

Run({"SET", "key4", "1"});
EXPECT_EQ(Run({"getrange", "key4", "-1", "-2"}), "");
EXPECT_EQ(Run({"getrange", "key4", "0", "-2"}), "1");

EXPECT_EQ(CheckedInt({"SETRANGE", "key5", "1", ""}), 0);
EXPECT_EQ(Run({"GET", "key5"}).type, facade::RespExpr::NIL);

EXPECT_EQ(CheckedInt({"SETRANGE", "num", "6", ""}), 4);
EXPECT_EQ(Run({"GET", "num"}), "1234");

// we support only 256MB string so this test is failed now
// EXPECT_THAT(CheckedInt({"SETRANGE", "", "268435456", "0"}), 268435457);
}

TEST_F(StringFamilyTest, IncrByFloat) {
Expand Down

0 comments on commit 987e6fe

Please sign in to comment.