threads: fix wait time overflow check

When passing a very large timeout to mpthread_cond_timed_wait(), the
calculations could overflow, setting tv_sec to a negative value, and
making the pthread_cond_timed_wait() call return immediately. This
accidentally made Lua support poll and burn CPU for no reason.

The existing overflow check was ineffective on 32 bit systems. tv_sec is
usually a long, so adding INT_MAX to it will usually not overflow on 64
bit systems, but on 32 bit systems it's guaranteed to overflow. Simply
fix by clamping against a relatively high value. This will work until 1
week before the UNIX time wraps around in 32 bits.
This commit is contained in:
wm4
2014-02-26 20:44:27 +01:00
parent 412bb336ab
commit 9ccbc03ab1

View File

@@ -39,8 +39,8 @@ static void get_pthread_time(struct timespec *out_ts)
static void timespec_add_seconds(struct timespec *ts, double seconds)
{
if (seconds > INT_MAX)
seconds = INT_MAX;
// clamp to 1 week to avoid tv_sec overflows
seconds = MPMIN(seconds, 60 * 60 * 24 * 7);
unsigned long secs = (int)seconds;
unsigned long nsecs = (seconds - secs) * 1000000000UL;
if (nsecs + ts->tv_nsec >= 1000000000UL) {