This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 94df83d7 Fix DECRBY LLONG_MIN caused an overflow (#1581)
94df83d7 is described below

commit 94df83d7b90c339d89705e80c5133f784ec9f93d
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())

Reply via email to