changeset 15323:b4b10d174a9d

nanosleep: simplify carrying * lib/nanosleep.c (nanosleep): Use the requested tv_nsec for the first call to the underyling nanosleep, not for the last one. This doesn't fix any bugs, but it simplifies the computation of the remaining delay. Found while auditing integer overflow issues.
author Paul Eggert <eggert@cs.ucla.edu>
date Sat, 25 Jun 2011 11:55:20 -0700
parents c0796192673c
children 04f6eb9c0c53
files ChangeLog lib/nanosleep.c
diffstat 2 files changed, 9 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sat Jun 25 01:56:52 2011 -0700
+++ b/ChangeLog	Sat Jun 25 11:55:20 2011 -0700
@@ -1,5 +1,11 @@
 2011-06-25  Paul Eggert  <eggert@cs.ucla.edu>
 
+	nanosleep: simplify carrying
+	* lib/nanosleep.c (nanosleep): Use the requested tv_nsec for the
+	first call to the underyling nanosleep, not for the last one.
+	This doesn't fix any bugs, but it simplifies the computation of
+	the remaining delay.  Found while auditing integer overflow issues.
+
 	dup2: remove test for existence of fcntl
 	* m4/dup2.m4 (gl_FUNC_DUP2): Use "#ifdef FD_CLOEXEC", not
 	"#if HAVE_FCNTL", in the configure-time test program.
--- a/lib/nanosleep.c	Sat Jun 25 01:56:52 2011 -0700
+++ b/lib/nanosleep.c	Sat Jun 25 11:55:20 2011 -0700
@@ -65,7 +65,7 @@
     const time_t limit = 24 * 24 * 60 * 60;
     time_t seconds = requested_delay->tv_sec;
     struct timespec intermediate;
-    intermediate.tv_nsec = 0;
+    intermediate.tv_nsec = requested_delay->tv_nsec;
 
     while (limit < seconds)
       {
@@ -76,20 +76,12 @@
         if (result)
           {
             if (remaining_delay)
-              {
-                remaining_delay->tv_sec += seconds;
-                remaining_delay->tv_nsec += requested_delay->tv_nsec;
-                if (BILLION <= requested_delay->tv_nsec)
-                  {
-                    remaining_delay->tv_sec++;
-                    remaining_delay->tv_nsec -= BILLION;
-                  }
-              }
+              remaining_delay->tv_sec += seconds;
             return result;
           }
+        intermediate.tv_nsec = 0;
       }
     intermediate.tv_sec = seconds;
-    intermediate.tv_nsec = requested_delay->tv_nsec;
     return nanosleep (&intermediate, remaining_delay);
   }
 }