bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mktime fix for glibc bug 2821


From: Paul Eggert
Subject: mktime fix for glibc bug 2821
Date: Fri, 08 Sep 2006 15:50:29 -0700
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

I installed this into gnulib; when the patch gets back to
glibc we can undo the change to srclist.txt.

2006-09-08  Paul Eggert  <address@hidden>

        * config/srclist.txt: Temporarily comment out mktime.c until glibc bug
        2821 is fixed.

        * lib/mktime.c (guess_time_tm): Fix bug where mktime
        returned the maximum time_t value rather than (time_t) -1.
        Problem originally reported by William Bardwell
        <http://sourceware.org/bugzilla/show_bug.cgi?id=2821>.

        * m4/mktime.m4 (AC_FUNC_MKTIME): Sync from Autoconf.
        Check for unistd.h too, since Autoconf doesn't assume POSIX.
        Also:

        2006-09-08  Paul Eggert  <address@hidden>
        Add year_2050_test to catch glibc bug 2821
        <http://sourceware.org/bugzilla/show_bug.cgi?id=2821>.

        2006-08-15  Paul Eggert  <address@hidden>
        Prefer #ifdef to #if.

        2006-04-02  Paul Eggert  <address@hidden>
        Return from 'main' instead of calling 'exit'.

--- config/srclist.txt  4 Sep 2006 12:45:56 -0000       1.137
+++ config/srclist.txt  8 Sep 2006 22:47:18 -0000
@@ -195,7 +195,8 @@ $LIBCSRC/stdlib/strtoll.c           lib gpl
 $LIBCSRC/stdlib/strtoul.c              lib gpl
 $LIBCSRC/string/strtok_r.c             lib gpl
 $LIBCSRC/string/memmem.c               lib gpl
-$LIBCSRC/time/mktime.c                 lib gpl
+# http://sourceware.org/bugzilla/show_bug.cgi?id=2821
+#$LIBCSRC/time/mktime.c                        lib gpl
 
 #
 # http://sourceware.org/bugzilla/show_bug.cgi?id=1439
