From patchwork Thu Dec 20 20:40:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Albert ARIBAUD (3ADEV)" X-Patchwork-Id: 1017040 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-98670-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=3adev.fr Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="i60SsZpE"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43LNv86JJwz9s8J for ; Fri, 21 Dec 2018 07:40:52 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=LTmOWCJ7Mhkw9tvwpjdozifaqdhHoqxg6jkxquySwgwdOCQqyuz/7 0DpCD0BJoEolECoMZZcE4ATF5E2jX2/F3yEUVavTYYL0vk7BzJdAnybaygJTIvEN b4ndaJE3RZguLtP/Ctoa0tRJSmb3vD5Nt90oShffbdVqc7ZK/I41V4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=tzHe6fWdMoTfbZxuQQZXB0S1KtI=; b=i60SsZpEZFaG8Vr4V6+ys7McnM5H vaSUVinFJjMGWnqT9c3CI1Ce2rWCjzsOrBfjWdyuPn6yTNal4Foi6MSC1YTlngUt Ixd3KSUYFFBWtzX5XWMqqS8dfe20m/b71ftURVCif48ttujtvruz0DbgjPc6YCNY ybXkp/lUuuWVrN0= Received: (qmail 63161 invoked by alias); 20 Dec 2018 20:40:47 -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 63150 invoked by uid 89); 20 Dec 2018 20:40:46 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.6 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_LOW autolearn=ham version=3.3.2 spammy= X-HELO: smtp1-g21.free.fr From: "Albert ARIBAUD (3ADEV)" To: libc-alpha@sourceware.org Subject: [PATCH v5] Y2038: make __difftime compatible with 64-bit time Date: Thu, 20 Dec 2018 21:40:41 +0100 Message-Id: <20181220204041.9377-1-albert.aribaud@3adev.fr> 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. --- v5: fixed erroneously duplicated strong_alias v4: added period and double space where missing added libc_hidden_proto and libc_hidden_def v3: specified which tests were done v2: add more explanations and partial changelog include/time.h | 7 +++++++ time/difftime.c | 34 +++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/include/time.h b/include/time.h index a10a59a628..f935e9dd3e 100644 --- a/include/time.h +++ b/include/time.h @@ -141,6 +141,13 @@ 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); +libc_hidden_proto (__difftime64) +#endif + extern double __difftime (time_t time1, time_t time0); diff --git a/time/difftime.c b/time/difftime.c index 7c5dd9898b..5243cbd9cb 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,20 @@ __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 + +libc_hidden_def (__difftime64) + +double +__difftime (time_t time1, time_t time0) +{ + return __difftime64 (time1, time0); +} + +#endif + strong_alias (__difftime, difftime) +