From patchwork Wed Oct 24 19:32:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Albert ARIBAUD X-Patchwork-Id: 29876 Received: (qmail 113162 invoked by alias); 24 Oct 2018 19:32:16 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 113150 invoked by uid 89); 24 Oct 2018 19:32:16 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 spammy= X-HELO: smtp3-g21.free.fr From: "Albert ARIBAUD (3ADEV)" To: libc-alpha@sourceware.org Cc: "Albert ARIBAUD (3ADEV)" Subject: [PATCH] Ensure mktime sets errno on error (bug 23789) Date: Wed, 24 Oct 2018 21:32:06 +0200 Message-Id: <20181024193206.22790-1-albert.aribaud@3adev.fr> Posix mandates that mktime set errno to EOVERFLOW on error, that it, upon retuning -1, but the glibc mktime does not so far on 32-bit architectures. Fix this and add a test to prevent regressions. The test was run through 'make check' on i686-linux-gnu, then the fix was added and 'make check' run again. * time/mktime.c (mktime): Set errno to EOVERFLOW on error. * time/bug-mktime4.c: New file. * time/Makefile: Add bug-mktime4. --- time/Makefile | 2 +- time/bug-mktime4.c | 28 ++++++++++++++++++++++++++++ time/mktime.c | 11 +++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 time/bug-mktime4.c diff --git a/time/Makefile b/time/Makefile index ec3e39dcea..743bd99f18 100644 --- a/time/Makefile +++ b/time/Makefile @@ -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 index 0000000000..dd1e0c76bf --- /dev/null +++ b/time/bug-mktime4.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +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; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/time/mktime.c b/time/mktime.c index 00f0dec6b4..db775bdc3a 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -49,6 +49,7 @@ # define LEAP_SECONDS_POSSIBLE 1 #endif +#include #include #include @@ -522,6 +523,7 @@ __mktime_internal (struct tm *tp, time_t mktime (struct tm *tp) { + time_t result; /* 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. */ @@ -529,11 +531,16 @@ mktime (struct tm *tp) # if defined _LIBC || NEED_MKTIME_WORKING static mktime_offset_t localtime_offset; - return __mktime_internal (tp, __localtime_r, &localtime_offset); + result = __mktime_internal (tp, __localtime_r, &localtime_offset); # else # undef mktime - return mktime (tp); + result = mktime (tp); # endif + if (result == -1) + { + __set_errno(EOVERFLOW); + } + return result; } #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */