diff options
author | wm4 <wm4@nowhere> | 2014-05-22 22:32:57 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-05-22 22:36:42 +0200 |
commit | c9b68957c9f79543063dcfa5c8baa417b43e285e (patch) | |
tree | 7fba72eb668195c9570020f920a0a09ae013599b /osdep/timer.c | |
parent | cb2e784c075bed1a3220cf46dc072171f4f632e5 (diff) | |
download | mpv-c9b68957c9f79543063dcfa5c8baa417b43e285e.tar.bz2 mpv-c9b68957c9f79543063dcfa5c8baa417b43e285e.tar.xz |
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).
Diffstat (limited to 'osdep/timer.c')
-rw-r--r-- | osdep/timer.c | 9 |
1 files changed, 5 insertions, 4 deletions
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) |