This is an automated email from the ASF dual-hosted git repository. hulk pushed a commit to branch 2.5 in repository https://gitbox.apache.org/repos/asf/kvrocks.git
commit 367a0eedf1a85188a7cf6b8a2634e58d32935025 Author: Binbin <[email protected]> AuthorDate: Thu Jul 13 14:33:38 2023 +0800 Fix DECRBY LLONG_MIN caused an overflow (#1581) Note that this may break compatibility since in the past doing: `DECRBY key -9223372036854775808` would succeed but create an invalid result. And now we will return an error rejecting the request. Before: ``` 127.0.0.1:6666> set key 0 OK 127.0.0.1:6666> decrby key -9223372036854775807 (integer) 9223372036854775807 127.0.0.1:6666> get key "9223372036854775807" 127.0.0.1:6666> set key 0 OK 127.0.0.1:6666> decrby key -9223372036854775808 (integer) -9223372036854775808 127.0.0.1:6666> get key "-9223372036854775808" ``` After: ``` 127.0.0.1:6666> decrby key -9223372036854775808 (error) ERR decrement would overflow ``` --- src/commands/cmd_string.cc | 6 ++++++ tests/gocase/unit/type/incr/incr_test.go | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/commands/cmd_string.cc b/src/commands/cmd_string.cc index 4897011e..9832716b 100644 --- a/src/commands/cmd_string.cc +++ b/src/commands/cmd_string.cc @@ -530,6 +530,12 @@ class CommandDecrBy : public Commander { } increment_ = *parse_result; + + // Negating LLONG_MIN will cause an overflow. + if (increment_ == LLONG_MIN) { + return {Status::RedisParseErr, "decrement would overflow"}; + } + return Commander::Parse(args); } diff --git a/tests/gocase/unit/type/incr/incr_test.go b/tests/gocase/unit/type/incr/incr_test.go index eb5c9f79..a90f9e1d 100644 --- a/tests/gocase/unit/type/incr/incr_test.go +++ b/tests/gocase/unit/type/incr/incr_test.go @@ -84,6 +84,11 @@ func TestIncr(t *testing.T) { require.EqualValues(t, -1, rdb.DecrBy(ctx, "novar", 17179869185).Val()) }) + t.Run("DECRBY negation overflow", func(t *testing.T) { + require.NoError(t, rdb.Do(ctx, "SET", "foo", "0").Err()) + util.ErrorRegexp(t, rdb.Do(ctx, "DECRBY", "foo", "-9223372036854775808").Err(), ".*decrement would overflow.*") + }) + t.Run("INCRBYFLOAT against non existing key", func(t *testing.T) { require.NoError(t, rdb.Del(ctx, "novar").Err()) require.EqualValues(t, 1, rdb.IncrByFloat(ctx, "novar", 1.0).Val())
