bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] nanosleep: simplify carrying


From: Paul Eggert
Subject: [PATCH 1/2] nanosleep: simplify carrying
Date: Sat, 25 Jun 2011 15:09:43 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110516 Thunderbird/3.1.10

* 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.
---
 ChangeLog       |    6 ++++++
 lib/nanosleep.c |   14 +++-----------
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 14d3b0b..8e39acf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2011-06-25  Paul Eggert  <address@hidden>
 
+       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.
diff --git a/lib/nanosleep.c b/lib/nanosleep.c
index ab5cc89..aae2653 100644
--- a/lib/nanosleep.c
+++ b/lib/nanosleep.c
@@ -65,7 +65,7 @@ nanosleep (const struct timespec *requested_delay,
     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 @@ nanosleep (const struct timespec *requested_delay,
         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);
   }
 }
-- 
1.7.4.4





reply via email to

[Prev in Thread] Current Thread [Next in Thread]