Skip to content

Commit 7fdd1fe

Browse files
committed
fix(string): propagate WRONGTYPE error for conditional SET operations
- Return WRONGTYPE when IFEQ/IFNE/IFDEQ/IFDNE is used on non-string keys - Treat NX with wrong type as "key exists" so the condition is not met - Keep other behaviors unchanged
1 parent 6278634 commit 7fdd1fe

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/types/redis_string.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,21 @@ rocksdb::Status String::Set(engine::Context &ctx, const std::string &user_key, c
249249
uint64_t old_expire = 0;
250250
auto s = getValueAndExpire(ctx, ns_key, &old_value, &old_expire);
251251
if (!s.ok() && !s.IsNotFound() && !s.IsInvalidArgument()) return s;
252+
// If the existing key is not a string type, enforce expected behaviors:
253+
if (s.IsInvalidArgument()) {
254+
// For conditional comparisons (IFEQ/IFNE/IFDEQ/IFDNE), reading the old value is required,
255+
// so return the underlying WRONGTYPE (InvalidArgument) error.
256+
if (args.type == StringSetType::IFEQ || args.type == StringSetType::IFNE || args.type == StringSetType::IFDEQ ||
257+
args.type == StringSetType::IFDNE) {
258+
return s;
259+
}
260+
// For NX option, treat a wrong type as "key exists" so the condition is not met.
261+
if (args.type == StringSetType::NX) {
262+
if (!args.get) ret = std::nullopt;
263+
return rocksdb::Status::OK();
264+
}
265+
// For other options, continue (e.g., XX may still proceed since key exists).
266+
}
252267
// GET option
253268
if (args.get) {
254269
if (s.IsInvalidArgument()) {

0 commit comments

Comments
 (0)