From c9b68957c9f79543063dcfa5c8baa417b43e285e Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 22 May 2014 22:32:57 +0200 Subject: timer: improve overflow checks Probably more correct and better readable. Although the special-casing of 0x1p63 is weird in terms of readability (the value itself is INT64_MAX+1, so it's already outside of range, but INT64_MAX is not exactly representable with double precision). --- osdep/timer.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'osdep/timer.c') diff --git a/osdep/timer.c b/osdep/timer.c index 286022db49..06ee7647e5 100644 --- a/osdep/timer.c +++ b/osdep/timer.c @@ -69,12 +69,13 @@ int64_t mp_time_relative_us(int64_t *t) int64_t mp_add_timeout(int64_t time_us, double timeout_sec) { assert(time_us > 0); // mp_time_us() returns strictly positive values - double t = timeout_sec * 1000 * 1000; - if (t >= (double)(INT64_MAX - time_us)) + double t = MPCLAMP(timeout_sec * (1000 * 1000), -0x1p63, 0x1p63); + int64_t ti = t == 0x1p63 ? INT64_MAX : t; + if (ti > INT64_MAX - time_us) return INT64_MAX; - if (t <= -(double)time_us) + if (ti <= -time_us) return 1; - return time_us + (int64_t)t; + return time_us + ti; } static void get_realtime(struct timespec *out_ts) -- cgit v1.2.3