From 114b35bbbc2c1b1c62d667fe61350b74e999df74 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Fri, 16 Jan 2015 17:08:29 -0800 Subject: [PATCH] Fix overflow in KTime_GetEpochNS --- sys/kern/ktime.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sys/kern/ktime.c b/sys/kern/ktime.c index 41fc445..3cb0103 100644 --- a/sys/kern/ktime.c +++ b/sys/kern/ktime.c @@ -194,7 +194,12 @@ KTime_GetEpochNS() Spinlock_Lock(&ktimeLock); tscDiff = Time_GetTSC() - ktimeLastTSC; - epoch = (ktimeLastEpoch * 1000000000) + (tscDiff * 1000000000 / ticksPerSecond); + /* + * This is ugly but it avoids overflowing tscDiff to time computation. + * Note that the bottom bits of ticksPerSecond are not significant so it is + * okay to discard them. + */ + epoch = (ktimeLastEpoch * 1000000000ULL) + (tscDiff * 1000000ULL / ticksPerSecond * 1000ULL); Spinlock_Unlock(&ktimeLock); return epoch;