This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Time-HiRes: avoid going negative on struct timespec
authorJarkko Hietaniemi <jhi@iki.fi>
Fri, 4 Mar 2016 02:11:38 +0000 (21:11 -0500)
committerJarkko Hietaniemi <jhi@iki.fi>
Fri, 4 Mar 2016 22:46:47 +0000 (17:46 -0500)
dist/Time-HiRes/HiRes.xs

index 991c101..c6ac977 100644 (file)
@@ -902,11 +902,21 @@ static int clock_nanosleep(int clock_id, int flags,
 static NV
 nsec_without_unslept(struct timespec *sleepfor,
                      const struct timespec *unslept) {
-  sleepfor->tv_sec -= unslept->tv_sec;
-  sleepfor->tv_nsec -= unslept->tv_nsec;
-  if (sleepfor->tv_nsec < 0) {
-    sleepfor->tv_sec--;
-    sleepfor->tv_nsec += IV_1E9;
+  if (sleepfor->tv_sec >= unslept->tv_sec) {
+    sleepfor->tv_sec -= unslept->tv_sec;
+    if (sleepfor->tv_nsec >= unslept->tv_nsec) {
+      sleepfor->tv_nsec -= unslept->tv_nsec;
+    } else if (sleepfor->tv_sec > 0) {
+      sleepfor->tv_sec--;
+      sleepfor->tv_nsec += IV_1E9;
+      sleepfor->tv_nsec -= unslept->tv_nsec;
+    } else {
+      sleepfor->tv_sec = 0;
+      sleepfor->tv_nsec = 0;
+    }
+  } else {
+    sleepfor->tv_sec = 0;
+    sleepfor->tv_nsec = 0;
   }
   return ((NV)sleepfor->tv_sec) * NV_1E9 + ((NV)sleepfor->tv_nsec);
 }