]> Savannah Git Hosting - gnulib.git/commitdiff
gethrxtime: remove incorrect overflow detection
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 22 Dec 2019 20:38:22 +0000 (12:38 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 22 Dec 2019 20:45:34 +0000 (12:45 -0800)
This is part of a patch written by Bruno Haible:
https://lists.gnu.org/r/bug-gnulib/2019-12/msg00192.html
* lib/xtime.h (xtime_make): Remove attempt to prevent internal
integer overflow, as it didn’t suffice.  This reverts the xtime.h
part of 2018-10-12T04:46:09Z!akim.demaille@gmail.com, which I
cannot now see the need for anyway (even in cases where it works),
as the patch is helpful only when the signs of S and NS disagree,
and all callers pass nonnegative values for S and NS.
Instead, add a comment saying args should be nonnegative.

ChangeLog
lib/xtime.h

index b5dedb14da582d496a3740771e394c51ab3f4eb9..25c8c4755fc2f80edd3ae4cad8292c842383c94f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2019-12-22  Bruno Haible  <bruno@clisp.org>
+
+       gethrxtime: remove incorrect overflow detection
+       * lib/xtime.h (xtime_make): Remove attempt to prevent internal
+       integer overflow, as it didn’t suffice.  This reverts the xtime.h
+       part of 2018-10-12T04:46:09Z!akim.demaille@gmail.com, which I
+       cannot now see the need for anyway (even in cases where it works),
+       as the patch is helpful only when the signs of S and NS disagree,
+       and all callers pass nonnegative values for S and NS.
+
 2019-12-22  Bruno Haible  <bruno@clisp.org>
 
        setlocale-null: Add standalone include file.
index 77f1c30faa584a6c7c4cfac6470f034ccdbedec8..a442da5c0e15fa6a0a05228822228646013cb3ed 100644 (file)
@@ -38,16 +38,18 @@ extern "C" {
 #endif
 
 /* Return an extended time value that contains S seconds and NS
-   nanoseconds.  */
+   nanoseconds.  S and NS should be nonnegative; otherwise, integer
+   overflow can occur even if the result is in range.  */
 XTIME_INLINE xtime_t
 xtime_make (xtime_t s, long int ns)
 {
-  const long int giga = 1000 * 1000 * 1000;
-  s += ns / giga;
-  ns %= giga;
   return XTIME_PRECISION * s + ns;
 }
 
+/* The following functions split an extended time value:
+   T = XTIME_PRECISION * xtime_sec (T) + xtime_nsec (T)
+   with  0 <= xtime_nsec (T) < XTIME_PRECISION.  */
+
 /* Return the number of seconds in T, which must be nonnegative.  */
 XTIME_INLINE xtime_t
 xtime_nonnegative_sec (xtime_t t)