From 9273d09dd46353015406c26cd7d3826b78109a38 Mon Sep 17 00:00:00 2001 From: Binbin Date: Wed, 24 Nov 2021 15:39:23 +0800 Subject: [PATCH] Add tests to cover EXPIRE overflow fix (#9839) In #8287, some overflow checks have been added. But when `when *= 1000` overflows, it will become a positive number. And the check not able to catch it. The key will be added with a short expiration time and will deleted a few seconds later. In #9601, will check the overflow after `*=` and return an error first, and avoiding this situation. In this commit, added some tests to cover those code paths. Found it in #9825, and close it. --- tests/unit/expire.tcl | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/unit/expire.tcl b/tests/unit/expire.tcl index 3b430f713..8c5304fad 100644 --- a/tests/unit/expire.tcl +++ b/tests/unit/expire.tcl @@ -268,9 +268,17 @@ start_server {tags {"expire"}} { test {EXPIRE with big integer overflows when converted to milliseconds} { r set foo bar - catch {r EXPIRE foo 10000000000000000} e - set e - } {ERR invalid expire time in expire} + + # Hit `when > LLONG_MAX - basetime` + assert_error "ERR invalid expire time in expire" {r EXPIRE foo 9223370399119966} + + # Hit `when > LLONG_MAX / 1000` + assert_error "ERR invalid expire time in expire" {r EXPIRE foo 9223372036854776} + assert_error "ERR invalid expire time in expire" {r EXPIRE foo 10000000000000000} + assert_error "ERR invalid expire time in expire" {r EXPIRE foo 18446744073709561} + + assert_equal {-1} [r ttl foo] + } test {PEXPIRE with big integer overflow when basetime is added} { r set foo bar @@ -280,8 +288,11 @@ start_server {tags {"expire"}} { test {EXPIRE with big negative integer} { r set foo bar - catch {r EXPIRE foo -9999999999999999} e - assert_match {ERR invalid expire time in expire} $e + + # Hit `when < LLONG_MIN / 1000` + assert_error "ERR invalid expire time in expire" {r EXPIRE foo -9223372036854776} + assert_error "ERR invalid expire time in expire" {r EXPIRE foo -9999999999999999} + r ttl foo } {-1}