Skip to content

Commit 4404eb7

Browse files
fix(string): add empty string value check for INCR to match Redis behavior (#3354)
### Background Previously, Kvrocks allowed INCR on a key whose value was an empty string (""), treating it as zero and returning 1. In Redis, the same operation results in an error: ERR value is not an integer or out of range This difference caused inconsistent behavior between Kvrocks and Redis. ### Changes - Added check for empty string values before performing INCR - Return error when value is an empty string, matching Redis behavior ### Result Kvrocks now behaves consistently with Redis when performing INCR on empty string values, improving compatibility and reducing unexpected results in client applications. Co-authored-by: yxj25245 <yxj25245@ly.com>
1 parent cf384eb commit 4404eb7

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

src/types/redis_string.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ rocksdb::Status String::IncrBy(engine::Context &ctx, const std::string &user_key
373373

374374
size_t offset = Metadata::GetOffsetAfterExpire(raw_value[0]);
375375
std::string value = raw_value.substr(offset);
376+
if (s.ok() && value.empty()) {
377+
return rocksdb::Status::InvalidArgument("value is not an integer or out of range");
378+
}
379+
376380
int64_t n = 0;
377381
if (!value.empty()) {
378382
auto parse_result = ParseInt<int64_t>(value, 10);

tests/gocase/unit/type/incr/incr_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func testIncr(t *testing.T, configs util.KvrocksServerConfigs) {
8484
require.EqualValues(t, 1, rdb.IncrBy(ctx, "expired-str", 1).Val())
8585
})
8686

87+
t.Run("INCR fails against key with empty value", func(t *testing.T) {
88+
require.NoError(t, rdb.Set(ctx, "novar", "", 0).Err())
89+
util.ErrorRegexp(t, rdb.Incr(ctx, "novar").Err(), "ERR.*")
90+
})
91+
8792
t.Run("INCR fails against key with spaces (left)", func(t *testing.T) {
8893
require.NoError(t, rdb.Set(ctx, "novar", " 11", 0).Err())
8994
util.ErrorRegexp(t, rdb.Incr(ctx, "novar").Err(), "ERR.*")

0 commit comments

Comments
 (0)