1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00

fix(server):Return an error, instead of crashing, for requests trying to rename a key to the same key. (#897)

fix:Return an error, instead of crashing, for requests trying to rename a key to the same key.

Fixes #850

---------

Co-authored-by: Shahar Mike <shmike@google.com>
This commit is contained in:
Chaka 2023-03-02 21:46:27 +02:00 committed by GitHub
parent 4a5d2f2a9a
commit bcc3d3ec4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View file

@ -13,7 +13,6 @@ std::string WrongNumArgsError(std::string_view cmd);
std::string InvalidExpireTime(std::string_view cmd);
std::string UnknownSubCmd(std::string_view subcmd, std::string_view cmd);
extern const char kSyntaxErr[];
extern const char kWrongTypeErr[];
extern const char kKeyNotFoundErr[];
@ -32,4 +31,4 @@ extern const char kIndexOutOfRange[];
extern const char kOutOfMemory[];
extern const char kInvalidNumericResult[];
} // namespace dfly
} // namespace facade

View file

@ -1327,6 +1327,9 @@ OpResult<void> GenericFamily::OpRen(const OpArgs& op_args, string_view from_key,
if (!IsValid(from_it))
return OpStatus::KEY_NOTFOUND;
if (from_key == to_key)
return OpStatus::OK;
bool is_prior_list = false;
auto [to_it, to_expire] = db_slice.FindExt(op_args.db_cntx, to_key);
if (IsValid(to_it)) {

View file

@ -199,6 +199,15 @@ TEST_F(GenericFamilyTest, RenameNx) {
ASSERT_EQ(Run({"get", "y"}), x_val);
}
TEST_F(GenericFamilyTest, RenameSameName) {
const char kKey[] = "key";
ASSERT_THAT(Run({"rename", kKey, kKey}), ErrArg("no such key"));
ASSERT_EQ(Run({"set", kKey, "value"}), "OK");
EXPECT_EQ(Run({"rename", kKey, kKey}), "OK");
}
TEST_F(GenericFamilyTest, Stick) {
// check stick returns zero on non-existent keys
ASSERT_THAT(Run({"stick", "a", "b"}), IntArg(0));