From 275c4235245bb8d5eb1923339d9c5694a554286d Mon Sep 17 00:00:00 2001 From: Stalin Date: Thu, 3 Dec 2026 14:57:40 +0530 Subject: [PATCH] Fix DECRBY with LLONG_MIN storing a wrapped value DECRBY negates the decrement before calling incrDecrCommand(). -LLONG_MIN wraps back to LLONG_MIN, so adding it to a non-negative value passes the overflow check and stores a wrong number. Reply with "decrement would overflow" instead, as Redis 7.0 does, and add a regression test. Fixes #982 Signed-off-by: Stalin --- src/t_string.cpp | 6 ++++++ tests/unit/type/incr.tcl | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/t_string.cpp b/src/t_string.cpp index f946ba3a3..8d7e70df9 100644 --- a/src/t_string.cpp +++ b/src/t_string.cpp @@ -637,6 +637,12 @@ void decrbyCommand(client *c) { long long incr; if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != C_OK) return; + /* -LLONG_MIN does not fit in a long long and wraps back to LLONG_MIN, which + * would let the overflow check in incrDecrCommand() pass and store a wrong value. */ + 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 dbf45e455..de179dd58 100644 --- a/tests/unit/type/incr.tcl +++ b/tests/unit/type/incr.tcl @@ -54,6 +54,16 @@ start_server {tags {"incr"}} { r decrby novar 17179869185 } {-1} + test {DECRBY against LLONG_MIN fails instead of wrapping} { + r set novar 5 + assert_error {*decrement would overflow*} {r decrby novar -9223372036854775808} + # the stored value must be left untouched + assert_equal 5 [r get novar] + r set novar -5 + assert_error {*decrement would overflow*} {r decrby novar -9223372036854775808} + assert_equal -5 [r get novar] + } + test {INCR uses shared objects in the 0-9999 range} { r set foo -1 r incr foo