mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-11 10:25:47 +02:00
fix: GETRANGE params validation (#3781)
fix: getrange params validation
This commit is contained in:
parent
526bce4222
commit
987e6feaa5
2 changed files with 23 additions and 11 deletions
|
@ -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 (start < 0)
|
||||
start = strlen + start;
|
||||
if (end < 0)
|
||||
end = strlen + end;
|
||||
|
||||
end = min(end, strlen - 1);
|
||||
|
||||
if (strlen == 0 || start > end)
|
||||
if (strlen == 0)
|
||||
return "";
|
||||
|
||||
start = max(start, 0);
|
||||
end = max(end, 0);
|
||||
if (start < 0) {
|
||||
if (end < start) {
|
||||
return "";
|
||||
}
|
||||
start = strlen + start;
|
||||
start = max(start, 0);
|
||||
}
|
||||
|
||||
if (end < 0) {
|
||||
end = strlen + end;
|
||||
end = max(end, 0);
|
||||
} else {
|
||||
end = min(end, strlen - 1);
|
||||
}
|
||||
|
||||
if (start > end) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return slice.substr(start, end - start + 1);
|
||||
};
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue