KeyDB icon indicating copy to clipboard operation
KeyDB copied to clipboard

[BUG] `DECRBY k -9223372036854775808` stores a wrapped value instead of failing (fixed in Redis 7.0, still in KeyDB 6.3.4)

Open jiren-the-gray opened this issue 1 month ago • 0 comments

Describe the bug

DECRBY k d negates d and then adds it with an overflow check. For d = LLONG_MIN the negation wraps back to LLONG_MIN. Adding that to a non-negative value does not overflow, so the check passes and the wrong value is stored:

127.0.0.1:6379> SET k 5
OK
127.0.0.1:6379> DECRBY k -9223372036854775808
(integer) -9223372036854775803
127.0.0.1:6379> GET k
"-9223372036854775803"

The true result, 5 + 2^63, is not a 64-bit signed integer, so the command must fail. Redis 6.2 has the same bug. Redis 7.0 and later answer ERR decrement would overflow (both checked with the redis:6.2-alpine and redis:7.0-alpine images). KeyDB 6.3.4 inherited the 6.2 behaviour. For a negative stored value the addition does overflow and KeyDB already answers ERR increment or decrement would overflow.

To reproduce

Standalone script (python3 repro.py <port>):

#!/usr/bin/env python3
# usage: python3 repro.py [port]   (Python 3, standard library only)
import socket, sys, time
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 6379

class Conn:  # minimal RESP2 client, standard library only
    def __init__(self):
        self.s = socket.create_connection(("127.0.0.1", PORT), timeout=5)
        self.f = self.s.makefile("rb")
    def send(self, *args):
        a = [str(x).encode() for x in args]
        self.s.sendall(b"*%d\r\n" % len(a) + b"".join(b"$%d\r\n%s\r\n" % (len(x), x) for x in a))
    def read(self):
        line = self.f.readline()
        if not line: return "<connection closed>"
        t, v = line[:1], line[1:-2]
        if t == b"+": return v.decode()
        if t == b"-": return "(error) " + v.decode()
        if t == b":": return int(v)
        if t == b"$": return None if v == b"-1" else self.f.read(int(v) + 2)[:-2]
        if t == b"*": return None if v == b"-1" else [self.read() for _ in range(int(v))]
        return "<unexpected bytes %r>" % line[:20]
    def __call__(self, *args):
        self.send(*args)
        return self.read()

r = Conn()
r("DEL", "k")
print("SET k 5                       ->", r("SET", "k", "5"))
print("DECRBY k -9223372036854775808 ->", r("DECRBY", "k", "-9223372036854775808"))
print("GET k                         ->", r("GET", "k"))

KeyDB 6.3.4:

SET k 5                       -> OK
DECRBY k -9223372036854775808 -> -9223372036854775803
GET k                         -> b'-9223372036854775803'

Redis 8.10.2 (same on Redis 7.0):

SET k 5                       -> OK
DECRBY k -9223372036854775808 -> (error) ERR decrement would overflow
GET k                         -> b'5'

Expected behavior

DECRBY: "This operation is limited to 64-bit signed integers." Answer an overflow error and leave k at 5. The Redis 7.0 fix rejects decrement == LLONG_MIN in decrbyCommand before negating it.

Additional information

Environment: KeyDB 6.3.4, eqalpha/keydb:latest (pulled 2026-10-15), default settings, Linux x86-64 (Docker).

jiren-the-gray avatar Oct 15 '26 16:10 jiren-the-gray