From 6f4f31f167fbaf6da1939a7764606059ca7e6de1 Mon Sep 17 00:00:00 2001 From: yoav-steinberg Date: Sun, 3 Oct 2021 09:38:05 +0300 Subject: [PATCH] decrby LLONG_MIN caused nagation overflow. (#9577) Note that this breaks compatibility because in the past doing: DECRBY x -9223372036854775808 would succeed (and create an invalid result) and now this returns an error. --- src/t_string.c | 5 +++++ tests/unit/type/incr.tcl | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/t_string.c b/src/t_string.c index d7173b53f..fd938a9ec 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -637,6 +637,11 @@ void decrbyCommand(client *c) { long long incr; if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != C_OK) return; + /* Overflow check: negating LLONG_MIN will cause an overflow */ + if (incr == LLONG_MIN) { + addReplyError(c, "decrement would overflow"); + return; + } incrDecrCommand(c,-incr); } diff --git a/tests/unit/type/incr.tcl b/tests/unit/type/incr.tcl index aa37061d5..b6aaa1112 100644 --- a/tests/unit/type/incr.tcl +++ b/tests/unit/type/incr.tcl @@ -42,6 +42,12 @@ start_server {tags {"incr"}} { format $err } {ERR*} + test {DECRBY negation overflow} { + r set x 0 + catch {r decrby x -9223372036854775808} err + format $err + } {ERR*} + test {INCR fails against a key holding a list} { r rpush mylist 1 catch {r incr mylist} err