Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/t_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/type/incr.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down