about summary refs log tree commit diff
path: root/support/timespec-sub.c
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2019-10-31 14:46:44 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2019-10-31 17:03:04 -0300
commitc1dac8f83ecda0d23f92ddacad52818f658988a4 (patch)
tree2ee6749ab71e810cc45780392e3fa32e6beb33d6 /support/timespec-sub.c
parent356ced8dcb2c65f2d61eaa0fec32fa4b6628be16 (diff)
downloadglibc-c1dac8f83ecda0d23f92ddacad52818f658988a4.tar.gz
glibc-c1dac8f83ecda0d23f92ddacad52818f658988a4.tar.xz
glibc-c1dac8f83ecda0d23f92ddacad52818f658988a4.zip
Sync timespec-{add,sub} with gnulib
It sync with gnulib commit 06011ed74e978613422aca43c0bd92dc44213933.

Reviewed-by: Paul Eggert <eggert@cs.ucla.edu>
Diffstat (limited to 'support/timespec-sub.c')
-rw-r--r--support/timespec-sub.c20
1 files changed, 7 insertions, 13 deletions
diff --git a/support/timespec-sub.c b/support/timespec-sub.c
index 0fe6517109..cf16ed7832 100644
--- a/support/timespec-sub.c
+++ b/support/timespec-sub.c
@@ -33,36 +33,30 @@ timespec_sub (struct timespec a, struct timespec b)
   time_t bs = b.tv_sec;
   int ns = a.tv_nsec - b.tv_nsec;
   int rns = ns;
-  time_t tmin = TYPE_MINIMUM (time_t);
-  time_t tmax = TYPE_MAXIMUM (time_t);
 
   if (ns < 0)
     {
       rns = ns + TIMESPEC_HZ;
-      if (bs < tmax)
-        bs++;
+      time_t bs1;
+      if (!INT_ADD_WRAPV (bs, 1, &bs1))
+        bs = bs1;
       else if (- TYPE_SIGNED (time_t) < rs)
         rs--;
       else
         goto low_overflow;
     }
 
-  /* INT_SUBTRACT_WRAPV is not appropriate since time_t might be unsigned.
-     In theory time_t might be narrower than int, so plain
-     INT_SUBTRACT_OVERFLOW does not suffice.  */
-  if (! INT_SUBTRACT_OVERFLOW (rs, bs) && tmin <= rs - bs && rs - bs <= tmax)
-    rs -= bs;
-  else
+  if (INT_SUBTRACT_WRAPV (rs, bs, &rs))
     {
-      if (rs < 0)
+      if (0 < bs)
         {
         low_overflow:
-          rs = tmin;
+          rs = TYPE_MINIMUM (time_t);
           rns = 0;
         }
       else
         {
-          rs = tmax;
+          rs = TYPE_MAXIMUM (time_t);
           rns = TIMESPEC_HZ - 1;
         }
     }