diff mbox

[v3] Y2038: make __difftime compatible with 64-bit time

Message ID 20181013211641.5215-1-albert.aribaud@3adev.fr
State New
Headers show

Commit Message

Albert ARIBAUD Oct. 13, 2018, 9:16 p.m. UTC
Provide a 64-bit-time version of __difftime (but do not assume
__time64_t is a signed int so that Gnulib can reuse the code)
and make the 32-bit version a wrapper of it.

Current difftime expects two time_t arguments and returns a
double. To preserve source-code compatibility, its 64-bit-time
equivalent expects two __time64_t arguments but still returns
a double.

This patch was tested by running 'make check' on branch
master then applying this patch and its two predecessors and
running 'make check' again, and checking that both 'make check'
yield identical results. This was done on x86_64-linux-gnu and
i686-linux-gnu.

This patch was also functionally tested with an ad hoc userland
C program which checks the result of difftime for various pairs
of 32-bit and, for 64-bit builds, of 64-bit time_t values too.
The program was built and run against a glibc with and without
the patch, and the results compared to ensure the patch does
not change the behavior of difftime.

* include/time.h (__difftime64): Add.
* time/difftime.c (subtract): convert to 64-bit time.
* time/difftime.c (__difftime64): Add.
* time/difftime.c (__difftime): Wrap around __difftime64.
---
 include/time.h  |  6 ++++++
 time/difftime.c | 31 ++++++++++++++++++++++---------
 2 files changed, 28 insertions(+), 9 deletions(-)

Comments

Albert ARIBAUD Oct. 13, 2018, 9:18 p.m. UTC | #1
On Sat, 13 Oct 2018 23:16:41 +0200, "Albert ARIBAUD (3ADEV)"
<albert.aribaud@3adev.fr> wrote :

> Provide a 64-bit-time version of __difftime (but do not assume
> __time64_t is a signed int so that Gnulib can reuse the code)
> and make the 32-bit version a wrapper of it.

Note: this patch applies over the __time64_t patch series v10. The
whole series can be found as branch aaribaud/y2038 on the glibc repo.

Cordialement,
Albert ARIBAUD
3ADEV
Albert ARIBAUD Dec. 5, 2018, 11:05 a.m. UTC | #2
On Sat, 13 Oct 2018 23:18:28 +0200, Albert ARIBAUD
<albert.aribaud@3adev.fr> wrote :

> On Sat, 13 Oct 2018 23:16:41 +0200, "Albert ARIBAUD (3ADEV)"
> <albert.aribaud@3adev.fr> wrote :
> 
> > Provide a 64-bit-time version of __difftime (but do not assume
> > __time64_t is a signed int so that Gnulib can reuse the code)
> > and make the 32-bit version a wrapper of it.  
> 
> Note: this patch applies over the __time64_t patch series v10. The
> whole series can be found as branch aaribaud/y2038 on the glibc repo.

Any comments on this patch?

Cordialement,
Albert ARIBAUD
3ADEV
Albert ARIBAUD Dec. 19, 2018, 8:30 p.m. UTC | #3
On Wed, 5 Dec 2018 12:05:29 +0100, Albert ARIBAUD
<albert.aribaud@3adev.fr> wrote :

> On Sat, 13 Oct 2018 23:18:28 +0200, Albert ARIBAUD
> <albert.aribaud@3adev.fr> wrote :
> 
> > On Sat, 13 Oct 2018 23:16:41 +0200, "Albert ARIBAUD (3ADEV)"
> > <albert.aribaud@3adev.fr> wrote :
> >   
> > > Provide a 64-bit-time version of __difftime (but do not assume
> > > __time64_t is a signed int so that Gnulib can reuse the code)
> > > and make the 32-bit version a wrapper of it.    
> > 
> > Note: this patch applies over the __time64_t patch series v10. The
> > whole series can be found as branch aaribaud/y2038 on the glibc repo.  
> 
> Any comments on this patch?

Ping -- just rebased and re-ran make checks on this patch, still good.

Cordialement,
Albert ARIBAUD
3ADEV
Joseph Myers Dec. 19, 2018, 8:40 p.m. UTC | #4
On Wed, 19 Dec 2018, Albert ARIBAUD wrote:

> On Wed, 5 Dec 2018 12:05:29 +0100, Albert ARIBAUD
> <albert.aribaud@3adev.fr> wrote :
> 
> > On Sat, 13 Oct 2018 23:18:28 +0200, Albert ARIBAUD
> > <albert.aribaud@3adev.fr> wrote :
> > 
> > > On Sat, 13 Oct 2018 23:16:41 +0200, "Albert ARIBAUD (3ADEV)"
> > > <albert.aribaud@3adev.fr> wrote :
> > >   
> > > > Provide a 64-bit-time version of __difftime (but do not assume
> > > > __time64_t is a signed int so that Gnulib can reuse the code)
> > > > and make the 32-bit version a wrapper of it.    
> > > 
> > > Note: this patch applies over the __time64_t patch series v10. The
> > > whole series can be found as branch aaribaud/y2038 on the glibc repo.  
> > 
> > Any comments on this patch?
> 
> Ping -- just rebased and re-ran make checks on this patch, still good.