--- lib/mktime.c        18 Sep 2005 12:30:04 -0000      1.53
+++ lib/mktime.c        8 Sep 2006 22:47:18 -0000
@@ -1,5 +1,5 @@
 /* Convert a `struct tm' to a time_t value.
-   Copyright (C) 1993-1999, 2002-2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 1993-1999, 2002-2004, 2005, 2006 Free Software Foundation, 
Inc.
    This file is part of the GNU C Library.
    Contributed by Paul Eggert (address@hidden).
 
@@ -215,10 +215,11 @@ guess_time_tm (long int year, long int y
   /* Overflow occurred one way or another.  Return the nearest result
      that is actually in range, except don't report a zero difference
      if the actual difference is nonzero, as that would cause a false
-     match.  */
+     match; and don't oscillate between two values, as that would
+     confuse the spring-forward gap detector.  */
   return (*t < TIME_T_MIDPOINT
-         ? TIME_T_MIN + (*t == TIME_T_MIN)
-         : TIME_T_MAX - (*t == TIME_T_MAX));
+         ? (*t <= TIME_T_MIN + 1 ? *t + 1 : TIME_T_MIN)
+         : (TIME_T_MAX - 1 <= *t ? *t - 1 : TIME_T_MAX));
 }
 
 /* Use CONVERT to convert *T to a broken down time in *TP.
--- m4/mktime.m4        23 Sep 2005 04:15:13 -0000      1.24
+++ m4/mktime.m4        8 Sep 2006 22:47:19 -0000
@@ -1,29 +1,29 @@
-#serial 7
-dnl Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
+#serial 8
+dnl Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
 dnl From Jim Meyering.
 
-# Redefine AC_FUNC_MKTIME, to fix a bug in Autoconf 2.57 and earlier.
-# This redefinition can be removed once a new version of Autoconf comes out.
+# Redefine AC_FUNC_MKTIME, to fix a bug in Autoconf 2.60a and earlier.
+# This redefinition can be removed once a new version of Autoconf is assumed.
 # The redefinition is taken from
-# 
<http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/autoconf/autoconf/lib/autoconf/functions.m4?rev=1.78>.
+# 
<http://cvs.sv.gnu.org/viewcvs/*checkout*/autoconf/lib/autoconf/functions.m4?rev=1.108&root=autoconf>.
 # AC_FUNC_MKTIME
 # --------------
 AC_DEFUN([AC_FUNC_MKTIME],
 [AC_REQUIRE([AC_HEADER_TIME])dnl
-AC_CHECK_HEADERS_ONCE(sys/time.h)
+AC_CHECK_HEADERS_ONCE(sys/time.h unistd.h)
 AC_CHECK_FUNCS_ONCE(alarm)
 AC_CACHE_CHECK([for working mktime], ac_cv_func_working_mktime,
 [AC_RUN_IFELSE([AC_LANG_SOURCE(
 [[/* Test program from Paul Eggert and Tony Leneis.  */
-#if TIME_WITH_SYS_TIME
+#ifdef TIME_WITH_SYS_TIME
 # include <sys/time.h>
 # include <time.h>
 #else
-# if HAVE_SYS_TIME_H
+# ifdef HAVE_SYS_TIME_H
 #  include <sys/time.h>
 # else
 #  include <time.h>
@@ -31,9 +31,12 @@ AC_CACHE_CHECK([for working mktime], ac_
 #endif
 
 #include <stdlib.h>
-#include <unistd.h>
 
-#if !HAVE_ALARM
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifndef HAVE_ALARM
 # define alarm(X) /* empty */
 #endif
 
@@ -50,9 +53,9 @@ static char *tz_strings[] = {
 };
 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
 
-/* Fail if mktime fails to convert a date in the spring-forward gap.
+/* Return 0 if mktime fails to convert a date in the spring-forward gap.
    Based on a problem report from Andreas Jaeger.  */
-static void
+static int
 spring_forward_gap ()
 {
   /* glibc (up to about 1998-10-07) failed this test. */
@@ -71,29 +74,27 @@ spring_forward_gap ()
   tm.tm_min = 0;
   tm.tm_sec = 0;
   tm.tm_isdst = -1;
-  if (mktime (&tm) == (time_t)-1)
-    exit (1);
+  return mktime (&tm) != (time_t) -1;
 }
 
-static void
+static int
 mktime_test1 (now)
      time_t now;
 {
   struct tm *lt;
-  if ((lt = localtime (&now)) && mktime (lt) != now)
-    exit (1);
+  return ! (lt = localtime (&now)) || mktime (lt) == now;
 }
 
-static void
+static int
 mktime_test (now)
      time_t now;
 {
-  mktime_test1 (now);
-  mktime_test1 ((time_t) (time_t_max - now));
-  mktime_test1 ((time_t) (time_t_min + now));
+  return (mktime_test1 (now)
+         && mktime_test1 ((time_t) (time_t_max - now))
+         && mktime_test1 ((time_t) (time_t_min + now)));
 }
 
-static void
+static int
 irix_6_4_bug ()
 {
   /* Based on code from Ariel Faigon.  */
@@ -106,11 +107,10 @@ irix_6_4_bug ()
   tm.tm_sec = 0;
   tm.tm_isdst = -1;
   mktime (&tm);
-  if (tm.tm_mon != 2 || tm.tm_mday != 31)
-    exit (1);
+  return tm.tm_mon == 2 && tm.tm_mday == 31;
 }
 
-static void
+static int
 bigtime_test (j)
      int j;
 {
@@ -132,8 +132,39 @@ bigtime_test (j)
             && lt->tm_wday == tm.tm_wday
             && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
                  == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
-       exit (1);
+       return 0;
     }
+  return 1;
+}
+
+static int
+year_2050_test ()
+{
+  /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
+     ignoring leap seconds.  */
+  unsigned long int answer = 2527315200UL;
+
+  struct tm tm;
+  time_t t;
+  tm.tm_year = 2050 - 1900;
+  tm.tm_mon = 2 - 1;
+  tm.tm_mday = 1;
+  tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+  tm.tm_isdst = -1;
+
+  /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
+     instead of "TZ=America/Vancouver" in order to detect the bug even
+     on systems that don't support the Olson extension, or don't have the
+     full zoneinfo tables installed.  */
+  putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
+
+  t = mktime (&tm);
+
+  /* Check that the result is either a failure, or close enough
+     to the correct answer that we can assume the discrepancy is
+     due to leap seconds.  */
+  return (t == (time_t) -1
+         || (0 < t && answer - 120 <= t && t <= answer + 120));
 }
 
 int
@@ -160,18 +191,20 @@ main ()
        putenv (tz_strings[i]);
 
       for (t = 0; t <= time_t_max - delta; t += delta)
-       mktime_test (t);
-      mktime_test ((time_t) 1);
-      mktime_test ((time_t) (60 * 60));
-      mktime_test ((time_t) (60 * 60 * 24));
+       if (! mktime_test (t))
+         return 1;
+      if (! (mktime_test ((time_t) 1)
+            && mktime_test ((time_t) (60 * 60))
+            && mktime_test ((time_t) (60 * 60 * 24))))
+       return 1;
 
       for (j = 1; 0 < j; j *= 2)
-       bigtime_test (j);
-      bigtime_test (j - 1);
+       if (! bigtime_test (j))
+         return 1;
+      if (! bigtime_test (j - 1))
+       return 1;
     }
-  irix_6_4_bug ();
-  spring_forward_gap ();
-  exit (0);
+  return ! (irix_6_4_bug () && spring_forward_gap () && year_2050_test ());
 }]])],
               [ac_cv_func_working_mktime=yes],
               [ac_cv_func_working_mktime=no],




reply via email to

[Prev in Thread] Current Thread [Next in Thread]