diff --git a/src/t_hash.c b/src/t_hash.c index d8d511c2740..8af6afcbb59 100644 --- a/src/t_hash.c +++ b/src/t_hash.c @@ -1300,7 +1300,7 @@ void hincrbyfloatCommand(client *c) { mstime_t expiry = EXPIRY_NONE; if (getLongDoubleFromObjectOrReply(c, c->argv[3], &incr, NULL) != C_OK) return; - if (isnan(incr) || isinf(incr)) { + if (isinf(incr)) { addReplyError(c, "value is NaN or Infinity"); return; } @@ -1320,8 +1320,8 @@ void hincrbyfloatCommand(client *c) { } value += incr; - if (isnan(value) || isinf(value)) { - addReplyError(c, "increment would produce NaN or Infinity"); + if (isinf(value)) { + addReplyError(c, "increment would produce Infinity"); return; } diff --git a/src/t_string.c b/src/t_string.c index dee6e718170..15a5ff43955 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -906,10 +906,6 @@ void increxCommand(client *c) { return; } value_ld = oldvalue_ld + incr_ld; - if (isnan(value_ld)) { - addReplyError(c, "Increment is not a valid float"); - return; - } if (isinf(value_ld)) { addReplyArrayLen(c, 2); addReplyHumanLongDouble(c, oldvalue_ld); diff --git a/tests/unit/type/hash.tcl b/tests/unit/type/hash.tcl index 5fcd639ef68..71f76d171cd 100644 --- a/tests/unit/type/hash.tcl +++ b/tests/unit/type/hash.tcl @@ -960,6 +960,21 @@ start_server {tags {"hash"}} { assert_error "*value is NaN or Infinity*" {r hincrbyfloat hfoo field +inf} assert_equal 0 [r exists hfoo] } {} {valgrind:skip} + + test {HINCRBYFLOAT rejects a NaN increment} { + r del hfoo + assert_error "*value is not a valid float*" {r hincrbyfloat hfoo field nan} + } + + test {HINCRBYFLOAT with an infinite field value} { + r del hfoo + r hset hfoo field inf + assert_error "*would produce Infinity*" {r hincrbyfloat hfoo field 1} + assert_equal inf [r hget hfoo field] + # An infinite increment is dropped before the result is ever computed. + assert_error "*value is NaN or Infinity*" {r hincrbyfloat hfoo field -inf} + assert_equal inf [r hget hfoo field] + } {} {valgrind:skip} } start_server {config "minimal.conf" tags {"hash" "external:skip"} overrides {io-threads 4 io-threads-always-active yes hash-max-listpack-entries 0}} { diff --git a/tests/unit/type/incr.tcl b/tests/unit/type/incr.tcl index 8c28458ee95..658ef352cd1 100644 --- a/tests/unit/type/incr.tcl +++ b/tests/unit/type/incr.tcl @@ -151,6 +151,17 @@ start_server {tags {"incr"}} { # perform divisions. } {ERR *would produce*} {valgrind:skip} + test {INCRBYFLOAT rejects a NaN increment} { + r set foo 0 + assert_error "*value is not a valid float*" {r incrbyfloat foo nan} + } + + test {INCRBYFLOAT with an infinite field value} { + r set foo inf + assert_error "*would produce NaN or Infinity*" {r incrbyfloat foo -inf} + assert_equal inf [r get foo] + } {} {valgrind:skip} + test {INCRBYFLOAT decrement} { r set foo 1 roundFloat [r incrbyfloat foo -1.1]