<https://sourceware.org/ml/libc-alpha/2018-10/msg00225.html> is missing 
libc_hidden_proto / libc_hidden_def for __difftime64, and is missing ".  " 
at the end of a comment.  (If that's not the version for which you want 
review, you should give the URL to a patch when pinging it....)
Albert ARIBAUD Dec. 19, 2018, 9:17 p.m. UTC | #5
Hi Joseph,

On Wed, 19 Dec 2018 20:40:14 +0000, Joseph Myers
<joseph@codesourcery.com> wrote :

> On Wed, 19 Dec 2018, Albert ARIBAUD wrote:
> 
> > On Wed, 5 Dec 2018 12:05:29 +0100, Albert ARIBAUD
> > <albert.aribaud@3adev.fr> wrote :
> >   
> > > On Sat, 13 Oct 2018 23:18:28 +0200, Albert ARIBAUD
> > > <albert.aribaud@3adev.fr> wrote :
> > >   
>  [...]  
>  [...]  
>  [...]  
> > > 
> > > Any comments on this patch?  
> > 
> > Ping -- just rebased and re-ran make checks on this patch, still good.  
> 
> <https://sourceware.org/ml/libc-alpha/2018-10/msg00225.html> is missing 
> libc_hidden_proto / libc_hidden_def for __difftime64, and is missing ".  " 
> at the end of a comment.  (If that's not the version for which you want 
> review, you should give the URL to a patch when pinging it....)

sorry for the unclear ref, and thhanks for the heads-up. v4 on its way.


Cordialement,
Albert ARIBAUD
3ADEV
diff mbox

Patch

diff --git a/include/time.h b/include/time.h
index 2710b4521a..7b435c00c2 100644
--- a/include/time.h
+++ b/include/time.h
@@ -129,6 +129,12 @@  extern char * __strptime_internal (const char *rp, const char *fmt,
 				   struct tm *tm, void *statep,
 				   locale_t locparam) attribute_hidden;
 
+#if __TIMESIZE == 64
+# define __difftime64 __difftime
+#else
+extern double __difftime64 (__time64_t time1, __time64_t time0);
+#endif
+
 extern double __difftime (time_t time1, time_t time0);
 
 /* Use in the clock_* functions.  Size of the field representing the
diff --git a/time/difftime.c b/time/difftime.c
index 7c5dd9898b..e3a4e57b44 100644
--- a/time/difftime.c
+++ b/time/difftime.c
@@ -31,9 +31,9 @@ 
    time_t is known to be an integer type.  */
 
 static double
-subtract (time_t time1, time_t time0)
+subtract (__time64_t time1, __time64_t time0)
 {
-  if (! TYPE_SIGNED (time_t))
+  if (! TYPE_SIGNED (__time64_t))
     return time1 - time0;
   else
     {
@@ -76,9 +76,9 @@  subtract (time_t time1, time_t time0)
              1 is unsigned in C, so it need not be compared to zero.  */
 
 	  uintmax_t hdt = dt / 2;
-	  time_t ht1 = time1 / 2;
-	  time_t ht0 = time0 / 2;
-	  time_t dht = ht1 - ht0;
+	  __time64_t ht1 = time1 / 2;
+	  __time64_t ht0 = time0 / 2;
+	  __time64_t dht = ht1 - ht0;
 
 	  if (2 < dht - hdt + 1)
 	    {
@@ -99,18 +99,18 @@  subtract (time_t time1, time_t time0)
 
 /* Return the difference between TIME1 and TIME0.  */
 double
-__difftime (time_t time1, time_t time0)
+__difftime64 (__time64_t time1, __time64_t time0)
 {
   /* Convert to double and then subtract if no double-rounding error could
      result.  */
 
-  if (TYPE_BITS (time_t) <= DBL_MANT_DIG
-      || (TYPE_FLOATING (time_t) && sizeof (time_t) < sizeof (long double)))
+  if (TYPE_BITS (__time64_t) <= DBL_MANT_DIG
+      || (TYPE_FLOATING (__time64_t) && sizeof (__time64_t) < sizeof (long double)))
     return (double) time1 - (double) time0;
 
   /* Likewise for long double.  */
 
-  if (TYPE_BITS (time_t) <= LDBL_MANT_DIG || TYPE_FLOATING (time_t))
+  if (TYPE_BITS (__time64_t) <= LDBL_MANT_DIG || TYPE_FLOATING (__time64_t))
     return (long double) time1 - (long double) time0;
 
   /* Subtract the smaller integer from the larger, convert the difference to
@@ -118,4 +118,17 @@  __difftime (time_t time1, time_t time0)
 
   return time1 < time0 ? - subtract (time0, time1) : subtract (time1, time0);
 }
+
+/* Provide a 32-bit wrapper if needed */
+
+#if __TIMESIZE != 64
+
+double
+__difftime (time_t time1, time_t time0)
+{
+  return __difftime64 (time1, time0);
+}
+
+#endif
+
 strong_alias (__difftime, difftime)