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