]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Ensure mktime sets errno on error [BZ #23789] aaribaud/bugzilla/23789/v4
authorAlbert ARIBAUD (3ADEV) <albert.aribaud@3adev.fr>
Wed, 24 Oct 2018 12:43:06 +0000 (14:43 +0200)
committerAlbert ARIBAUD (3ADEV) <albert.aribaud@3adev.fr>
Fri, 2 Nov 2018 06:03:52 +0000 (07:03 +0100)
Posix mandates that mktime set errno to EOVERFLOW
on error, but the glibc mktime wasn't doing it so
far.

Fix this and add a test to prevent regressions.
The fix also fixes the same issue in timegm.

Tested with 'make check' on x86-linux-gnu and
i686-linux-gnu.

        * time/Makefile: Add bug-mktime4.
* time/bug-mktime4.c: New file.
* time/mktime.c
(__mktime_internal): Set errno to EOVERFLOW on error.
(mktime): Move call to __tzset inside conditional.

ChangeLog
time/Makefile
time/bug-mktime4.c [new file with mode: 0644]
time/mktime.c

index af7569507bd40bc54a245e217b56cc80495637ae..20d9f737a43fa352622800264b99fdcaa5968e6a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2018-11-02  Albert ARIBAUD  <albert.aribaud@3adev.fr>
+
+       * time/Makefile: Add bug-mktime4.
+       * time/bug-mktime4.c: New file.
+       * time/mktime.c
+       (__mktime_internal): Set errno to EOVERFLOW on error.
+       (mktime): Move call to __tzset inside conditional.
+
 2018-11-01  Zong Li  <zong@andestech.com>
 
        * soft-fp/op-8.h (_FP_FRAC_SET_8, _FP_FRAC_ADD_8, _FP_FRAC_SUB_8)
index ec3e39dcea75ee51288314640660a3a36c7be706..743bd99f182e62cd611b5a0f1b40a87e1cabf0ce 100644 (file)
@@ -43,7 +43,7 @@ tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \
           tst-getdate tst-mktime tst-mktime2 tst-ftime_l tst-strftime \
           tst-mktime3 tst-strptime2 bug-asctime bug-asctime_r bug-mktime1 \
           tst-strptime3 bug-getdate1 tst-strptime-whitespace tst-ftime \
-          tst-tzname tst-y2039
+          tst-tzname tst-y2039 bug-mktime4
 
 include ../Rules
 
diff --git a/time/bug-mktime4.c b/time/bug-mktime4.c
new file mode 100644 (file)
index 0000000..14d04c6
--- /dev/null
@@ -0,0 +1,27 @@
+#include <time.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+
+static int
+do_test (void)
+{
+  struct tm tm = { .tm_year = INT_MIN, .tm_mon = INT_MIN, .tm_mday = INT_MIN,
+                   .tm_hour = INT_MIN, .tm_min = INT_MIN, .tm_sec = INT_MIN };
+  errno = 0;
+  time_t tt = mktime (&tm);
+  if (tt != -1)
+    {
+      printf ("mktime() should have returned -1, returned %ld\n", (long int) tt);
+      return 1;
+    }
+  if (errno != EOVERFLOW)
+    {
+      printf ("mktime() returned -1, errno should be %d (EOVERFLOW) but is %d (%s)\n", EOVERFLOW, errno, strerror(errno));
+      return 1;
+    }
+  return 0;
+}
+
+#include "support/test-driver.c"
index 00f0dec6b405aca9f847d3fa138ab0735c20854a..2e0c467147dabebd28146b61ece2665b1426b14a 100644 (file)
@@ -49,6 +49,7 @@
 # define LEAP_SECONDS_POSSIBLE 1
 #endif
 
+#include <errno.h>
 #include <time.h>
 
 #include <limits.h>
@@ -435,7 +436,10 @@ __mktime_internal (struct tm *tp,
         useful than returning -1.  */
       goto offset_found;
     else if (--remaining_probes == 0)
-      return -1;
+      {
+       __set_errno (EOVERFLOW);
+       return -1;
+      }
 
   /* We have a match.  Check whether tm.tm_isdst has the requested
      value, if any.  */
@@ -507,7 +511,10 @@ __mktime_internal (struct tm *tp,
       if (INT_ADD_WRAPV (t, sec_adjustment, &t)
          || ! (mktime_min <= t && t <= mktime_max)
          || ! convert_time (convert, t, &tm))
-       return -1;
+       {
+         __set_errno (EOVERFLOW);
+         return -1;
+       }
     }
 
   *tp = tm;
@@ -522,13 +529,12 @@ __mktime_internal (struct tm *tp,
 time_t
 mktime (struct tm *tp)
 {
+# if defined _LIBC || NEED_MKTIME_WORKING
+  static mktime_offset_t localtime_offset;
   /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
      time zone names contained in the external variable 'tzname' shall
      be set as if the tzset() function had been called.  */
   __tzset ();
-
-# if defined _LIBC || NEED_MKTIME_WORKING
-  static mktime_offset_t localtime_offset;
   return __mktime_internal (tp, __localtime_r, &localtime_offset);
 # else
 #  undef mktime