From 08c3f0bca6178a1e418da0ea30e934f040a699c3 Mon Sep 17 00:00:00 2001 From: guybe7 Date: Thu, 3 Dec 2020 19:36:48 +0100 Subject: [PATCH] Modules: Fix an integer sign bug in moduleTimerHandler (#8131) bug was introduced in 1a91a2700b (cherry picked from commit 2f41a3856845265ffc6cc3a35524883a8690cff7) --- src/module.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/module.c b/src/module.c index ae7fa22b2..afea0063e 100644 --- a/src/module.c +++ b/src/module.c @@ -5443,8 +5443,11 @@ int moduleTimerHandler(struct aeEventLoop *eventLoop, long long id, void *client } else { /* We call ustime() again instead of using the cached 'now' so that * 'next_period' isn't affected by the time it took to execute - * previous calls to 'callback. */ - next_period = (expiretime-ustime())/1000; /* Scale to milliseconds. */ + * previous calls to 'callback. + * We need to cast 'expiretime' so that the compiler will not treat + * the difference as unsigned (Causing next_period to be huge) in + * case expiretime < ustime() */ + next_period = ((long long)expiretime-ustime())/1000; /* Scale to milliseconds. */ break; } } @@ -5488,7 +5491,8 @@ RedisModuleTimerID RM_CreateTimer(RedisModuleCtx *ctx, mstime_t period, RedisMod /* We need to install the main event loop timer if it's not already * installed, or we may need to refresh its period if we just installed - * a timer that will expire sooner than any other else. */ + * a timer that will expire sooner than any other else (i.e. the timer + * we just installed is the first timer in the Timers rax). */ if (aeTimer != -1) { raxIterator ri; raxStart(&ri,Timers);