Use 64-bit time_t interfaces in strftime and strptime (bug 30053)

Message ID mvmr0vhto02.fsf@suse.de
State Committed
Headers
Series Use 64-bit time_t interfaces in strftime and strptime (bug 30053) |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Andreas Schwab Jan. 26, 2023, 1:38 p.m. UTC
  Both functions use time_t only internally, so the ABI is not affected.
---
 time/Makefile           |  3 ++-
 time/strftime_l.c       |  4 ++++
 time/strptime_l.c       |  4 +++-
 time/tst-y2038-time64.c |  1 +
 time/tst-y2038.c        | 52 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 time/tst-y2038-time64.c
 create mode 100644 time/tst-y2038.c
  

Comments

Adhemerval Zanella Jan. 26, 2023, 1:47 p.m. UTC | #1
On 26/01/23 10:38, Andreas Schwab via Libc-alpha wrote:
> Both functions use time_t only internally, so the ABI is not affected.
> ---
>  time/Makefile           |  3 ++-
>  time/strftime_l.c       |  4 ++++
>  time/strptime_l.c       |  4 +++-
>  time/tst-y2038-time64.c |  1 +
>  time/tst-y2038.c        | 52 +++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 62 insertions(+), 2 deletions(-)
>  create mode 100644 time/tst-y2038-time64.c
>  create mode 100644 time/tst-y2038.c
> 
> diff --git a/time/Makefile b/time/Makefile
> index d86f2105c5..8b2e1d2398 100644
> --- a/time/Makefile
> +++ b/time/Makefile
> @@ -50,7 +50,7 @@ tests	:= test_time clocktest tst-posixtz tst-strptime tst_wcsftime \
>  	   tst-clock tst-clock2 tst-clock_nanosleep tst-cpuclock1 \
>  	   tst-adjtime tst-ctime tst-difftime tst-mktime4 tst-clock_settime \
>  	   tst-settimeofday tst-itimer tst-gmtime tst-timegm \
> -	   tst-timespec_get tst-timespec_getres
> +	   tst-timespec_get tst-timespec_getres tst-y2038
>  
>  tests-time64 := \
>    tst-adjtime-time64 \
> @@ -68,6 +68,7 @@ tests-time64 := \
>    tst-timegm-time64 \
>    tst-timespec_get-time64 \
>    tst-timespec_getres-time64 \
> +  tst-y2038-time64 \

I think it should be renamed to tst-strftime4-time64 to show it is testing
strftime.  The rest of the patch looks ok.

