chiark / gitweb /
util: avoid divide by zero FPE
authorDave Reisner <dreisner@archlinux.org>
Mon, 29 Oct 2012 19:49:34 +0000 (15:49 -0400)
committerDave Reisner <dreisner@archlinux.org>
Mon, 29 Oct 2012 19:53:03 +0000 (15:53 -0400)
In early userspace, if kernel initialization happens extremely quickly,
a call to systemd-timestamp can potentially result in division by zero.
Ensure that the check in timespec_load, which only makes sense if tv_sec
is greater than zero, is guarded by this condition.

src/shared/util.c

index e2f8b1fb7812b46fbae18109fdd3f0f38aa0031b..9a45e6058e200813fed1bae7ff9d27f0ebb164a5 100644 (file)
@@ -148,7 +148,8 @@ usec_t timespec_load(const struct timespec *ts) {
             ts->tv_nsec == (long) -1)
                 return (usec_t) -1;
 
-        if (USEC_PER_SEC > ((UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / (usec_t) ts->tv_sec))
+        if (ts->tv_sec > 0 &&
+            USEC_PER_SEC > ((UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / (usec_t) ts->tv_sec))
                 return (usec_t) -1;
 
         return