From afbaf59abba79a79ab6f4c659ff3d832e02a8e7f Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 8 Oct 2024 18:18:05 +0200 Subject: [PATCH] Cast to sc_tick type in conversion macros With the old macros definitions, the type of the result depended on the type of `sec`. In particular, if sec is a 32-bit type, sec * 1000000 was likely to overflow (even if the result was assigned to a sc_tick by the caller of the macro). This was the case on Windows, where the long type is a 32-bit signed integer: the --time-limit argument, expressed in seconds, was first parsed to a long value, then multiplied by 1000000 by the SC_TICK_FROM_SEC() macro, causing an overflow when the value was greater than 2147 (2^31 / 1000000). Fixes #5355 --- app/src/util/tick.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/src/util/tick.h b/app/src/util/tick.h index 2d941f23..b037734b 100644 --- a/app/src/util/tick.h +++ b/app/src/util/tick.h @@ -10,14 +10,14 @@ typedef int64_t sc_tick; #define SC_TICK_FREQ 1000000 // microsecond // To be adapted if SC_TICK_FREQ changes -#define SC_TICK_TO_NS(tick) ((tick) * 1000) -#define SC_TICK_TO_US(tick) (tick) -#define SC_TICK_TO_MS(tick) ((tick) / 1000) -#define SC_TICK_TO_SEC(tick) ((tick) / 1000000) -#define SC_TICK_FROM_NS(ns) ((ns) / 1000) -#define SC_TICK_FROM_US(us) (us) -#define SC_TICK_FROM_MS(ms) ((ms) * 1000) -#define SC_TICK_FROM_SEC(sec) ((sec) * 1000000) +#define SC_TICK_TO_NS(tick) ((sc_tick) (tick) * 1000) +#define SC_TICK_TO_US(tick) ((sc_tick) tick) +#define SC_TICK_TO_MS(tick) ((sc_tick) (tick) / 1000) +#define SC_TICK_TO_SEC(tick) ((sc_tick) (tick) / 1000000) +#define SC_TICK_FROM_NS(ns) ((sc_tick) (ns) / 1000) +#define SC_TICK_FROM_US(us) ((sc_tick) us) +#define SC_TICK_FROM_MS(ms) ((sc_tick) (ms) * 1000) +#define SC_TICK_FROM_SEC(sec) ((sc_tick) (sec) * 1000000) sc_tick sc_tick_now(void);