>    tst-y2039-time64
>  
>  include ../Rules
> diff --git a/time/strftime_l.c b/time/strftime_l.c
> index e09561c39c..402c6c4111 100644
> --- a/time/strftime_l.c
> +++ b/time/strftime_l.c
> @@ -159,6 +159,10 @@ extern char *tzname[];
>  #ifdef _LIBC
>  # define tzname __tzname
>  # define tzset __tzset
> +
> +# define time_t __time64_t
> +# define __gmtime_r(t, tp) __gmtime64_r (t, tp)
> +# define mktime(tp) __mktime64 (tp)
>  #endif
>  
>  #if !HAVE_TM_GMTOFF
> diff --git a/time/strptime_l.c b/time/strptime_l.c
> index 80fd705b8d..85c3249fcc 100644
> --- a/time/strptime_l.c
> +++ b/time/strptime_l.c
> @@ -30,8 +30,10 @@
>  #ifdef _LIBC
>  # define HAVE_LOCALTIME_R 0
>  # include "../locale/localeinfo.h"
> -#endif
>  
> +# define time_t __time64_t
> +# define __localtime_r(t, tp) __localtime64_r (t, tp)
> +#endif
>  
>  #if ! HAVE_LOCALTIME_R && ! defined localtime_r
>  # ifdef _LIBC
> diff --git a/time/tst-y2038-time64.c b/time/tst-y2038-time64.c
> new file mode 100644
> index 0000000000..d6482b0be9
> --- /dev/null
> +++ b/time/tst-y2038-time64.c
> @@ -0,0 +1 @@
> +#include "tst-y2038.c"
> diff --git a/time/tst-y2038.c b/time/tst-y2038.c
> new file mode 100644
> index 0000000000..659716d0fa
> --- /dev/null
> +++ b/time/tst-y2038.c
> @@ -0,0 +1,52 @@
> +/* Test strftime and strptime after 2038-01-19 03:14:07 UTC (bug 30053).
> +   Copyright (C) 2023 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <time.h>
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <support/check.h>
> +
> +static int
> +do_test (void)
> +{
> +  TEST_VERIFY_EXIT (setenv ("TZ", "UTC0", 1) == 0);
> +  tzset ();
> +  if (sizeof (time_t) > 4)
> +    {
> +      time_t wrap = (time_t) 2147483648LL;
> +      char buf[80];
> +      struct tm *tm = gmtime (&wrap);
> +      TEST_VERIFY_EXIT (tm != NULL);
> +      TEST_VERIFY_EXIT (strftime (buf, sizeof buf, "%s", tm) > 0);
> +      puts (buf);
> +      TEST_VERIFY (strcmp (buf, "2147483648") == 0);
> +
> +      struct tm tm2;
> +      char *p = strptime (buf, "%s", &tm2);
> +      TEST_VERIFY_EXIT (p != NULL && *p == '\0');
> +      time_t t = mktime (&tm2);
> +      printf ("%lld\n", (long long) t);
> +      TEST_VERIFY (t == wrap);
> +    }
> +  else
> +    FAIL_UNSUPPORTED ("32-bit time_t");
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>
  
Andreas Schwab Jan. 26, 2023, 2:05 p.m. UTC | #2
On Jan 26 2023, Adhemerval Zanella Netto wrote:

> I think it should be renamed to tst-strftime4-time64 to show it is testing
> strftime.

It also tests strptime.
  
Adhemerval Zanella Jan. 26, 2023, 2:25 p.m. UTC | #3
On 26/01/23 11:05, Andreas Schwab wrote:
> On Jan 26 2023, Adhemerval Zanella Netto wrote:
> 
>> I think it should be renamed to tst-strftime4-time64 to show it is testing
>> strftime.
> 
> It also tests strptime.
> 

Right, I see time/tst-y2039.c does something similar.  Maybe time/tst-y2039-2.c?
I still think time/tst-y2038.c is somewhat confusing.
  
Andreas Schwab Jan. 26, 2023, 2:36 p.m. UTC | #4
On Jan 26 2023, Adhemerval Zanella Netto wrote:

> I still think time/tst-y2038.c is somewhat confusing.

It tests the Y2038 problem.
  
Adhemerval Zanella Jan. 26, 2023, 3:26 p.m. UTC | #5
On 26/01/23 11:36, Andreas Schwab wrote:
> On Jan 26 2023, Adhemerval Zanella Netto wrote:
> 
>> I still think time/tst-y2038.c is somewhat confusing.
> 
> It tests the Y2038 problem.
> 

Yes, as every *-time64 test (and time/tst-y2039 is also confusing).
  
Fangrui Song Feb. 6, 2023, 8:33 p.m. UTC | #6
On 2023-01-26, Adhemerval Zanella Netto via Libc-alpha wrote:
>
>
>On 26/01/23 11:36, Andreas Schwab wrote:
>> On Jan 26 2023, Adhemerval Zanella Netto wrote:
>>
>>> I still think time/tst-y2038.c is somewhat confusing.
>>
>> It tests the Y2038 problem.
>>
>
>Yes, as every *-time64 test (and time/tst-y2039 is also confusing).

I feel similarly.  A name like tst-strftime*-time64.c may be more
readable than tst-y2039.c :)
  

Patch

diff --git a/time/Makefile b/time/Makefile
index d86f2105c5..8b2e1d2398 100644
--- a/time/Makefile
+++ b/time/Makefile
@@ -50,7 +50,7 @@  tests	:= test_time clocktest tst-posixtz tst-strptime tst_wcsftime \
 	   tst-clock tst-clock2 tst-clock_nanosleep tst-cpuclock1 \
 	   tst-adjtime tst-ctime tst-difftime tst-mktime4 tst-clock_settime \
 	   tst-settimeofday tst-itimer tst-gmtime tst-timegm \
-	   tst-timespec_get tst-timespec_getres
+	   tst-timespec_get tst-timespec_getres tst-y2038
 
 tests-time64 := \
   tst-adjtime-time64 \
@@ -68,6 +68,7 @@  tests-time64 := \
   tst-timegm-time64 \
   tst-timespec_get-time64 \
   tst-timespec_getres-time64 \
+  tst-y2038-time64 \
   tst-y2039-time64
 
 include ../Rules
diff --git a/time/strftime_l.c b/time/strftime_l.c
index e09561c39c..402c6c4111 100644
--- a/time/strftime_l.c
+++ b/time/strftime_l.c
@@ -159,6 +159,10 @@  extern char *tzname[];
 #ifdef _LIBC
 # define tzname __tzname
 # define tzset __tzset
+
+# define time_t __time64_t
+# define __gmtime_r(t, tp) __gmtime64_r (t, tp)
+# define mktime(tp) __mktime64 (tp)
 #endif
 
 #if !HAVE_TM_GMTOFF
diff --git a/time/strptime_l.c b/time/strptime_l.c
index 80fd705b8d..85c3249fcc 100644
--- a/time/strptime_l.c
+++ b/time/strptime_l.c
@@ -30,8 +30,10 @@ 
 #ifdef _LIBC
 # define HAVE_LOCALTIME_R 0
 # include "../locale/localeinfo.h"
-#endif
 
+# define time_t __time64_t
+# define __localtime_r(t, tp) __localtime64_r (t, tp)
+#endif
 
 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
 # ifdef _LIBC
diff --git a/time/tst-y2038-time64.c b/time/tst-y2038-time64.c
new file mode 100644
index 0000000000..d6482b0be9
--- /dev/null
+++ b/time/tst-y2038-time64.c
@@ -0,0 +1 @@ 
+#include "tst-y2038.c"
diff --git a/time/tst-y2038.c b/time/tst-y2038.c
new file mode 100644
index 0000000000..659716d0fa
--- /dev/null
+++ b/time/tst-y2038.c
@@ -0,0 +1,52 @@ 
+/* Test strftime and strptime after 2038-01-19 03:14:07 UTC (bug 30053).
+   Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <time.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+  TEST_VERIFY_EXIT (setenv ("TZ", "UTC0", 1) == 0);
+  tzset ();
+  if (sizeof (time_t) > 4)
+    {
+      time_t wrap = (time_t) 2147483648LL;
+      char buf[80];
+      struct tm *tm = gmtime (&wrap);
+      TEST_VERIFY_EXIT (tm != NULL);
+      TEST_VERIFY_EXIT (strftime (buf, sizeof buf, "%s", tm) > 0);
+      puts (buf);
+      TEST_VERIFY (strcmp (buf, "2147483648") == 0);
+
+      struct tm tm2;
+      char *p = strptime (buf, "%s", &tm2);
+      TEST_VERIFY_EXIT (p != NULL && *p == '\0');
+      time_t t = mktime (&tm2);
+      printf ("%lld\n", (long long) t);
+      TEST_VERIFY (t == wrap);
+    }
+  else
+    FAIL_UNSUPPORTED ("32-bit time_t");
+  return 0;
+}
+
+#include <support/test-driver.c>