[RFC,11/12] y2038: Convert timespec* files in ./support to be Y2038 safe

Message ID 20200601140740.16371-12-lukma@denx.de
State Dropped
Delegated to: Lukasz Majewski
Headers
Series y2038: Convert timespec_{sub|add|create} in support to be Y2038 safe |

Commit Message

Lukasz Majewski June 1, 2020, 2:07 p.m. UTC
  After this change functions in ./support/timespec* files:
- __timespec_add
- __timespec_sub
- test_timespec_before_impl
- test_timespec_equal_or_after_impl

are Y2038 safe, as the struct timespec and time_t have been replaced with
struct __timespec64 and __time64_t respectively.
---
 support/timespec-add.c | 10 +++++-----
 support/timespec-sub.c | 16 ++++++++--------
 support/timespec.c     | 12 ++++++------
 support/timespec.h     | 17 +++++++++--------
 4 files changed, 28 insertions(+), 27 deletions(-)
  

Patch

diff --git a/support/timespec-add.c b/support/timespec-add.c
index a2eb43791e..d2bcdae8de 100644
--- a/support/timespec-add.c
+++ b/support/timespec-add.c
@@ -25,11 +25,11 @@ 
 
 #include "intprops.h"
 
-struct timespec
-__timespec_add (struct timespec a, struct timespec b)
+struct __timespec64
+__timespec_add (struct __timespec64 a, struct __timespec64 b)
 {
-  time_t rs = a.tv_sec;
-  time_t bs = b.tv_sec;
+  __time64_t rs = a.tv_sec;
+  __time64_t bs = b.tv_sec;
   int ns = a.tv_nsec + b.tv_nsec;
   int nsd = ns - TIMESPEC_HZ;
   int rns = ns;
@@ -37,7 +37,7 @@  __timespec_add (struct timespec a, struct timespec b)
   if (0 <= nsd)
     {
       rns = nsd;
-      time_t bs1;
+      __time64_t bs1;
       if (!INT_ADD_WRAPV (bs, 1, &bs1))
         bs = bs1;
       else if (rs < 0)
diff --git a/support/timespec-sub.c b/support/timespec-sub.c
index 836f1e4f70..f0ae821ea4 100644
--- a/support/timespec-sub.c
+++ b/support/timespec-sub.c
@@ -26,21 +26,21 @@ 
 
 #include "intprops.h"
 
-struct timespec
-__timespec_sub (struct timespec a, struct timespec b)
+struct __timespec64
+__timespec_sub (struct __timespec64 a, struct __timespec64 b)
 {
-  time_t rs = a.tv_sec;
-  time_t bs = b.tv_sec;
+  __time64_t rs = a.tv_sec;
+  __time64_t bs = b.tv_sec;
   int ns = a.tv_nsec - b.tv_nsec;
   int rns = ns;
 
   if (ns < 0)
     {
       rns = ns + TIMESPEC_HZ;
-      time_t bs1;
+      __time64_t bs1;
       if (!INT_ADD_WRAPV (bs, 1, &bs1))
         bs = bs1;
-      else if (- TYPE_SIGNED (time_t) < rs)
+      else if (- TYPE_SIGNED (__time64_t) < rs)
         rs--;
       else
         goto low_overflow;
@@ -51,12 +51,12 @@  __timespec_sub (struct timespec a, struct timespec b)
       if (0 < bs)
         {
         low_overflow:
-          rs = TYPE_MINIMUM (time_t);
+          rs = TYPE_MINIMUM (__time64_t);
           rns = 0;
         }
       else
         {
-          rs = TYPE_MAXIMUM (time_t);
+          rs = TYPE_MAXIMUM (__time64_t);
           rns = TIMESPEC_HZ - 1;
         }
     }
diff --git a/support/timespec.c b/support/timespec.c
index 3647caed36..b69ce61c93 100644
--- a/support/timespec.c
+++ b/support/timespec.c
@@ -22,14 +22,14 @@ 
 
 void
 test_timespec_before_impl (const char *file, int line,
-			   const struct timespec left,
-			   const struct timespec right)
+			   const struct __timespec64 left,
+			   const struct __timespec64 right)
 {
   if (left.tv_sec > right.tv_sec
       || (left.tv_sec == right.tv_sec
 	  && left.tv_nsec > right.tv_nsec)) {
     support_record_failure ();
-    const struct timespec diff = __timespec_sub (left, right);
+    const struct __timespec64 diff = __timespec_sub (left, right);
     printf ("%s:%d: %jd.%09jds not before %jd.%09jds "
 	    "(difference %jd.%09jds)\n",
 	    file, line,
@@ -41,14 +41,14 @@  test_timespec_before_impl (const char *file, int line,
 
 void
 test_timespec_equal_or_after_impl (const char *file, int line,
-				   const struct timespec left,
-				   const struct timespec right)
+				   const struct __timespec64 left,
+				   const struct __timespec64 right)
 {
   if (left.tv_sec < right.tv_sec
       || (left.tv_sec == right.tv_sec
 	  && left.tv_nsec < right.tv_nsec)) {
     support_record_failure ();
-    const struct timespec diff = __timespec_sub (right, left);
+    const struct __timespec64 diff = __timespec_sub (right, left);
     printf ("%s:%d: %jd.%09jds not after %jd.%09jds "
 	    "(difference %jd.%09jds)\n",
 	    file, line,
diff --git a/support/timespec.h b/support/timespec.h
index 8de40d25f4..f0b054f5e9 100644
--- a/support/timespec.h
+++ b/support/timespec.h
@@ -23,10 +23,11 @@ 
 #include <time.h>
 #include <support/check.h>
 #include <support/xtime.h>
+#include <struct___timespec64.h>
 
-struct timespec __timespec_add (struct timespec, struct timespec)
+struct __timespec64 __timespec_add (struct __timespec64, struct __timespec64)
   __attribute__((const));
-struct timespec __timespec_sub (struct timespec, struct timespec)
+struct __timespec64 __timespec_sub (struct __timespec64, struct __timespec64)
   __attribute__((const));
 
 static inline struct __timespec64
@@ -41,12 +42,12 @@  __make_timespec (__time64_t s, long int ns)
 enum { TIMESPEC_HZ = 1000000000 };
 
 void test_timespec_before_impl (const char *file, int line,
-                                const struct timespec left,
-                                const struct timespec right);
+                                const struct __timespec64 left,
+                                const struct __timespec64 right);
 
 void test_timespec_equal_or_after_impl (const char *file, int line,
-                                        const struct timespec left,
-                                        const struct timespec right);
+                                        const struct __timespec64 left,
+                                        const struct __timespec64 right);
 
 /* Check that the timespec on the left represents a time before the
    time on the right. */
@@ -55,7 +56,7 @@  void test_timespec_equal_or_after_impl (const char *file, int line,
 
 #define TEST_TIMESPEC_BEFORE_NOW(left, clockid)                 \
   ({                                                            \
-    struct timespec now;                                        \
+    struct __timespec64 now;                                    \
     const int saved_errno = errno;                              \
     __xclock_gettime ((clockid), &now);                         \
     TEST_TIMESPEC_BEFORE ((left), now);                         \
@@ -69,7 +70,7 @@  void test_timespec_equal_or_after_impl (const char *file, int line,
 
 #define TEST_TIMESPEC_NOW_OR_AFTER(clockid, right)              \
   ({                                                            \
-    struct timespec now;                                        \
+    struct __timespec64 now;                                    \
     const int saved_errno = errno;                              \
     __xclock_gettime ((clockid), &now);                         \
     TEST_TIMESPEC_EQUAL_OR_AFTER (now, (right));                \