Commit 598ee1ad authored by Simon Marlow's avatar Simon Marlow

Fix #7087 (integer overflow in getDelayTarget())

parent 9e7acbe7
......@@ -156,6 +156,8 @@ void _assertFail(const char *filename, unsigned int linenum)
#define TIME_RESOLUTION 1000000000
typedef StgInt64 Time;
#define TIME_MAX HS_INT64_MAX
#if TIME_RESOLUTION == 1000000000
// I'm being lazy, but it's awkward to define fully general versions of these
#define TimeToUS(t) ((t) / 1000)
......
......@@ -67,9 +67,18 @@ static LowResTime getLowResTimeOfDay(void)
*/
LowResTime getDelayTarget (HsInt us)
{
// round up the target time, because we never want to sleep *less*
// than the desired amount.
return TimeToLowResTimeRoundUp(getProcessElapsedTime() + USToTime(us));
Time elapsed;
elapsed = getProcessElapsedTime();
// If the desired target would be larger than the maximum Time,
// default to the maximum Time. (#7087)
if (us > TimeToUS(TIME_MAX - elapsed)) {
return TimeToLowResTimeRoundDown(TIME_MAX);
} else {
// round up the target time, because we never want to sleep *less*
// than the desired amount.
return TimeToLowResTimeRoundUp(elapsed + USToTime(us));
}
}
/* There's a clever trick here to avoid problems when the time wraps
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment