Skip to content

Commit

Permalink
Fix DECRBY LLONG_MIN caused an overflow (#1581)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
enjoy-binbin authored Jul 13, 2023
1 parent 53b7e87 commit 94df83d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/commands/cmd_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/gocase/unit/type/incr/incr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 94df83d

Please sign in to comment.