From patchwork Sun Jan 5 05:56:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104038 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82E99385841E for ; Sun, 5 Jan 2025 05:59:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82E99385841E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=Qoi7EaB7 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 6FB713858D35 for ; Sun, 5 Jan 2025 05:58:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6FB713858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6FB713858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056692; cv=none; b=WaSqUU/rXvJTz7KJCgYBcMvd9adltsOJPrcE7QZaHjr3FzQIAqDdUAlup4tyYyRezQt/Qc3BkSATJsWTJK9XdI7Neen1gYhbU+dB3CmoovHxlssf2VFufr5oJdjqMP5tOQnNfh+Y6dqiKZnU+M02FaPm+FYTf5FfYwSuZaRPGB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056692; c=relaxed/simple; bh=tZDgCzZSBeaXnyV9B0PBBR+XtPJ86Y7/8MhGKXJ/wwA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=v15XeHF5rO6agdONyrcOZeOCivl9R/CKKJUXR4ByEjjRD3BDe0w7JkLmAtCwyIcPzQMWw2+D88ekLn7aOmaij3+Umzo+/eDnMwrHYleKoWH0NvpEnGF/G8VCjAs9Rjad3feXvr/sUeAPLRauqUJGq8VlJjxYj1jUZbO69rMhkeA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6FB713858D35 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id DBA703C123844 for ; Sat, 4 Jan 2025 21:58:11 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id t4Id0Rba1kQX; Sat, 4 Jan 2025 21:58:10 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 088563C123843; Sat, 4 Jan 2025 21:58:10 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 088563C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056690; bh=yH/v7T2kw6ZC/wIL5zgY8QhiD0dr61pqemGjuTuoX/s=; h=From:To:Date:Message-ID:MIME-Version; b=Qoi7EaB7z+KVaxW7a81gCpkLElMWpTSOnIHHW3wyoqArf5qnMzdyoDl9d1Qo7z6KD WPSvXehUf1DanjXPFnl5GCplj6U3Yg1y8Gq/2MENLFuAhtcx8gtgm8gSDJEscxQ0+W JmveFmeDoxf0znmKJAlwCkHAfr6bK7RJuzgVLFb+CdTX2ue5Sr3yCaXIX8zvEDC0bK 1cPzku3fFva/kdqEQLVBpF5eUHXDGJS51gosiqwRgAzA0Gk8neiMaGJ99oZB4A6TQK onNcnj6u4Zl/f8+LJL8t7X1rbeEGWkev+2f9DH54mxAzThEx/IL21xooKrz7duWh0j oHCVMtONq5k7A== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id KaVhTuHt6uRp; Sat, 4 Jan 2025 21:58:09 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id DF4FB3C082EB9; Sat, 4 Jan 2025 21:58:09 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 01/59] Split intprops.h into two Date: Sat, 4 Jan 2025 21:56:36 -0800 Message-ID: <20250105055750.1668721-2-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This syncs from Gnulib, and prepares for stdckdint.h support. * include/intprops-internal.h: New file, copied from Gnulib. * include/intprops.h: Sync from Gnulib. --- CONTRIBUTED-BY | 3 + SHARED-FILES | 4 +- include/intprops-internal.h | 400 ++++++++++++++++++++++++++++++++++++ include/intprops.h | 356 +++----------------------------- 4 files changed, 432 insertions(+), 331 deletions(-) create mode 100644 include/intprops-internal.h diff --git a/CONTRIBUTED-BY b/CONTRIBUTED-BY index 0cb379cc9d..652642fe86 100644 --- a/CONTRIBUTED-BY +++ b/CONTRIBUTED-BY @@ -1406,6 +1406,9 @@ include/atomic.h: include/inline-hashtab.h: Contributed by Alexandre Oliva +include/intprops-internal.h: + Written by Paul Eggert. + include/intprops.h: Written by Paul Eggert. diff --git a/SHARED-FILES b/SHARED-FILES index 032c407881..444eff5141 100644 --- a/SHARED-FILES +++ b/SHARED-FILES @@ -29,7 +29,9 @@ gnulib: argp/argp.h dirent/alphasort.c dirent/scandir.c - # Merged from gnulib 2021-09-21 + # Merged from gnulib 2024-10-14 + include/intprops-internal.h + # Merged from gnulib 2024-10-14 include/intprops.h # Merged from gnulib 2021-09-21 include/regex.h diff --git a/include/intprops-internal.h b/include/intprops-internal.h new file mode 100644 index 0000000000..62de3c889e --- /dev/null +++ b/include/intprops-internal.h @@ -0,0 +1,400 @@ +/* intprops-internal.h -- properties of integer types not visible to users + + Copyright (C) 2001-2025 Free Software Foundation, Inc. + + This program 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. + + This program 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 this program. If not, see . */ + +#ifndef _GL_INTPROPS_INTERNAL_H +#define _GL_INTPROPS_INTERNAL_H + +#include + +/* Pacify GCC 13.2 in some calls to _GL_EXPR_SIGNED. */ +#if 4 < __GNUC__ + (3 <= __GNUC_MINOR__) && !defined __clang__ +# pragma GCC diagnostic ignored "-Wtype-limits" +#endif + +/* Return a value with the common real type of E and V and the value of V. + Do not evaluate E. */ +#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) + +/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see + . */ +#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) + +/* The extra casts in the following macros work around compiler bugs, + e.g., in Cray C 5.0.3.0. */ + +/* True if the real type T is signed. */ +#define _GL_TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) + +/* Return 1 if the real expression E, after promotion, has a + signed or floating type. Do not evaluate E. */ +#define _GL_EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0) + + +/* Minimum and maximum values for integer types and expressions. */ + +/* The width in bits of the integer type or expression T. + Do not evaluate T. T must not be a bit-field expression. + Padding bits are not supported; this is checked at compile-time below. */ +#define _GL_TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT) + +/* The maximum and minimum values for the type of the expression E, + after integer promotion. E is not evaluated. */ +#define _GL_INT_MINIMUM(e) \ + (_GL_EXPR_SIGNED (e) \ + ? ~ _GL_SIGNED_INT_MAXIMUM (e) \ + : _GL_INT_CONVERT (e, 0)) +#define _GL_INT_MAXIMUM(e) \ + (_GL_EXPR_SIGNED (e) \ + ? _GL_SIGNED_INT_MAXIMUM (e) \ + : _GL_INT_NEGATE_CONVERT (e, 1)) +#define _GL_SIGNED_INT_MAXIMUM(e) \ + (((_GL_INT_CONVERT (e, 1) << (_GL_TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1) + +/* Work around OpenVMS incompatibility with C99. */ +#if !defined LLONG_MAX && defined __INT64_MAX +# define LLONG_MAX __INT64_MAX +# define LLONG_MIN __INT64_MIN +#endif + +/* This include file assumes that signed types are two's complement without + padding bits; the above macros have undefined behavior otherwise. + If this is a problem for you, please let us know how to fix it for your host. + This assumption is tested by the intprops-tests module. */ + +/* Does the __typeof__ keyword work? This could be done by + 'configure', but for now it's easier to do it by hand. */ +#if ((defined __GNUC__ && 2 <= __GNUC__) \ + || (defined __clang_major__ && 4 <= __clang_major__) \ + || (defined __IBMC__ && 1210 <= __IBMC__ && defined __IBM__TYPEOF__) \ + || (defined __SUNPRO_C && 0x5110 <= __SUNPRO_C && !__STDC__) \ + || (defined _MSC_VER && 1939 <= _MSC_VER)) +# define _GL_HAVE___TYPEOF__ 1 +#else +# define _GL_HAVE___TYPEOF__ 0 +#endif + +/* Return 1 if the integer type or expression T might be signed. Return 0 + if it is definitely unsigned. T must not be a bit-field expression. + This macro does not evaluate its argument, and expands to an + integer constant expression. */ +#if _GL_HAVE___TYPEOF__ +# define _GL_SIGNED_TYPE_OR_EXPR(t) _GL_TYPE_SIGNED (__typeof__ (t)) +#else +# define _GL_SIGNED_TYPE_OR_EXPR(t) 1 +#endif + +/* Return 1 if - A would overflow in [MIN,MAX] arithmetic. + A should not have side effects, and A's type should be an + integer with minimum value MIN and maximum MAX. */ +#define _GL_INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ + ((min) < 0 ? (a) < - (max) : 0 < (a)) + +/* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow + (A, B, P) work when P is non-null. */ +#ifdef __EDG__ +/* EDG-based compilers like nvc 22.1 cannot add 64-bit signed to unsigned + . */ +# define _GL_HAS_BUILTIN_ADD_OVERFLOW 0 +#elif defined __has_builtin +# define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow) +/* __builtin_{add,sub}_overflow exists but is not reliable in GCC 5.x and 6.x, + see . */ +#elif 7 <= __GNUC__ +# define _GL_HAS_BUILTIN_ADD_OVERFLOW 1 +#else +# define _GL_HAS_BUILTIN_ADD_OVERFLOW 0 +#endif + +/* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */ +#if defined __clang_major__ && __clang_major__ < 14 +/* Work around Clang bug . */ +# define _GL_HAS_BUILTIN_MUL_OVERFLOW 0 +#else +# define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW +#endif + +/* True if __builtin_add_overflow_p (A, B, C) works, and similarly for + __builtin_sub_overflow_p and __builtin_mul_overflow_p. */ +#ifdef __EDG__ +/* In EDG-based compilers like ICC 2021.3 and earlier, + __builtin_add_overflow_p etc. are not treated as integral constant + expressions even when all arguments are. */ +# define _GL_HAS_BUILTIN_OVERFLOW_P 0 +#elif defined __has_builtin +# define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p) +#else +# define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__) +#endif + +#if (!defined _GL_STDCKDINT_H && 202311 <= __STDC_VERSION__ \ + && ! (_GL_HAS_BUILTIN_ADD_OVERFLOW && _GL_HAS_BUILTIN_MUL_OVERFLOW)) +# include +#endif + +/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. + Return 1 if the result overflows. Arguments should not have side + effects and A, B and *R can be of any integer type other than char, + bool, a bit-precise integer type, or an enumeration type. */ +#if _GL_HAS_BUILTIN_ADD_OVERFLOW +# define _GL_INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r) +# define _GL_INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r) +#elif defined ckd_add && defined ckd_sub && !defined _GL_STDCKDINT_H +# define _GL_INT_ADD_WRAPV(a, b, r) ckd_add (r, + (a), + (b)) +# define _GL_INT_SUBTRACT_WRAPV(a, b, r) ckd_sub (r, + (a), + (b)) +#else +# define _GL_INT_ADD_WRAPV(a, b, r) \ + _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW) +# define _GL_INT_SUBTRACT_WRAPV(a, b, r) \ + _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW) +#endif +#if _GL_HAS_BUILTIN_MUL_OVERFLOW +# if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \ + || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \ + && !defined __clang__ && !defined __EDG__) +# define _GL_INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r) +# else + /* Work around GCC bug 91450. */ +# define _GL_INT_MULTIPLY_WRAPV(a, b, r) \ + ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && _GL_EXPR_SIGNED (a) && _GL_EXPR_SIGNED (b) \ + && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, \ + (__typeof__ (*(r))) 0, \ + (__typeof__ (*(r))) -1)) \ + ? ((void) __builtin_mul_overflow (a, b, r), 1) \ + : __builtin_mul_overflow (a, b, r)) +# endif +#elif defined ckd_mul && !defined _GL_STDCKDINT_H +# define _GL_INT_MULTIPLY_WRAPV(a, b, r) ckd_mul (r, + (a), + (b)) +#else +# define _GL_INT_MULTIPLY_WRAPV(a, b, r) \ + _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW) +#endif + +/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See: + https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193 + https://llvm.org/bugs/show_bug.cgi?id=25390 + For now, assume GCC < 14 and all Clang versions generate bogus + warnings for _Generic. This matters only for compilers that + lack relevant builtins. */ +#if (__GNUC__ && __GNUC__ < 14) || defined __clang__ +# define _GL__GENERIC_BOGUS 1 +#else +# define _GL__GENERIC_BOGUS 0 +#endif + +/* Store the low-order bits of A B into *R, where OP specifies + the operation and OVERFLOW the overflow predicate. Return 1 if the + result overflows. Arguments should not have side effects, + and A, B and *R can be of any integer type other than char, bool, a + bit-precise integer type, or an enumeration type. */ +#if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS +# define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \ + (_Generic \ + (*(r), \ + signed char: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + signed char, SCHAR_MIN, SCHAR_MAX), \ + unsigned char: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + unsigned char, 0, UCHAR_MAX), \ + short int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + short int, SHRT_MIN, SHRT_MAX), \ + unsigned short int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + unsigned short int, 0, USHRT_MAX), \ + int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + int, INT_MIN, INT_MAX), \ + unsigned int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + unsigned int, 0, UINT_MAX), \ + long int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ + long int, LONG_MIN, LONG_MAX), \ + unsigned long int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ + unsigned long int, 0, ULONG_MAX), \ + long long int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ + long long int, LLONG_MIN, LLONG_MAX), \ + unsigned long long int: \ + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ + unsigned long long int, 0, ULLONG_MAX))) +#else +/* Store the low-order bits of A B into *R, where OP specifies + the operation and OVERFLOW the overflow predicate. If *R is + signed, its type is ST with bounds SMIN..SMAX; otherwise its type + is UT with bounds U..UMAX. ST and UT are narrower than int. + Return 1 if the result overflows. Arguments should not have side + effects, and A, B and *R can be of any integer type other than + char, bool, a bit-precise integer type, or an enumeration type. */ +# if _GL_HAVE___TYPEOF__ +# define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \ + (_GL_TYPE_SIGNED (__typeof__ (*(r))) \ + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \ + : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax)) +# else +# define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \ + (overflow (a, b, smin, smax) \ + ? (overflow (a, b, 0, umax) \ + ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \ + : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \ + : (overflow (a, b, 0, umax) \ + ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \ + : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0))) +# endif + +# define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \ + (sizeof *(r) == sizeof (signed char) \ + ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \ + signed char, SCHAR_MIN, SCHAR_MAX, \ + unsigned char, UCHAR_MAX) \ + : sizeof *(r) == sizeof (short int) \ + ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \ + short int, SHRT_MIN, SHRT_MAX, \ + unsigned short int, USHRT_MAX) \ + : sizeof *(r) == sizeof (int) \ + ? (_GL_EXPR_SIGNED (*(r)) \ + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + int, INT_MIN, INT_MAX) \ + : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ + unsigned int, 0, UINT_MAX)) \ + : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow)) +# ifdef LLONG_MAX +# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ + (sizeof *(r) == sizeof (long int) \ + ? (_GL_EXPR_SIGNED (*(r)) \ + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ + long int, LONG_MIN, LONG_MAX) \ + : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ + unsigned long int, 0, ULONG_MAX)) \ + : (_GL_EXPR_SIGNED (*(r)) \ + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ + long long int, LLONG_MIN, LLONG_MAX) \ + : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ + unsigned long long int, 0, ULLONG_MAX))) +# else +# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ + (_GL_EXPR_SIGNED (*(r)) \ + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ + long int, LONG_MIN, LONG_MAX) \ + : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ + unsigned long int, 0, ULONG_MAX)) +# endif +#endif + +/* Store the low-order bits of A B into *R, where the operation + is given by OP. Use the unsigned type UT for calculation to avoid + overflow problems. *R's type is T, with extrema TMIN and TMAX. + T can be any signed integer type other than char, bool, a + bit-precise integer type, or an enumeration type. + Return 1 if the result overflows. */ +#define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \ + (overflow (a, b, tmin, tmax) \ + ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \ + : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0)) + +/* Return 1 if the integer expressions A - B and -A would overflow, + respectively. Arguments should not have side effects, + and can be any signed integer type other than char, bool, a + bit-precise integer type, or an enumeration type. + These macros are tuned for their last input argument being a constant. */ + +#if _GL_HAS_BUILTIN_OVERFLOW_P +# define _GL_INT_NEGATE_OVERFLOW(a) \ + __builtin_sub_overflow_p (0, a, (__typeof__ (- (a))) 0) +#else +# define _GL_INT_NEGATE_OVERFLOW(a) \ + _GL_INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) +#endif + +/* Return the low-order bits of A B, where the operation is given + by OP. Use the unsigned type UT for calculation to avoid undefined + behavior on signed integer overflow, and convert the result to type T. + UT is at least as wide as T and is no narrower than unsigned int, + T is two's complement, and there is no padding or trap representations. + Assume that converting UT to T yields the low-order bits, as is + done in all known two's-complement C compilers. E.g., see: + https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html + + According to the C standard, converting UT to T yields an + implementation-defined result or signal for values outside T's + range. However, code that works around this theoretical problem + runs afoul of a compiler bug in Oracle Studio 12.3 x86. See: + https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html + As the compiler bug is real, don't try to work around the + theoretical problem. */ + +#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \ + ((t) ((ut) (a) op (ut) (b))) + +/* Return true if the numeric values A + B, A - B, A * B fall outside + the range TMIN..TMAX. Arguments should not have side effects + and can be any integer type other than char, bool, + a bit-precise integer type, or an enumeration type. + TMIN should be signed and nonpositive. + TMAX should be positive, and should be signed unless TMIN is zero. */ +#define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \ + ((b) < 0 \ + ? (((tmin) \ + ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \ + && (a) < (tmin) - (b)) \ + : (a) <= -1 - (b)) \ + || ((_GL_EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \ + : (a) < 0 \ + ? (((tmin) \ + ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \ + && (b) < (tmin) - (a)) \ + : (b) <= -1 - (a)) \ + || ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \ + && (tmax) < (a) + (b))) \ + : (tmax) < (b) || (tmax) - (b) < (a)) +#define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \ + (((a) < 0) == ((b) < 0) \ + ? ((a) < (b) \ + ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \ + : (tmax) < (a) - (b)) \ + : (a) < 0 \ + ? ((!_GL_EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \ + || (a) - (tmin) < (b)) \ + : ((! (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \ + && _GL_EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \ + && (tmax) <= -1 - (b)) \ + || (tmax) + (b) < (a))) +#define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \ + ((b) < 0 \ + ? ((a) < 0 \ + ? (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \ + ? (a) < (tmax) / (b) \ + : ((_GL_INT_NEGATE_OVERFLOW (b) \ + ? _GL_INT_CONVERT (b, tmax) >> (_GL_TYPE_WIDTH (+ (b)) - 1) \ + : (tmax) / -(b)) \ + <= -1 - (a))) \ + : _GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \ + ? (_GL_EXPR_SIGNED (a) \ + ? 0 < (a) + (tmin) \ + : 0 < (a) && -1 - (tmin) < (a) - 1) \ + : (tmin) / (b) < (a)) \ + : (b) == 0 \ + ? 0 \ + : ((a) < 0 \ + ? (_GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \ + ? (_GL_EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \ + : (tmin) / (a) < (b)) \ + : (tmax) / (b) < (a))) + +#endif /* _GL_INTPROPS_INTERNAL_H */ diff --git a/include/intprops.h b/include/intprops.h index 140e51054a..92dfef2500 100644 --- a/include/intprops.h +++ b/include/intprops.h @@ -15,19 +15,10 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ - #ifndef _GL_INTPROPS_H #define _GL_INTPROPS_H -#include - -/* Return a value with the common real type of E and V and the value of V. - Do not evaluate E. */ -#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) - -/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see - . */ -#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) +#include "intprops-internal.h" /* The extra casts in the following macros work around compiler bugs, e.g., in Cray C 5.0.3.0. */ @@ -37,11 +28,11 @@ #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) /* True if the real type T is signed. */ -#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) +#define TYPE_SIGNED(t) _GL_TYPE_SIGNED (t) /* Return 1 if the real expression E, after promotion, has a signed or floating type. Do not evaluate E. */ -#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0) +#define EXPR_SIGNED(e) _GL_EXPR_SIGNED (e) /* Minimum and maximum values for integer types and expressions. */ @@ -49,7 +40,7 @@ /* The width in bits of the integer type or expression T. Do not evaluate T. T must not be a bit-field expression. Padding bits are not supported; this is checked at compile-time below. */ -#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT) +#define TYPE_WIDTH(t) _GL_TYPE_WIDTH (t) /* The maximum and minimum values for the integer type T. */ #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t)) @@ -58,51 +49,6 @@ ? (t) -1 \ : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1))) -/* The maximum and minimum values for the type of the expression E, - after integer promotion. E is not evaluated. */ -#define _GL_INT_MINIMUM(e) \ - (EXPR_SIGNED (e) \ - ? ~ _GL_SIGNED_INT_MAXIMUM (e) \ - : _GL_INT_CONVERT (e, 0)) -#define _GL_INT_MAXIMUM(e) \ - (EXPR_SIGNED (e) \ - ? _GL_SIGNED_INT_MAXIMUM (e) \ - : _GL_INT_NEGATE_CONVERT (e, 1)) -#define _GL_SIGNED_INT_MAXIMUM(e) \ - (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1) - -/* Work around OpenVMS incompatibility with C99. */ -#if !defined LLONG_MAX && defined __INT64_MAX -# define LLONG_MAX __INT64_MAX -# define LLONG_MIN __INT64_MIN -#endif - -/* This include file assumes that signed types are two's complement without - padding bits; the above macros have undefined behavior otherwise. - If this is a problem for you, please let us know how to fix it for your host. - This assumption is tested by the intprops-tests module. */ - -/* Does the __typeof__ keyword work? This could be done by - 'configure', but for now it's easier to do it by hand. */ -#if (2 <= __GNUC__ \ - || (4 <= __clang_major__) \ - || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \ - || (0x5110 <= __SUNPRO_C && !__STDC__)) -# define _GL_HAVE___TYPEOF__ 1 -#else -# define _GL_HAVE___TYPEOF__ 0 -#endif - -/* Return 1 if the integer type or expression T might be signed. Return 0 - if it is definitely unsigned. T must not be a bit-field expression. - This macro does not evaluate its argument, and expands to an - integer constant expression. */ -#if _GL_HAVE___TYPEOF__ -# define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t)) -#else -# define _GL_SIGNED_TYPE_OR_EXPR(t) 1 -#endif - /* Bound on length of the string representing an unsigned integer value representable in B bits. log10 (2.0) < 146/485. The smallest value of B where this bound is not tight is 2621. */ @@ -129,12 +75,11 @@ /* Range overflow checks. The INT__RANGE_OVERFLOW macros return 1 if the corresponding C - operators might not yield numerically correct answers due to - arithmetic overflow. They do not rely on undefined or - implementation-defined behavior. Their implementations are simple - and straightforward, but they are harder to use and may be less - efficient than the INT__WRAPV, INT__OK, and - INT__OVERFLOW macros described below. + operators overflow arithmetically when given the same arguments. + These macros do not rely on undefined or implementation-defined behavior. + Although their implementations are simple and straightforward, + they are harder to use and may be less efficient than the + INT__WRAPV, INT__OK, and INT__OVERFLOW macros described below. Example usage: @@ -181,9 +126,7 @@ /* Return 1 if - A would overflow in [MIN,MAX] arithmetic. See above for restrictions. */ #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ - ((min) < 0 \ - ? (a) < - (max) \ - : 0 < (a)) + _GL_INT_NEGATE_RANGE_OVERFLOW (a, min, max) /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. See above for restrictions. Avoid && and || as they tickle @@ -227,40 +170,6 @@ ? (a) < (min) >> (b) \ : (max) >> (b) < (a)) -/* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow - (A, B, P) work when P is non-null. */ -/* __builtin_{add,sub}_overflow exists but is not reliable in GCC 5.x and 6.x, - see . */ -#if 7 <= __GNUC__ && !defined __ICC -# define _GL_HAS_BUILTIN_ADD_OVERFLOW 1 -#elif defined __has_builtin -# define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow) -#else -# define _GL_HAS_BUILTIN_ADD_OVERFLOW 0 -#endif - -/* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */ -#ifdef __clang__ -/* Work around Clang bug . */ -# define _GL_HAS_BUILTIN_MUL_OVERFLOW 0 -#else -# define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW -#endif - -/* True if __builtin_add_overflow_p (A, B, C) works, and similarly for - __builtin_sub_overflow_p and __builtin_mul_overflow_p. */ -#if defined __clang__ || defined __ICC -/* Clang 11 lacks __builtin_mul_overflow_p, and even if it did it - would presumably run afoul of Clang bug 16404. ICC 2021.1's - __builtin_add_overflow_p etc. are not treated as integral constant - expressions even when all arguments are. */ -# define _GL_HAS_BUILTIN_OVERFLOW_P 0 -#elif defined __has_builtin -# define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p) -#else -# define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__) -#endif - /* The _GL*_OVERFLOW macros have the same restrictions as the *_RANGE_OVERFLOW macros, except that they do not assume that operands (e.g., A and B) have the same type as MIN and MAX. Instead, they assume @@ -347,13 +256,18 @@ Because the WRAPV macros convert the result, they report overflow in different circumstances than the OVERFLOW macros do. For example, in the typical case with 16-bit 'short' and 32-bit 'int', - if A, B and R are all of type 'short' then INT_ADD_OVERFLOW (A, B) + if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B) returns false because the addition cannot overflow after A and B - are converted to 'int', whereas INT_ADD_WRAPV (A, B, &R) returns + are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns true or false depending on whether the sum fits into 'short'. These macros are tuned for their last input argument being a constant. + A, B, and *R should be integers; they need not be the same type, + and they need not be all signed or all unsigned. + However, none of the integer types should be bit-precise, + and *R's type should not be char, bool, or an enumeration type. + Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, A % B, and A << B would overflow, respectively. */ @@ -361,12 +275,7 @@ _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) #define INT_SUBTRACT_OVERFLOW(a, b) \ _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) -#if _GL_HAS_BUILTIN_OVERFLOW_P -# define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a) -#else -# define INT_NEGATE_OVERFLOW(a) \ - INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) -#endif +#define INT_NEGATE_OVERFLOW(a) _GL_INT_NEGATE_OVERFLOW (a) #define INT_MULTIPLY_OVERFLOW(a, b) \ _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) #define INT_DIVIDE_OVERFLOW(a, b) \ @@ -388,224 +297,9 @@ /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. Return 1 if the result overflows. See above for restrictions. */ -#if _GL_HAS_BUILTIN_ADD_OVERFLOW -# define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r) -# define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r) -#else -# define INT_ADD_WRAPV(a, b, r) \ - _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW) -# define INT_SUBTRACT_WRAPV(a, b, r) \ - _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW) -#endif -#if _GL_HAS_BUILTIN_MUL_OVERFLOW -# if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \ - || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \ - && !defined __ICC) -# define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r) -# else - /* Work around GCC bug 91450. */ -# define INT_MULTIPLY_WRAPV(a, b, r) \ - ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \ - && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \ - ? ((void) __builtin_mul_overflow (a, b, r), 1) \ - : __builtin_mul_overflow (a, b, r)) -# endif -#else -# define INT_MULTIPLY_WRAPV(a, b, r) \ - _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW) -#endif - -/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See: - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193 - https://llvm.org/bugs/show_bug.cgi?id=25390 - For now, assume all versions of GCC-like compilers generate bogus - warnings for _Generic. This matters only for compilers that - lack relevant builtins. */ -#if __GNUC__ || defined __clang__ -# define _GL__GENERIC_BOGUS 1 -#else -# define _GL__GENERIC_BOGUS 0 -#endif - -/* Store the low-order bits of A B into *R, where OP specifies - the operation and OVERFLOW the overflow predicate. Return 1 if the - result overflows. See above for restrictions. */ -#if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS -# define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \ - (_Generic \ - (*(r), \ - signed char: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - signed char, SCHAR_MIN, SCHAR_MAX), \ - unsigned char: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - unsigned char, 0, UCHAR_MAX), \ - short int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - short int, SHRT_MIN, SHRT_MAX), \ - unsigned short int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - unsigned short int, 0, USHRT_MAX), \ - int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - int, INT_MIN, INT_MAX), \ - unsigned int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - unsigned int, 0, UINT_MAX), \ - long int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ - long int, LONG_MIN, LONG_MAX), \ - unsigned long int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ - unsigned long int, 0, ULONG_MAX), \ - long long int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ - long long int, LLONG_MIN, LLONG_MAX), \ - unsigned long long int: \ - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ - unsigned long long int, 0, ULLONG_MAX))) -#else -/* Store the low-order bits of A B into *R, where OP specifies - the operation and OVERFLOW the overflow predicate. If *R is - signed, its type is ST with bounds SMIN..SMAX; otherwise its type - is UT with bounds U..UMAX. ST and UT are narrower than int. - Return 1 if the result overflows. See above for restrictions. */ -# if _GL_HAVE___TYPEOF__ -# define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \ - (TYPE_SIGNED (__typeof__ (*(r))) \ - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \ - : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax)) -# else -# define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \ - (overflow (a, b, smin, smax) \ - ? (overflow (a, b, 0, umax) \ - ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \ - : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \ - : (overflow (a, b, 0, umax) \ - ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \ - : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0))) -# endif - -# define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \ - (sizeof *(r) == sizeof (signed char) \ - ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \ - signed char, SCHAR_MIN, SCHAR_MAX, \ - unsigned char, UCHAR_MAX) \ - : sizeof *(r) == sizeof (short int) \ - ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \ - short int, SHRT_MIN, SHRT_MAX, \ - unsigned short int, USHRT_MAX) \ - : sizeof *(r) == sizeof (int) \ - ? (EXPR_SIGNED (*(r)) \ - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - int, INT_MIN, INT_MAX) \ - : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ - unsigned int, 0, UINT_MAX)) \ - : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow)) -# ifdef LLONG_MAX -# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ - (sizeof *(r) == sizeof (long int) \ - ? (EXPR_SIGNED (*(r)) \ - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ - long int, LONG_MIN, LONG_MAX) \ - : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ - unsigned long int, 0, ULONG_MAX)) \ - : (EXPR_SIGNED (*(r)) \ - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ - long long int, LLONG_MIN, LLONG_MAX) \ - : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ - unsigned long long int, 0, ULLONG_MAX))) -# else -# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ - (EXPR_SIGNED (*(r)) \ - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ - long int, LONG_MIN, LONG_MAX) \ - : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ - unsigned long int, 0, ULONG_MAX)) -# endif -#endif - -/* Store the low-order bits of A B into *R, where the operation - is given by OP. Use the unsigned type UT for calculation to avoid - overflow problems. *R's type is T, with extrema TMIN and TMAX. - T must be a signed integer type. Return 1 if the result overflows. */ -#define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \ - (overflow (a, b, tmin, tmax) \ - ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \ - : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0)) - -/* Return the low-order bits of A B, where the operation is given - by OP. Use the unsigned type UT for calculation to avoid undefined - behavior on signed integer overflow, and convert the result to type T. - UT is at least as wide as T and is no narrower than unsigned int, - T is two's complement, and there is no padding or trap representations. - Assume that converting UT to T yields the low-order bits, as is - done in all known two's-complement C compilers. E.g., see: - https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html - - According to the C standard, converting UT to T yields an - implementation-defined result or signal for values outside T's - range. However, code that works around this theoretical problem - runs afoul of a compiler bug in Oracle Studio 12.3 x86. See: - https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html - As the compiler bug is real, don't try to work around the - theoretical problem. */ - -#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \ - ((t) ((ut) (a) op (ut) (b))) - -/* Return true if the numeric values A + B, A - B, A * B fall outside - the range TMIN..TMAX. Arguments should be integer expressions - without side effects. TMIN should be signed and nonpositive. - TMAX should be positive, and should be signed unless TMIN is zero. */ -#define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \ - ((b) < 0 \ - ? (((tmin) \ - ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \ - && (a) < (tmin) - (b)) \ - : (a) <= -1 - (b)) \ - || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \ - : (a) < 0 \ - ? (((tmin) \ - ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \ - && (b) < (tmin) - (a)) \ - : (b) <= -1 - (a)) \ - || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \ - && (tmax) < (a) + (b))) \ - : (tmax) < (b) || (tmax) - (b) < (a)) -#define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \ - (((a) < 0) == ((b) < 0) \ - ? ((a) < (b) \ - ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \ - : (tmax) < (a) - (b)) \ - : (a) < 0 \ - ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \ - || (a) - (tmin) < (b)) \ - : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \ - && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \ - && (tmax) <= -1 - (b)) \ - || (tmax) + (b) < (a))) -#define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \ - ((b) < 0 \ - ? ((a) < 0 \ - ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \ - ? (a) < (tmax) / (b) \ - : ((INT_NEGATE_OVERFLOW (b) \ - ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (+ (b)) - 1) \ - : (tmax) / -(b)) \ - <= -1 - (a))) \ - : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \ - ? (EXPR_SIGNED (a) \ - ? 0 < (a) + (tmin) \ - : 0 < (a) && -1 - (tmin) < (a) - 1) \ - : (tmin) / (b) < (a)) \ - : (b) == 0 \ - ? 0 \ - : ((a) < 0 \ - ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \ - ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \ - : (tmin) / (a) < (b)) \ - : (tmax) / (b) < (a))) +#define INT_ADD_WRAPV(a, b, r) _GL_INT_ADD_WRAPV (a, b, r) +#define INT_SUBTRACT_WRAPV(a, b, r) _GL_INT_SUBTRACT_WRAPV (a, b, r) +#define INT_MULTIPLY_WRAPV(a, b, r) _GL_INT_MULTIPLY_WRAPV (a, b, r) /* The following macros compute A + B, A - B, and A * B, respectively. If no overflow occurs, they set *R to the result and return 1; @@ -621,6 +315,8 @@ A, B, and *R should be integers; they need not be the same type, and they need not be all signed or all unsigned. + However, none of the integer types should be bit-precise, + and *R's type should not be char, bool, or an enumeration type. These macros work correctly on all known practical hosts, and do not rely on undefined behavior due to signed arithmetic overflow. @@ -632,8 +328,8 @@ These macros are tuned for B being a constant. */ -#define INT_ADD_OK(a, b, r) ! INT_ADD_WRAPV (a, b, r) -#define INT_SUBTRACT_OK(a, b, r) ! INT_SUBTRACT_WRAPV (a, b, r) -#define INT_MULTIPLY_OK(a, b, r) ! INT_MULTIPLY_WRAPV (a, b, r) +#define INT_ADD_OK(a, b, r) (! INT_ADD_WRAPV (a, b, r)) +#define INT_SUBTRACT_OK(a, b, r) (! INT_SUBTRACT_WRAPV (a, b, r)) +#define INT_MULTIPLY_OK(a, b, r) (! INT_MULTIPLY_WRAPV (a, b, r)) #endif /* _GL_INTPROPS_H */ From patchwork Sun Jan 5 05:56:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104042 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DF1F43858402 for ; Sun, 5 Jan 2025 06:04:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DF1F43858402 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=kTX7oSwm X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id F00903858C32 for ; Sun, 5 Jan 2025 05:58:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F00903858C32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F00903858C32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056696; cv=none; b=HxJHoKbNeirLo+SkF6JM1w5czKWNyMU0Yf0vm1dtswb9gkYGV+bGsTCnF6bnf/C5JSm5HFkbFZKA9df0KloMiZgS2X336V2yK4EtUwJd72xNDU/wf0yGsmlW3rS0WkJOFv4oezZTP9RZMSYmPByb32e9Z2hzkkfY7VnECNM433s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056696; c=relaxed/simple; bh=hi0lju+9v7y+ztuFatfCeSUYtW/Ek7q1j77IhNJh3l8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TrLOid4yISO80x1r9LIzjtN+HfktVm6VXYt9IrsVkzAVwlRMyo22V0+MFE2u4cx0t6Jld75N+PXk0s7B3moJXjVsRn9QXaGROlbFtnWOwD3WQdZvcUvFplca/sSvMGyDbgnjNYIw4mc7ZKU4/HRriyHwkCcUuduEAMCRblYg1OE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F00903858C32 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 6DC0B3C123847 for ; Sat, 4 Jan 2025 21:58:15 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id RKIUZMCxHTjs; Sat, 4 Jan 2025 21:58:14 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D71A63C123843; Sat, 4 Jan 2025 21:58:14 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu D71A63C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056694; bh=dtvjRGLbV0v+Sc2rXgONf+7y8K6yTbSzZQFGJkZp27Y=; h=From:To:Date:Message-ID:MIME-Version; b=kTX7oSwmtmdvsVW1kW6lmyj5UKNa81W1jXxASk6+IKecR/Lb40GdA3yTRTfbRB6A9 F8mXlwVZwI1p5k2Sh10p/5+C/8ss9F2tOWThPNjitnlA00OuLOYWTsTR3q2fFjAC1L L4yN9Kpr9hI7hRNcqJy9taim34BFDSxZuwi6YmezHCZwAFAJZBBWKwg3bhgcTUlpDk QzVMlKOWzLXSevD6mbs336M6Emg8fFoyQVCPNig6ymqEelbRLAvNgsyG1WmR5uYC/P v0xUTguabmVBQmNGWk5oaNOHsoMZdbJPPFIE+6qh7MBBZ4vCSYe/mP7ZskmgQIU+Vb iLhBupoJsGv2A== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id UVZmY_ebmTYb; Sat, 4 Jan 2025 21:58:14 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id C02763C082EB9; Sat, 4 Jan 2025 21:58:14 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 02/59] Support stdckdint.h internally if older GCC Date: Sat, 4 Jan 2025 21:56:37 -0800 Message-ID: <20250105055750.1668721-3-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * include/stdckdint.h: New file. * include/stdckdint.in.h: New file, copied from Gnulib. --- CONTRIBUTED-BY | 6 ++++++ SHARED-FILES | 2 ++ include/stdckdint.h | 6 ++++++ include/stdckdint.in.h | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 include/stdckdint.h create mode 100644 include/stdckdint.in.h diff --git a/CONTRIBUTED-BY b/CONTRIBUTED-BY index 652642fe86..87ba0dfc58 100644 --- a/CONTRIBUTED-BY +++ b/CONTRIBUTED-BY @@ -1418,6 +1418,12 @@ include/list.h: include/list_t.h: Contributed by Ulrich Drepper , 2002. +include/stdckdint.h: + Written by Paul Eggert. + +include/stdckdint.in.h: + Written by Paul Eggert. + inet/bug-if1.c: Contributed by Ulrich Drepper , 2004. diff --git a/SHARED-FILES b/SHARED-FILES index 444eff5141..2edeef7f03 100644 --- a/SHARED-FILES +++ b/SHARED-FILES @@ -35,6 +35,8 @@ gnulib: include/intprops.h # Merged from gnulib 2021-09-21 include/regex.h + # Merged from gnulib 2024-10-14 + include/stdckdint.in.h locale/programs/3level.h # Merged from gnulib 2014-6-23 malloc/obstack.c diff --git a/include/stdckdint.h b/include/stdckdint.h new file mode 100644 index 0000000000..713fb4559d --- /dev/null +++ b/include/stdckdint.h @@ -0,0 +1,6 @@ +#if __GNUC__ < 14 +# include +# include "stdckdint.in.h" +#else +# include_next +#endif diff --git a/include/stdckdint.in.h b/include/stdckdint.in.h new file mode 100644 index 0000000000..83277b728e --- /dev/null +++ b/include/stdckdint.in.h @@ -0,0 +1,35 @@ +/* stdckdint.h -- checked integer arithmetic + + Copyright 2022-2025 Free Software Foundation, Inc. + + This program 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. + + This program 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 this program. If not, see . */ + +#ifndef _GL_STDCKDINT_H +#define _GL_STDCKDINT_H + +#include "intprops-internal.h" + +/* Store into *R the low-order bits of A + B, A - B, A * B, respectively. + Return 1 if the result overflows, 0 otherwise. + A, B, and *R can have any integer type other than char, bool, a + bit-precise integer type, or an enumeration type. + + These are like the standard macros introduced in C23, except that + arguments should not have side effects. */ + +#define ckd_add(r, a, b) ((bool) _GL_INT_ADD_WRAPV (a, b, r)) +#define ckd_sub(r, a, b) ((bool) _GL_INT_SUBTRACT_WRAPV (a, b, r)) +#define ckd_mul(r, a, b) ((bool) _GL_INT_MULTIPLY_WRAPV (a, b, r)) + +#endif /* _GL_STDCKDINT_H */ From patchwork Sun Jan 5 05:56:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104047 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 95FC03858C56 for ; Sun, 5 Jan 2025 06:09:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 95FC03858C56 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=Pm8kXwK7 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id E8EC73858C62 for ; Sun, 5 Jan 2025 05:58:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E8EC73858C62 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E8EC73858C62 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056700; cv=none; b=C6HTfo2SB6vPRvowpv0myYiWc6SjIqm8hjvF195B3vsMtnLDsVbClG9s7TqcY0CTCK6ZoF0a79t1/ECbUsFtpa4mxJxzaBmnuAFLpT4BxRrALD9WH/KpbBabnp8YVb6nQod2RSluo4edkkQLyqb/GQwKaZzTmkepEsahyZR3FoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056700; c=relaxed/simple; bh=e9ZrO/RYTM4g7TYI6ojr2OxwGdB3maTN1WBGv8derGs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bbNAaKVK8Bp+a1GhG2np8zvocMHumdVXBN112jcFn2wrAF7ffQEh5Ca/DcZ2VBiUc/1cErdmh42vu7B/+0GafrNsfwNb1G78vzFrfayfLXBxt5lf9PXKzo9V11OF24m5sN9R+rOi7tCMTUiLVxDrFUdphPSSNUVgvp3lOQe2xe4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8EC73858C62 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 626D53C123844 for ; Sat, 4 Jan 2025 21:58:19 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id Jn9kC3Nb1qwi; Sat, 4 Jan 2025 21:58:18 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 812653C123843; Sat, 4 Jan 2025 21:58:18 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 812653C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056698; bh=4sKb2uDLNkoCwsv3Ud1YMy/HvwusuoEcuJGnd+v9hVM=; h=From:To:Date:Message-ID:MIME-Version; b=Pm8kXwK7gnvmpJgrVcjJ9/zYooQW0rcvnhP+mjcfriQGKqSdC55rDm+rC2kH1/4gF IoXkdeJTIKtacshVMqWvgWBMYejOgEAZwDMerF3umXFgKnac8F2mFMqE5XNAOSGaAq rUMBqknU4k8E4vCQDh3+400rUQl3vTAp2/bU94GHD2eQ/g4ys+7Kr1+8k6huLc8Xrg vVvUXmpQRkjfBXT4TtUAfjB6X79b+oy0mTNGV+qqjTLwM+UFWdkCqeyl3sARijCIMe tpsjoWeZp3EbHUTCDShYjGQhvPOxFylyGyojw0Bk8vpbXckj4CmkeHMtlJ0q05GWuv Rng76+cUrj3JA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id KYAjEWDBFG31; Sat, 4 Jan 2025 21:58:18 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 679F93C082EB9; Sat, 4 Jan 2025 21:58:18 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 03/59] Fix mishandling of default DST rule Date: Sat, 4 Jan 2025 21:56:38 -0800 Message-ID: <20250105055750.1668721-4-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org For timezone settings like TZ="AST4ADT" where POSIX says the default is implementation-defined, time/tzfile.c has long had vestiges of an old attempt to be upward compatible with UNIX System V. This code has not worked for decades and evidently is not being used, so remove it and use the simpler fallback code already present in time/tzset.c, which uses US DST. Also, add a few test cases to demonstrate some bugs in the removed code. * time/tst-posixtz.c: Include stdckdint.h. (tests): WHEN is now intmax_t, not time_t, to support skipping tests after 2038 on 32-bit time_t. All uses changed. Make it const while we're at it. Add several tests for AST4ADT. * time/tzfile.c (rule_dstoff): Remove. All uses removed. (__tzfile_read): Remove args EXTRA, EXTRAP. All uses changed. (__tzfile_default): Remove. All uses removed. (__tzfile_compute): Remove no-longer-needed code dealing with __tzfile_default. * timezone/Makefile (build-testdata): Add '-p America/New_York' option if compiling 'northamerica', so that we can test non-use of posixrules. * timezone/tst-timezone.c: Include stdbool.h, stdckdint.h. (failed): Now bool, not int. All uses changed. (do_test): Test AST4ADT too. --- NEWS | 7 +++ include/time.h | 6 +-- manual/time.texi | 2 +- time/tst-posixtz.c | 56 ++++++++++++++++++-- time/tzfile.c | 112 ++-------------------------------------- time/tzset.c | 19 +------ timezone/Makefile | 3 +- timezone/tst-timezone.c | 64 ++++++++++++++++++++--- 8 files changed, 124 insertions(+), 145 deletions(-) diff --git a/NEWS b/NEWS index 00c569fe85..3e6227a448 100644 --- a/NEWS +++ b/NEWS @@ -84,6 +84,13 @@ Deprecated and removed features, and other changes affecting compatibility: explicitly because of the executable bit in GNU_STACK, and the stack is not already executable. Instead, loading such objects will fail. +* The default daylight saving rules for old Unix System V style TZ + strings like TZ="AST4ADT" are now those of current US DST. Although + the rules were supposed to be those of /usr/share/zoneinfo/posixrules, + this feature, which has been disabled by default and marked obsolete + upstream in tzcode, did not work in either glibc or tzcode, and in + practice posixrules invariably specified current US DST anyway. + Changes to build and runtime requirements: * On recent Linux kernels with vDSO getrandom support, getrandom does not diff --git a/include/time.h b/include/time.h index f599eeed4e..52bb22abd9 100644 --- a/include/time.h +++ b/include/time.h @@ -54,14 +54,10 @@ extern char *__tzstring (const char *string) attribute_hidden; extern int __use_tzfile attribute_hidden; -extern void __tzfile_read (const char *file, size_t extra, - char **extrap) attribute_hidden; +extern void __tzfile_read (const char *file) attribute_hidden; extern void __tzfile_compute (__time64_t timer, int use_localtime, long int *leap_correct, int *leap_hit, struct tm *tp) attribute_hidden; -extern void __tzfile_default (const char *std, const char *dst, - int stdoff, int dstoff) - attribute_hidden; extern void __tzset_parse_tz (const char *tz) attribute_hidden; extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) __THROW attribute_hidden; diff --git a/manual/time.texi b/manual/time.texi index 90bc9a2566..899128ca7f 100644 --- a/manual/time.texi +++ b/manual/time.texi @@ -2724,7 +2724,7 @@ and offset for the corresponding daylight saving time zone; if the The remainder of the proleptic format, which starts with the first comma, describes when daylight saving time is in effect. This remainder is -optional and if omitted, @theglibc{} defaults to the daylight saving +optional and if omitted, @theglibc{} defaults to US daylight saving rules. rules that would be used if @env{TZ} had the value @t{"posixrules"}. However, other POSIX implementations default to different daylight saving rules, so portable @env{TZ} settings should not omit the diff --git a/time/tst-posixtz.c b/time/tst-posixtz.c index 9bec7ae4bb..936c5b8fc6 100644 --- a/time/tst-posixtz.c +++ b/time/tst-posixtz.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,10 +7,10 @@ struct { - time_t when; + intmax_t when; const char *tz; const char *result; -} tests[] = +} const tests[] = { { 909312849L, "AEST-10AEDST-11,M10.5.0,M3.5.0", "1998/10/25 21:54:09 dst=1 zone=AEDST" }, @@ -27,6 +28,43 @@ struct "1999/04/23 06:54:09 dst=1 zone=EDT" }, { 919973892L, "EST+5EDT,M4.1.0/2,M10.5.0/2", "1999/02/25 15:18:12 dst=0 zone=EST" }, + + /* Test Atlantic Standard / Atlantic Daylight transitions in 2024 and 2038, + with explicit DST rule. */ + { 1710050399, "AST4ADT,M3.2.0,M11.1.0", + "2024/03/10 01:59:59 dst=0 zone=AST" }, + { 1710050400, "AST4ADT,M3.2.0/2,M11.1.0/2", + "2024/03/10 03:00:00 dst=1 zone=ADT" }, + { 1730609999, "AST4ADT,M3.2.0/02:00,M11.1.0/02:00", + "2024/11/03 01:59:59 dst=1 zone=ADT" }, + { 1730610000, "AST4ADT,M3.2.0/02:00:00,M11.1.0/02:00:00", + "2024/11/03 01:00:00 dst=0 zone=AST" }, + { 2152159199, "AST4ADT,M3.2.0,M11.1.0", + "2038/03/14 01:59:59 dst=0 zone=AST" }, + { 2152159200, "AST4ADT,M3.2.0/2,M11.1.0/2", + "2038/03/14 03:00:00 dst=1 zone=ADT" }, + { 2172718799, "AST4ADT,M3.2.0/02:00,M11.1.0/02:00", + "2038/11/07 01:59:59 dst=1 zone=ADT" }, + { 2172718800, "AST4ADT,M3.2.0/02:00:00,M11.1.0/02:00:00", + "2038/11/07 01:00:00 dst=0 zone=AST" }, + + /* Likewise, but with default DST rule. */ + { 1710050399, "AST4ADT", + "2024/03/10 01:59:59 dst=0 zone=AST" }, + { 1710050400, "AST4ADT", + "2024/03/10 03:00:00 dst=1 zone=ADT" }, + { 1730609999, "AST4ADT", + "2024/11/03 01:59:59 dst=1 zone=ADT" }, + { 1730610000, "AST4ADT", + "2024/11/03 01:00:00 dst=0 zone=AST" }, + { 2152159199, "AST4ADT", + "2038/03/14 01:59:59 dst=0 zone=AST" }, + { 2152159200, "AST4ADT", + "2038/03/14 03:00:00 dst=1 zone=ADT" }, + { 2172718799, "AST4ADT", + "2038/11/07 01:59:59 dst=1 zone=ADT" }, + { 2172718800, "AST4ADT", + "2038/11/07 01:00:00 dst=0 zone=AST" }, }; static int @@ -34,6 +72,7 @@ do_test (void) { int result = 0; size_t cnt; + time_t when; for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt) { @@ -41,12 +80,18 @@ do_test (void) struct tm *tmp; printf ("TZ = \"%s\", time = %jd => ", tests[cnt].tz, - (intmax_t) tests[cnt].when); + tests[cnt].when); fflush (stdout); + if (ckd_add (&when, tests[cnt].when, 0)) + { + puts ("SKIPPED [time_t out of range for this platform]"); + continue; + } + setenv ("TZ", tests[cnt].tz, 1); - tmp = localtime (&tests[cnt].when); + tmp = localtime (&when); snprintf (buf, sizeof (buf), "%04d/%02d/%02d %02d:%02d:%02d dst=%d zone=%s", @@ -66,7 +111,8 @@ do_test (void) } setenv ("TZ", "Universal", 1); - localtime (&tests[0].when); + when = tests[0].when; + localtime (&when); printf ("TZ = \"Universal\" daylight %d tzname = { \"%s\", \"%s\" }", daylight, tzname[0], tzname[1]); if (! daylight) diff --git a/time/tzfile.c b/time/tzfile.c index edb5643335..bca8b3f4ef 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -57,7 +57,6 @@ static size_t num_types; static struct ttinfo *types; static char *zone_names; static long int rule_stdoff; -static long int rule_dstoff; static size_t num_leaps; static struct leap *leaps; static char *tzspec; @@ -103,7 +102,7 @@ decode64 (const void *ptr) void -__tzfile_read (const char *file, size_t extra, char **extrap) +__tzfile_read (const char *file) { static const char default_tzdir[] = TZDIR; size_t num_isstd, num_isgmt; @@ -257,7 +256,6 @@ __tzfile_read (const char *file, size_t extra, char **extrap) unsigned char type_idxs[num_types]; char zone_names[chars]; char tzspec[tzspec_len]; - char extra_array[extra]; // Stored into *pextras if requested. The piece-wise allocations from buf below verify that no overflow/wraparound occurred in these computations. @@ -277,7 +275,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap) + num_types * sizeof (struct ttinfo) + num_transitions /* type_idxs */ + chars /* zone_names */ - + tzspec_len + extra); + + tzspec_len); transitions = malloc (total_size); if (transitions == NULL) goto lose; @@ -295,8 +293,6 @@ __tzfile_read (const char *file, size_t extra, char **extrap) tzspec = alloc_buffer_alloc_array (&buf, char, tzspec_len); else tzspec = NULL; - if (extra > 0) - *extrap = alloc_buffer_alloc_array (&buf, char, extra); if (alloc_buffer_has_failed (&buf)) goto lose; @@ -447,7 +443,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap) daylight_saved = 0; if (num_transitions == 0) /* Use the first rule (which should also be the only one). */ - rule_stdoff = rule_dstoff = types[0].offset; + rule_stdoff = types[0].offset; else { rule_stdoff = 0; @@ -488,98 +484,6 @@ __tzfile_read (const char *file, size_t extra, char **extrap) transitions = NULL; } -/* The user specified a hand-made timezone, but not its DST rules. - We will use the names and offsets from the user, and the rules - from the TZDEFRULES file. */ - -void -__tzfile_default (const char *std, const char *dst, - int stdoff, int dstoff) -{ - size_t stdlen = strlen (std) + 1; - size_t dstlen = strlen (dst) + 1; - size_t i; - int isdst; - char *cp; - - __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp); - if (!__use_tzfile) - return; - - if (num_types < 2) - { - __use_tzfile = 0; - return; - } - - /* Ignore the zone names read from the file and use the given ones - instead. */ - __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen); - zone_names = cp; - - /* Now there are only two zones, regardless of what the file contained. */ - num_types = 2; - - /* Now correct the transition times for the user-specified standard and - daylight offsets from GMT. */ - isdst = 0; - for (i = 0; i < num_transitions; ++i) - { - struct ttinfo *trans_type = &types[type_idxs[i]]; - - /* We will use only types 0 (standard) and 1 (daylight). - Fix up this transition to point to whichever matches - the flavor of its original type. */ - type_idxs[i] = trans_type->isdst; - - if (trans_type->isgmt) - /* The transition time is in GMT. No correction to apply. */ ; - else if (isdst && !trans_type->isstd) - /* The type says this transition is in "local wall clock time", and - wall clock time as of the previous transition was DST. Correct - for the difference between the rule's DST offset and the user's - DST offset. */ - transitions[i] += dstoff - rule_dstoff; - else - /* This transition is in "local wall clock time", and wall clock - time as of this iteration is non-DST. Correct for the - difference between the rule's standard offset and the user's - standard offset. */ - transitions[i] += stdoff - rule_stdoff; - - /* The DST state of "local wall clock time" for the next iteration is - as specified by this transition. */ - isdst = trans_type->isdst; - } - - /* Now that we adjusted the transitions to the requested offsets, - reset the rule_stdoff and rule_dstoff values appropriately. They - are used elsewhere. */ - rule_stdoff = stdoff; - rule_dstoff = dstoff; - - /* Reset types 0 and 1 to describe the user's settings. */ - types[0].idx = 0; - types[0].offset = stdoff; - types[0].isdst = 0; - types[1].idx = stdlen; - types[1].offset = dstoff; - types[1].isdst = 1; - - /* Reset time zone abbreviations to point to the user's abbreviations. */ - __tzname[0] = (char *) std; - __tzname[1] = (char *) dst; - - /* Set the timezone. */ - __timezone = -types[0].offset; - - /* Invalidate the tzfile attribute cache to force rereading - TZDEFRULES the next time it is used. */ - tzfile_dev = 0; - tzfile_ino = 0; - tzfile_mtime = 0; -} - void __tzfile_compute (__time64_t timer, int use_localtime, long int *leap_correct, int *leap_hit, @@ -642,16 +546,6 @@ __tzfile_compute (__time64_t timer, int use_localtime, /* Use the rules from the TZ string to compute the change. */ __tz_compute (timer, tp, 1); - /* If tzspec comes from posixrules loaded by __tzfile_default, - override the STD and DST zone names with the ones user - requested in TZ envvar. */ - if (__glibc_unlikely (zone_names == (char *) &leaps[num_leaps])) - { - assert (num_types == 2); - __tzname[0] = __tzstring (zone_names); - __tzname[1] = __tzstring (&zone_names[strlen (zone_names) + 1]); - } - goto leap; } else diff --git a/time/tzset.c b/time/tzset.c index 0ddc203287..e178742222 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -331,22 +331,7 @@ __tzset_parse_tz (const char *tz) if (*tz != '\0') { if (parse_tzname (&tz, 1)) - { - parse_offset (&tz, 1); - if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0')) - { - /* There is no rule. See if there is a default rule - file. */ - __tzfile_default (tz_rules[0].name, tz_rules[1].name, - tz_rules[0].offset, tz_rules[1].offset); - if (__use_tzfile) - { - free (old_tz); - old_tz = NULL; - return; - } - } - } + parse_offset (&tz, 1); /* Figure out the standard <-> DST rules. */ if (parse_rule (&tz, 0)) parse_rule (&tz, 1); @@ -402,7 +387,7 @@ tzset_internal (int always) old_tz = tz ? __strdup (tz) : NULL; /* Try to read a data file. */ - __tzfile_read (tz, 0, NULL); + __tzfile_read (tz); if (__use_tzfile) return; diff --git a/timezone/Makefile b/timezone/Makefile index ebe5cf73a1..6e28a2e670 100644 --- a/timezone/Makefile +++ b/timezone/Makefile @@ -80,7 +80,8 @@ CFLAGS-zic.c += $(tz-cflags) -Wno-unused-variable # Don't add leapseconds here since test-tz made checks that work only without # leapseconds. define build-testdata -$(built-program-cmd) -d $(testdata) -y ./yearistype $<; \ +$(built-program-cmd) -d $(testdata) \ + $(if $(findstring northamerica,$<),-p America/New_York) $<; \ $(evaluate-test) endef diff --git a/timezone/tst-timezone.c b/timezone/tst-timezone.c index 2d90be305d..882b3b8fd6 100644 --- a/timezone/tst-timezone.c +++ b/timezone/tst-timezone.c @@ -16,13 +16,15 @@ . */ #include +#include +#include #include #include #include #include #include -int failed = 0; +bool failed = false; struct test_times { @@ -69,20 +71,20 @@ check_tzvars (const char *name, int dayl, int timez, const char *const tznam[]) { printf ("*** Timezone: %s, daylight is: %d but should be: %d\n", name, daylight, dayl); - ++failed; + failed = true; } if (timezone != timez) { printf ("*** Timezone: %s, timezone is: %ld but should be: %d\n", name, timezone, timez); - ++failed; + failed = true; } for (i = 0; i <= 1; ++i) if (strcmp (tzname[i], tznam[i]) != 0) { printf ("*** Timezone: %s, tzname[%d] is: %s but should be: %s\n", name, i, tzname[i], tznam[i]); - ++failed; + failed = true; } } @@ -106,7 +108,7 @@ do_test (void) if (putenv (buf)) { puts ("putenv failed."); - failed = 1; + failed = true; } tzset (); print_tzvars (); @@ -136,7 +138,7 @@ do_test (void) puts ("TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1"); if (strcmp (buf, "TZ=Europe/London 892162800 0 0 0 10 3 98 5 99 1") != 0) { - failed = 1; + failed = true; fputs (" FAILED ***", stdout); } } @@ -159,11 +161,59 @@ do_test (void) puts ("TZ=GMT 892166400 0 0 0 10 3 98 5 99 0"); if (strcmp (buf, "TZ=GMT 892166400 0 0 0 10 3 98 5 99 0") != 0) { - failed = 1; + failed = true; fputs (" FAILED ***", stdout); } } + /* Test AST4ADT here, as well as in ../time/tst-posixtz.c, because + formerly the tzset implementation read the posixrules file + generated as part of the timezone data, and some bugs of that old + implementation are covered by this test. */ + { + static struct + { + intmax_t when; + char const *tz; + char const *result; + } const default_dst_test[] = + { + /* Test Atlantic Standard / Atlantic Daylight transitions in + 2024 and 2038, with default DST rule. */ + { 1710050399, "AST4ADT", "2024-03-10 01:59:59 -0400 (AST)" }, + { 1710050400, "AST4ADT", "2024-03-10 03:00:00 -0300 (ADT)" }, + { 1730609999, "AST4ADT", "2024-11-03 01:59:59 -0300 (ADT)" }, + { 1730610000, "AST4ADT", "2024-11-03 01:00:00 -0400 (AST)" }, + { 2152159199, "AST4ADT", "2038-03-14 01:59:59 -0400 (AST)" }, + { 2152159200, "AST4ADT", "2038-03-14 03:00:00 -0300 (ADT)" }, + { 2172718799, "AST4ADT", "2038-11-07 01:59:59 -0300 (ADT)" }, + { 2172718800, "AST4ADT", "2038-11-07 01:00:00 -0400 (AST)" }, + }; + + for (int i = 0; i < sizeof default_dst_test / sizeof *default_dst_test; i++) + { + sprintf (envstring, "TZ=%s", default_dst_test[i].tz); + /* No putenv call needed! */ + + time_t when; + if (ckd_add (&when, default_dst_test[i].when, 0)) + { + printf ("time=%jd out of range for test; test skipped\n", + default_dst_test[i].when); + continue; + } + char buf[100]; + strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S %z (%Z)", + localtime (&when)); + bool bad = strcmp (buf, default_dst_test[i].result) != 0; + printf ("%s should be\n%s when time=%jd TZ=%s%s\n\n", + buf, default_dst_test[i].result, + default_dst_test[i].when, default_dst_test[i].tz, + bad ? " FAILED ***" : ""); + failed |= bad; + } + } + return failed ? EXIT_FAILURE : EXIT_SUCCESS; } From patchwork Sun Jan 5 05:56:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104043 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B48AA3858CD1 for ; Sun, 5 Jan 2025 06:06:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B48AA3858CD1 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=FOSGx131 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id D29E13858C60 for ; Sun, 5 Jan 2025 05:58:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D29E13858C60 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D29E13858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056707; cv=none; b=mubW9PUOh4BZqy+bnbudnJ2zUVuoKi9Vt0rhMgWHP+XGEYyLLjzr6BZghYZr+BAmi7dKfjd6ITcqSOJEgxbPdMqdCHDc258nxnxUFoZ/cO4+StkXOVJjpEtZ0kYAnfAYyeIFkDrIa+Ad7s1/Fw5XLsx0eeXnoskhHJm4RMV4L9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056707; c=relaxed/simple; bh=mpwZ58HKiN6WvacctrlS3zxdnCdLewNCNEeQrevkZ58=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ushaiBXgqv3kWFTxUf0GNo/sUfGYz0dW02vOx4Uw3VjummeMkNILqlSSU0H28lbfZrLtVOnXmehjRMnzN0Loy/7UPFBpLVXK4csrgXosAf+i0DUHzlvFlAOYLIRLvcWSMtY+9V8njcHehD+rNNKyNsgkwH3t9WgW4iXQcAXdyc8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D29E13858C60 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 584293C123845 for ; Sat, 4 Jan 2025 21:58:22 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id P9SdqIAB_2dT; Sat, 4 Jan 2025 21:58:21 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id C77AB3C123843; Sat, 4 Jan 2025 21:58:21 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu C77AB3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056701; bh=mqOqiSPjUpIkr6K0Lhuj/oHEbuJnv3YwIpP/3bRpUxc=; h=From:To:Date:Message-ID:MIME-Version; b=FOSGx131xV4MlwFHlZv6v/nssrcvpyUNdxYBwaGwcxCKFFr5kVZYKduxFVz4oqqJW p+2Np03use281C3YO3YdQz1dYeAWJVoSEccuTZjlhxs3aPP+EhALRYoyFcE7pF1B7M fVw8pvVta96YrNsO5EgTc1RbddcTEldttFq9Q5n0OCOYeF28hxENL5VU8OJRyQdMm3 DqinQBM6w5OMgq0R5VtUyS/KhJejZ2BsjP3smZJZB/gJsxAorrTcZrVMefuxE34IYw Vf20RmUcZooetGwlGrxZfq5eygOou3r0IPpVa9FkFetelannxmfCI72AUecYazHnfR /lfk8ZcNa/rxg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id qXjHRLzRFPlQ; Sat, 4 Jan 2025 21:58:21 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id B0E863C082EB9; Sat, 4 Jan 2025 21:58:21 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 04/59] Don't worry about !TZDEFAULT Date: Sat, 4 Jan 2025 21:56:39 -0800 Message-ID: <20250105055750.1668721-5-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (tzset_internal): Simplify due to the fact that TZDEFAULT is not a null pointer (tzfile.c is already assuming this). --- time/tzset.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/time/tzset.c b/time/tzset.c index e178742222..1a9a03a436 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -384,17 +384,17 @@ tzset_internal (int always) /* Save the value of `tz'. */ free (old_tz); - old_tz = tz ? __strdup (tz) : NULL; + old_tz = __strdup (tz); /* Try to read a data file. */ __tzfile_read (tz); if (__use_tzfile) return; - /* No data file found. Default to UTC if nothing specified. */ + /* No data file found. Default to UTC if nothing specified or if + TZDEFAULT is broken. */ - if (tz == NULL || *tz == '\0' - || (TZDEFAULT != NULL && strcmp (tz, TZDEFAULT) == 0)) + if (*tz == '\0' || strcmp (tz, TZDEFAULT) == 0) { memset (tz_rules, '\0', sizeof tz_rules); tz_rules[0].name = tz_rules[1].name = "UTC"; From patchwork Sun Jan 5 05:56:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104039 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 294113858CDA for ; Sun, 5 Jan 2025 06:01:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 294113858CDA Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=o3zMhuTD X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id A53D23858D21 for ; Sun, 5 Jan 2025 05:58:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A53D23858D21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A53D23858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056710; cv=none; b=aO0YWoYou33aDR4jOStvFaipepjjeYq8UtpAUUGCFas8doeFiq3pJjJkttsYw+B489lJ/PuTgJuccKid8GiwcbNti7d2Plm6lEFsE0/n0Yf9oEGR7KarGIcnbDgM/ykGm97TCyM1mv4QVuYaMUbV9uGue6eqTbKgtXL+gKWqk0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056710; c=relaxed/simple; bh=Cc+efVkUG+5IUm3bwbc6eAUc1isFRgEJ0qFyLZH5+Zs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pBVH8ymhhyTsF7dJ4LBXXfsfSXWSGNHd7HJZaShp44VwKDhMDvoTdeBNpLCQ+14BSCoFp3K1fS+zlA+bM1Se9jCUstTLuw2xG0RMnVewDQ4bt+pD0X1qXLfgJtF3f+qi11E7cBqlqMkALUMygCocs//PRGHsDqDL1aE5Yc4Qlj8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A53D23858D21 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 22B3E3C123844 for ; Sat, 4 Jan 2025 21:58:25 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id WcklPoQ0eYCT; Sat, 4 Jan 2025 21:58:24 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id C83113C123843; Sat, 4 Jan 2025 21:58:24 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu C83113C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056704; bh=ihbYVsg7lkDbxamJWKBglLgxHWE0AmEThPgP4ZU/dRM=; h=From:To:Date:Message-ID:MIME-Version; b=o3zMhuTDqF5X4KWSwvaDeKXKvPM/K1iT4UnybE7gcWxix/PEHhBUKpVk8xgY+6TOf f/XK/dZEvK2jtGJZtwaVlDAXROBtaUxkVjyNTEXvqjDF72BNwaRtvJjYAAO2IR3rrA op3lToLwg9E00fpUvHDrfQIhohkB1VzUUiTiMEcJsWTR9jhml/Tn2JymukglMSlfiC cjsiV2A1k5nY1TdW56tPFPk6DOuXnw8IppMcH5AFTucrziHpn91B84ELKGol26qapy ZyjBxJQVpqQc9e2PP+n9UflU5DHXOc2c5RurGffvny3kzQT1np6fCaEDfnSqLmnU4E +6OM96u/sHQGA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id H3tz100xZaoU; Sat, 4 Jan 2025 21:58:24 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id B07F53C082EB9; Sat, 4 Jan 2025 21:58:24 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 05/59] Support static_assert in pre-C23 C Date: Sat, 4 Jan 2025 21:56:40 -0800 Message-ID: <20250105055750.1668721-6-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * include/sys/cdefs.h (static_assert) [!C++ && __STDC_VERSION__ < 202311]: New macro. * timezone/private.h, timezone/version: Copy from tzcode git. --- include/sys/cdefs.h | 8 ++++++++ timezone/private.h | 2 +- timezone/version | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/sys/cdefs.h b/include/sys/cdefs.h index a676f75f62..cd3127aaca 100644 --- a/include/sys/cdefs.h +++ b/include/sys/cdefs.h @@ -7,6 +7,14 @@ # define _Static_assert(expr, diagnostic) _Static_assert (expr, diagnostic) #endif +/* Support single-arg static_assert in C code even on pre-C23 compilers, + for compatibility with Gnulib. */ +#if (!defined __cplusplus \ + && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 202311) \ + && !defined static_assert) +# define static_assert(expr) _Static_assert (expr, #expr) +#endif + #include #ifndef _ISOMAC diff --git a/timezone/private.h b/timezone/private.h index c33041049f..3db0121135 100644 --- a/timezone/private.h +++ b/timezone/private.h @@ -50,7 +50,7 @@ # include #endif -#if __STDC_VERSION__ < 202311 +#if __STDC_VERSION__ < 202311 && !defined static_assert # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1] #endif diff --git a/timezone/version b/timezone/version index 04fe674443..c6db4af447 100644 --- a/timezone/version +++ b/timezone/version @@ -1 +1 @@ -2024a +2024b-11-gaf54a9e From patchwork Sun Jan 5 05:56:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104052 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2A8893858C54 for ; Sun, 5 Jan 2025 06:13:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2A8893858C54 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=Ti/aK6PD X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 92D753858401 for ; Sun, 5 Jan 2025 05:58:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 92D753858401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 92D753858401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056709; cv=none; b=nNKEo5mPbrweta+m0t5SWNNa0WCMLgsgjTW9NMdhF/ahJggcHsFm+HWiIJNXHXBEu8N/R++zWgJwUnpjsapNTQgXsTnzFwf69P+SdtEedTpM88DFCBTNf68TDWPZQPcfxOzz31z8iXlSWGJ88ir82rGYZ5LjyXHHcO2lM8KwI8c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056709; c=relaxed/simple; bh=7Vx5wA2tJdYvRAL5ik5NbdFvlGIhHGvFoY9wPAw/+us=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=A5PTWUqewFffW0PgF7+vwPSgLyIyjvdt2lxMajq7iIkMgQLZQDQipWI72thN3mqZldMts+0yzlMyecd6Bu8skV6TJEsFVCZ7QpI0w21ofu68k1iI6QfvQIgFvUK3rlzVxSjv8vxtcX5Sz/2mLasvi7xQ29WKoWNZ0Y4pSPKznZs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 92D753858401 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 157DB3C123843 for ; Sat, 4 Jan 2025 21:58:29 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id zRWMO7_jfK14; Sat, 4 Jan 2025 21:58:28 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 6EB6C3C123847; Sat, 4 Jan 2025 21:58:28 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 6EB6C3C123847 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056708; bh=XaxOrdMKV88NnEVSEdN/2wJD5VlfXW6RM42YBgnYPzY=; h=From:To:Date:Message-ID:MIME-Version; b=Ti/aK6PDmgr16784iOWF0x50NGn8lV+2xB8nSV4Abozy5qvE2kklxinl6A8eaqGnH E8ilbsuM4Gi/hFpSQpXL/xy/DCRfWYfDd1pCas0SbahrHfkpG2JsLok5Hl1pcYIPdV LUzIq+Thdilq8hK8hu4PaWkff64mkXGVehsDQN2ELzz0zZSJqPYkWSnegU6sgvuApS hCaP0mvnPm59qb3Wfh0JPcRfSAA5grTX11ak8JE2amU2f1/hVlWCuxpACq3pehgftM gTeQCTly6nnjcZ2z6OTb/MluMWkQs2tuKYvmBAKGN3fEJLnfmnFb5nESN1sr/cpDQk YvHD/wEWT0sdA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 1D7BNI--jUOW; Sat, 4 Jan 2025 21:58:28 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 569313C082EB9; Sat, 4 Jan 2025 21:58:28 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 06/59] Sync mktime.c from Gnulib 2024-10-04 Date: Sat, 4 Jan 2025 21:56:41 -0800 Message-ID: <20250105055750.1668721-7-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_NUMSUBJECT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Refactor by syncing from Gnulib commit 8e0c8d148117086894fe26576d9255ea7f047a6d (Oct 4 21:07:09 2024 -0700), which now uses C23's stdckdint.h for integer overflow checking, uses static_assert instead of verify, uses a convert_time function that is closer to what glibc wants, does not try to match tm_isdst in timezones that never observe DST, and fixes a minor bug when timegm fails on 32-bit time_t platforms. * time/mktime-internal.h, time/mktime.c, time/timegm.c: Copy from Gnulib. --- time/mktime-internal.h | 9 ++- time/mktime.c | 124 ++++++++++++++++++++++------------------- time/timegm.c | 3 +- 3 files changed, 73 insertions(+), 63 deletions(-) diff --git a/time/mktime-internal.h b/time/mktime-internal.h index 5fbcc0ec60..1da98b4373 100644 --- a/time/mktime-internal.h +++ b/time/mktime-internal.h @@ -71,9 +71,8 @@ typedef int mktime_offset_t; #endif /* Subroutine of mktime. Return the time_t representation of TP and - normalize TP, given that a struct tm * maps to a time_t as performed - by FUNC. Record next guess for localtime-gmtime offset in *OFFSET. */ -extern __time64_t __mktime_internal (struct tm *tp, - struct tm *(*func) (__time64_t const *, - struct tm *), + normalize TP, given that a struct tm * maps to a time_t. If + LOCAL, the mapping is performed by localtime_r, otherwise by gmtime_r. + Record next guess for localtime-gmtime offset in *OFFSET. */ +extern __time64_t __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) attribute_hidden; diff --git a/time/mktime.c b/time/mktime.c index 86d496ca65..7f7d33492b 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -46,11 +46,11 @@ #include #include #include +#include #include #include #include -#include #ifndef NEED_MKTIME_INTERNAL # define NEED_MKTIME_INTERNAL 0 @@ -118,12 +118,12 @@ my_tzset (void) __time64_t values that mktime can generate even on platforms where __time64_t is wider than the int components of struct tm. */ -#if INT_MAX <= LONG_MAX / 4 / 366 / 24 / 60 / 60 +# if INT_MAX <= LONG_MAX / 4 / 366 / 24 / 60 / 60 typedef long int long_int; -#else +# else typedef long long int long_int; -#endif -verify (INT_MAX <= TYPE_MAXIMUM (long_int) / 4 / 366 / 24 / 60 / 60); +# endif +static_assert (INT_MAX <= TYPE_MAXIMUM (long_int) / 4 / 366 / 24 / 60 / 60); /* Shift A right by B bits portably, by dividing A by 2**B and truncating towards minus infinity. B should be in the range 0 <= B @@ -154,9 +154,9 @@ static long_int const mktime_max = (TYPE_MAXIMUM (long_int) < TYPE_MAXIMUM (__time64_t) ? TYPE_MAXIMUM (long_int) : TYPE_MAXIMUM (__time64_t)); -#define EPOCH_YEAR 1970 -#define TM_YEAR_BASE 1900 -verify (TM_YEAR_BASE % 100 == 0); +# define EPOCH_YEAR 1970 +# define TM_YEAR_BASE 1900 +static_assert (TM_YEAR_BASE % 100 == 0); /* Is YEAR + TM_YEAR_BASE a leap year? */ static bool @@ -171,9 +171,9 @@ leapyear (long_int year) } /* How many days come before each month (0-12). */ -#ifndef _LIBC +# ifndef _LIBC static -#endif +# endif const unsigned short int __mon_yday[2][13] = { /* Normal years. */ @@ -205,7 +205,7 @@ static long_int ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1, int year0, int yday0, int hour0, int min0, int sec0) { - verify (-1 / 2 == 0); + static_assert (-1 / 2 == 0); /* Compute intervening leap days correctly even if year is negative. Take care to avoid integer overflow here. */ @@ -250,29 +250,33 @@ tm_diff (long_int year, long_int yday, int hour, int min, int sec, tp->tm_hour, tp->tm_min, tp->tm_sec); } -/* Use CONVERT to convert T to a struct tm value in *TM. T must be in - range for __time64_t. Return TM if successful, NULL (setting errno) on - failure. */ +/* Convert T to a struct tm value in *TM. Use localtime64_r if LOCAL, + otherwise gmtime64_r. T must be in range for __time64_t. Return + TM if successful, NULL (setting errno) on failure. */ static struct tm * -convert_time (struct tm *(*convert) (const __time64_t *, struct tm *), - long_int t, struct tm *tm) +convert_time (long_int t, bool local, struct tm *tm) { __time64_t x = t; - return convert (&x, tm); + if (local) + return __localtime64_r (&x, tm); + else + return __gmtime64_r (&x, tm); } - -/* Use CONVERT to convert *T to a broken down time in *TP. - If *T is out of range for conversion, adjust it so that - it is the nearest in-range value and then convert that. - A value is in range if it fits in both __time64_t and long_int. - Return TP on success, NULL (setting errno) on failure. */ +/* Call it __tzconvert to sync with other parts of glibc. */ +#define __tz_convert convert_time + +/* Convert *T to a broken down time in *TP (as if by localtime if + LOCAL, otherwise as if by gmtime). If *T is out of range for + conversion, adjust it so that it is the nearest in-range value and + then convert that. A value is in range if it fits in both + __time64_t and long_int. Return TP on success, NULL (setting + errno) on failure. */ static struct tm * -ranged_convert (struct tm *(*convert) (const __time64_t *, struct tm *), - long_int *t, struct tm *tp) +ranged_convert (bool local, long_int *t, struct tm *tp) { long_int t1 = (*t < mktime_min ? mktime_min : *t <= mktime_max ? *t : mktime_max); - struct tm *r = convert_time (convert, t1, tp); + struct tm *r = __tz_convert (t1, local, tp); if (r) { *t = t1; @@ -293,7 +297,7 @@ ranged_convert (struct tm *(*convert) (const __time64_t *, struct tm *), long_int mid = long_int_avg (ok, bad); if (mid == ok || mid == bad) break; - if (convert_time (convert, mid, tp)) + if (__tz_convert (mid, local, tp)) ok = mid, oktm = *tp; else if (errno != EOVERFLOW) return NULL; @@ -309,36 +313,45 @@ ranged_convert (struct tm *(*convert) (const __time64_t *, struct tm *), } -/* Convert *TP to a __time64_t value, inverting - the monotonic and mostly-unit-linear conversion function CONVERT. - Use *OFFSET to keep track of a guess at the offset of the result, +/* Convert *TP to a __time64_t value. If LOCAL, the reverse mapping + is performed as if localtime, otherwise as if by gmtime. Use + *OFFSET to keep track of a guess at the offset of the result, compared to what the result would be for UTC without leap seconds. - If *OFFSET's guess is correct, only one CONVERT call is needed. - If successful, set *TP to the canonicalized struct tm; + If *OFFSET's guess is correct, only one reverse mapping call is + needed. If successful, set *TP to the canonicalized struct tm; otherwise leave *TP alone, return ((time_t) -1) and set errno. This function is external because it is used also by timegm.c. */ __time64_t -__mktime_internal (struct tm *tp, - struct tm *(*convert) (const __time64_t *, struct tm *), - mktime_offset_t *offset) +__mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) { struct tm tm; - /* The maximum number of probes (calls to CONVERT) should be enough - to handle any combinations of time zone rule changes, solar time, - leap seconds, and oscillations around a spring-forward gap. - POSIX.1 prohibits leap seconds, but some hosts have them anyway. */ + /* The maximum number of probes should be enough to handle any + combinations of time zone rule changes, solar time, leap seconds, + and oscillations around a spring-forward gap. POSIX.1 prohibits + leap seconds, but some hosts have them anyway. */ int remaining_probes = 6; - /* Time requested. Copy it in case CONVERT modifies *TP; this can - occur if TP is localtime's returned value and CONVERT is localtime. */ +#ifndef _LIBC + /* Gnulib mktime doesn't lock the tz state, so it may need to probe + more often if some other thread changes local time while + __mktime_internal is probing. Double the number of probes; this + should suffice for practical cases that are at all likely. */ + remaining_probes *= 2; +#endif + + /* Time requested. Copy it in case gmtime/localtime modify *TP; + this can occur if TP is localtime's returned value and CONVERT is + localtime. */ int sec = tp->tm_sec; int min = tp->tm_min; int hour = tp->tm_hour; int mday = tp->tm_mday; int mon = tp->tm_mon; int year_requested = tp->tm_year; - int isdst = tp->tm_isdst; + + /* If the timezone never observes DST, ignore any tm_isdst request. */ + int isdst = local && __daylight ? tp->tm_isdst : 0; /* 1 if the previous probe was DST. */ int dst2 = 0; @@ -379,7 +392,7 @@ __mktime_internal (struct tm *tp, /* Invert CONVERT by probing. First assume the same offset as last time. */ - INT_SUBTRACT_WRAPV (0, off, &negative_offset_guess); + ckd_sub (&negative_offset_guess, 0, off); long_int t0 = ydhms_diff (year, yday, hour, min, sec, EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, negative_offset_guess); @@ -389,7 +402,7 @@ __mktime_internal (struct tm *tp, while (true) { - if (! ranged_convert (convert, &t, &tm)) + if (! ranged_convert (local, &t, &tm)) return -1; long_int dt = tm_diff (year, yday, hour, min, sec, &tm); if (dt == 0) @@ -465,10 +478,10 @@ __mktime_internal (struct tm *tp, for (direction = -1; direction <= 1; direction += 2) { long_int ot; - if (! INT_ADD_WRAPV (t, delta * direction, &ot)) + if (! ckd_add (&ot, t, delta * direction)) { struct tm otm; - if (! ranged_convert (convert, &ot, &otm)) + if (! ranged_convert (local, &ot, &otm)) return -1; if (! isdst_differ (isdst, otm.tm_isdst)) { @@ -478,7 +491,7 @@ __mktime_internal (struct tm *tp, &otm); if (mktime_min <= gt && gt <= mktime_max) { - if (convert_time (convert, gt, &tm)) + if (__tz_convert (gt, local, &tm)) { t = gt; goto offset_found; @@ -492,7 +505,7 @@ __mktime_internal (struct tm *tp, /* No unusual DST offset was found nearby. Assume one-hour DST. */ t += 60 * 60 * dst_difference; - if (mktime_min <= t && t <= mktime_max && convert_time (convert, t, &tm)) + if (mktime_min <= t && t <= mktime_max && __tz_convert (t, local, &tm)) goto offset_found; __set_errno (EOVERFLOW); @@ -503,8 +516,8 @@ __mktime_internal (struct tm *tp, /* Set *OFFSET to the low-order bits of T - T0 - NEGATIVE_OFFSET_GUESS. This is just a heuristic to speed up the next mktime call, and correctness is unaffected if integer overflow occurs here. */ - INT_SUBTRACT_WRAPV (t, t0, offset); - INT_SUBTRACT_WRAPV (*offset, negative_offset_guess, offset); + ckd_sub (offset, t, t0); + ckd_sub (offset, *offset, negative_offset_guess); if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec) { @@ -513,13 +526,13 @@ __mktime_internal (struct tm *tp, long_int sec_adjustment = sec == 0 && tm.tm_sec == 60; sec_adjustment -= sec; sec_adjustment += sec_requested; - if (INT_ADD_WRAPV (t, sec_adjustment, &t) + if (ckd_add (&t, t, sec_adjustment) || ! (mktime_min <= t && t <= mktime_max)) { __set_errno (EOVERFLOW); return -1; } - if (! convert_time (convert, t, &tm)) + if (! __tz_convert (t, local, &tm)) return -1; } @@ -535,14 +548,13 @@ __mktime_internal (struct tm *tp, __time64_t __mktime64 (struct tm *tp) { - /* POSIX.1 8.1.1 requires that whenever mktime() is called, the - time zone abbreviations contained in the external variable 'tzname' shall - be set as if the tzset() function had been called. */ + /* POSIX.1 requires mktime to set external variables like 'tzname' + as though tzset had been called. */ __tzset (); # if defined _LIBC || NEED_MKTIME_WORKING static mktime_offset_t localtime_offset; - return __mktime_internal (tp, __localtime64_r, &localtime_offset); + return __mktime_internal (tp, true, &localtime_offset); # else # undef mktime return mktime (tp); diff --git a/time/timegm.c b/time/timegm.c index 5e5fa0127f..4c2615b9fa 100644 --- a/time/timegm.c +++ b/time/timegm.c @@ -30,8 +30,7 @@ __time64_t __timegm64 (struct tm *tmp) { static mktime_offset_t gmtime_offset; - tmp->tm_isdst = 0; - return __mktime_internal (tmp, __gmtime64_r, &gmtime_offset); + return __mktime_internal (tmp, false, &gmtime_offset); } #if defined _LIBC && __TIMESIZE != 64 From patchwork Sun Jan 5 05:56:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104045 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A57D3858C54 for ; Sun, 5 Jan 2025 06:07:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A57D3858C54 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=EMX0fECW X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 755FB3858D39 for ; Sun, 5 Jan 2025 05:58:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 755FB3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 755FB3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056712; cv=none; b=IzWMhzZpNha1P3R0G0OcCAM4gqbIMbERtpfrSkXE2enhtplQLrEBATMODgKHpc0z07CFC9QeYWDEnITsNRBzMPmo3dvdyxnEsMJS33ELRQOMdYAmx1N2VIyS3lagq8gwFlBJeJyGuymTkqmX01XZatFMOoRXa/rUdxGm0qLUPcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056712; c=relaxed/simple; bh=wvZRt6TsI622ZyJO7WmVw7NXXL4mMdV+K+lHY9OWkyE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=grQOCRbUDH3FJJj9dj1XReZI5peJyZqkxGkD1fBXoN9ZKfyLOv9sy8IVSiPzFbwRTz6u/xBWVxF4rU/Va0hlp/4UB8q9NDplaEXZTwTlFByiHA2IDDzFkbbfeYRKatS5YTaRdvxsN9B9tGPAj6Q5vHUxCujdjw+GTzuXv+tqhgU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 755FB3858D39 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id E9A413C123847 for ; Sat, 4 Jan 2025 21:58:31 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id vLxTzl8Pu5AI; Sat, 4 Jan 2025 21:58:31 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 888E03C123845; Sat, 4 Jan 2025 21:58:31 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 888E03C123845 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056711; bh=NC6Mt4CO/lxZ+UBmHn9eu/zbrYYzBVSTrk+eMP8GHSs=; h=From:To:Date:Message-ID:MIME-Version; b=EMX0fECWx3P5xqBqaI0dc36AE/4XoXiV2tt0a1svY3PiPwti8HPxKdGprJjAHE8TZ Upj1PFL1msE78opyJ4MVBkvyUMo9i39HJAM0XS4HFgD0MSbsNMtWkrjdiu6Nr6V0hQ wNetOqu0d0Wzc7NMCRYx3Wn5/ToAnWJA4SM0lD6/T0XzJE4lWs9fWJ2Sf6HVocdiwz z00gnYE2jys0715KI5AVz6CsG6cGz48s8hy/D+b8kIhqpJchIQh8pfz5vLzPTjZ5fn 6pQcLDhQSQkiJ8Mu7bT19icxbObP5dOR1nosVFKlnTwzJr2NtE65A8M+eQgKRyjFVq rV+wnabtpT/7g== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id tcaQxtQfBcyI; Sat, 4 Jan 2025 21:58:31 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 720AF3C082EB9; Sat, 4 Jan 2025 21:58:31 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 07/59] Document mktime out-of-range + tm_isdst Date: Sat, 4 Jan 2025 21:56:42 -0800 Message-ID: <20250105055750.1668721-8-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * manual/time.texi (Broken-down Time): Document mktime better when dealing with out-of-range inputs and nonnegative tm_isdst. Documentation problem reported by Florian Weimer in: https://sourceware.org/pipermail/libc-alpha/2024-October/160310.html --- manual/time.texi | 51 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/manual/time.texi b/manual/time.texi index 899128ca7f..28d8912446 100644 --- a/manual/time.texi +++ b/manual/time.texi @@ -1075,9 +1075,11 @@ This is a flag that indicates whether daylight saving time is (or was, or will be) in effect at the time described. The value is positive if daylight saving time is in effect, zero if it is not, and negative if the information is not available. -Although this flag is useful when passing a broken-down time to the -@code{mktime} function, for other uses this flag should be ignored and -the @code{tm_gmtoff} and @code{tm_zone} fields should be inspected instead. +This flag should not be inspected; the @code{tm_gmtoff} and +@code{tm_zone} fields should be inspected instead. +However, before calling @code{mktime} this flag should be set, +and set to a negative value unless the application already has a +correct value for the specified time. @item long int tm_gmtoff This field describes the time zone that was used to compute this @@ -1297,13 +1299,46 @@ The @code{mktime} function ignores the specified contents of the members of the broken-down time structure. It uses the values of the other components to determine the calendar time; it's permissible for these components to have -unnormalized values outside their normal ranges. The last thing that -@code{mktime} does is adjust the components of the @var{brokentime} +unnormalized values outside their normal ranges. +Out-of-range members are brought into range in reverse order of +significance: for example, an hour equal to @minus{}1 is first +increased to 23 and the day decreased by 1, before an out-of-range day +is brought into range. + +If local time ever observes daylight saving time, the @code{tm_isdst} +member is inspected after other members are brought into range. If +negative, @code{mktime} uses time zone data to determine whether +daylight saving time is in effect at the requested time. Then members +are adjusted if they would otherwise be inconsistent, e.g., by adding +or subtracting an hour if the broken-down time would otherwise have +the wrong @code{tm_isdst} member or would occur during a +spring-forward gap. +The heuristic used for these adjustments is unspecified. +@c The heuristic conforms to POSIX.1-2024 for non-geographic timezones. +@c POSIX-1.2024 is incorrect (and unclear to boot) for geographic timezones, +@c which is the normal use cases for non-UTC timezones these days. + +Ordinarily callers should specify a negative @code{tm_isdst} member. +The main practical use for nonnegative @code{tm_isdst} is when a the +broken-down time was produced by @code{localtime} or similar functions +so its @code{tm_isdst} is already known to be correct. This might +happen if the original simple time has been lost, in which case the +nonnegative @code{tm_isdst} usually (but not always) lets +@code{mktime} disambiguate broken-down times within an overlapping +window after the clock jumps back during a daylight saving or other +transition. A nonnegative @code{tm_isdst} might also happen when +doing broken-down time arithmetic by calling @code{localtime}, adding +or subtracting to (say) @code{tm_mday}, and then calling +@code{mktime}; in this case, though, if the time arithmetic crosses a +time transition boundary the results are unspecified. + +If successful, @code{mktime} uses the equivalent of @code{localtime} +to set all the components of the @code{*@var{brokentime}} structure, including the members that were initially ignored. If the specified broken-down time cannot be represented as a simple time, @code{mktime} returns a value of @code{(time_t)(-1)} and does not modify -the contents of @var{brokentime}. +@code{*@var{brokentime}}. Calling @code{mktime} also sets the time zone state as if @code{tzset} were called; @code{mktime} uses this information instead @@ -1342,8 +1377,8 @@ available. @code{timelocal} is rather rare. @c tzfile_compute(!use_localtime) ok @code{timegm} is functionally identical to @code{mktime} except it -always takes the input values to be UTC -regardless of any local time zone setting. +operates in UTC, so it ignores @code{tm_isdst} other than setting it +to zero when successful. Note that @code{timegm} is the inverse of @code{gmtime}. From patchwork Sun Jan 5 05:56:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104057 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17C113858D3C for ; Sun, 5 Jan 2025 06:16:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17C113858D3C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=p/AcsP+a X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 56BEE3858C33 for ; Sun, 5 Jan 2025 05:58:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 56BEE3858C33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 56BEE3858C33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056715; cv=none; b=u5/3YvvyucClOIg0k+R7Z5eXs9eJ0YOLPjq4Tj+A3GCJFnOh3WxMEEN/fHBgK5z40WcZZ2gCoUytrGFHjgwf2EyhIuXGeQ7ksxBqomaAoCXj64FcpBMf2gVtUgw/Y4Y3O8Nf3wr+hhyVAJxobPLNIPqWWxSN7yHdj0z53jbqKhk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056715; c=relaxed/simple; bh=TFjb58z0V5b53b1xH/koMhLxxk/BaHni/kdAmUSn6N0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kWL2ajJZZPmDm8HB1QwygaGtPSkCG1zZdjL9p0mALLqnOSrc10ZBCAZnDwibaXMTH60q1d8eDcLG8efBNu6i4N1wvAu4F2XuVQsXDgl5xXdcdXzljPXHp7mKAJ/8GRBk6QBv5gLwYsWEq6+s5xA7T2Wbdrcvpo+6WNtvbcxdSAk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 56BEE3858C33 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D054E3C123844; Sat, 4 Jan 2025 21:58:34 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id IAKAW6f643Oq; Sat, 4 Jan 2025 21:58:34 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 2FD4C3C123843; Sat, 4 Jan 2025 21:58:34 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 2FD4C3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056714; bh=NPq6PQxAactlylm2XUrm/EE4hJl5qaf1vnrT/B69E/Q=; h=From:To:Date:Message-ID:MIME-Version; b=p/AcsP+aKesx7K2PMliwUWctG4TchHAtlC5wSw4eSzb0GBfIBnTXfegQ4wH14QJek VgmU8JUpV2aKLn0qVMktx4B172IYF3xfSHojidCU0PKi5CEjjyF6WOdLem/iO6q1gS LWGqiIRwoAnACT2a2EnKijtvGkxBKH5UiJ9qH1XRZgDyzwEF+EUa9261X0F5tyB37k RLPhlUtcCUWAqEBG8Kbb9d0MpO/lUH54DmTd7zn12h1BCpDiR+M3VTMxegs9yrBVZd +25H6GnU3+qOpqZMXvrv913V2Ck4WGeCMSb2tB2U8kgPMeeOWNZ3PVuH6/JTl0hwlA AgDW1B+SdC4aA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id r7YVc56Ut8W0; Sat, 4 Jan 2025 21:58:34 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 1502F3C082EB9; Sat, 4 Jan 2025 21:58:34 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert , Florian Weimer Subject: [PATCH 08/59] Push tzset lock into callers of time functions Date: Sat, 4 Jan 2025 21:56:43 -0800 Message-ID: <20250105055750.1668721-9-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This makes sure no other thread changes the time zone data while mktime is running, which could confuse its search probes. Likewise for timegm, though admittedly this is overkill unless leap seconds are being used and the leap seconds table changes (!). Although this means mktime and timegm will be more of a bottleneck for other threads, mktime and timegm are called so rarely that let's hope it's not a significant performance issue. This patch also fixes a harmless data race on the cached offset used by __mktime_internal. Co-authored-by: Florian Weimer * include/time.h: Move tzset-related declarations out of here ... * time/tzset.h: ... and into this new file. All .c files that use these declarations changed to include the new file. * time/gmtime.c (__gmtime64_r, __gmtime64): * time/localtime.c (__localtime64_r, __localtime64): * time/mktime.c (__mktime64): * time/timegm.c (__timegm64): Lock __tzset_lock while calling __tz_convert. * time/gmtime.c (__timegm64): Invoke __tzset_unlocked here; it is needed for leap seconds. * time/mktime-internal.h (__libc_lock_lock, __libc_lock_unlock) (__tzset_unlocked) [!_LIBC]: New no-op macros. * time/mktime.c (convert_time, __tz_convert) [_LIBC]: Remove; now done by glibc/time/tzset.c's __tz_convert. (__mktime_internal) [!_LIBC]: Double the number of probes. --- include/time.h | 19 ------------------- time/gmtime.c | 8 ++++++-- time/localtime.c | 8 ++++++-- time/mktime-internal.h | 7 ++++++- time/mktime.c | 23 +++++++++++++++-------- time/strftime_l.c | 1 + time/timegm.c | 9 ++++++++- time/tzfile.c | 1 + time/tzset.c | 21 +++++++++++---------- time/tzset.h | 30 ++++++++++++++++++++++++++++++ 10 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 time/tzset.h diff --git a/include/time.h b/include/time.h index 52bb22abd9..e9c083646a 100644 --- a/include/time.h +++ b/include/time.h @@ -49,20 +49,6 @@ extern const unsigned short int __mon_yday[2][13] attribute_hidden; /* Defined in localtime.c. */ extern struct tm _tmbuf attribute_hidden; -/* Defined in tzset.c. */ -extern char *__tzstring (const char *string) attribute_hidden; - -extern int __use_tzfile attribute_hidden; - -extern void __tzfile_read (const char *file) attribute_hidden; -extern void __tzfile_compute (__time64_t timer, int use_localtime, - long int *leap_correct, int *leap_hit, - struct tm *tp) attribute_hidden; -extern void __tzset_parse_tz (const char *tz) attribute_hidden; -extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) - __THROW attribute_hidden; - - #if __TIMESIZE == 64 # define __itimerspec64 itimerspec #else @@ -271,11 +257,6 @@ extern int __offtime (__time64_t __timer, extern char *__asctime_r (const struct tm *__tp, char *__buf) attribute_hidden; -extern void __tzset (void) attribute_hidden; - -/* Prototype for the internal function to get information based on TZ. */ -extern struct tm *__tz_convert (__time64_t timer, int use_localtime, - struct tm *tp) attribute_hidden; extern int __nanosleep (const struct timespec *__requested_time, struct timespec *__remaining); diff --git a/time/gmtime.c b/time/gmtime.c index ad6e8eb352..9d0af33401 100644 --- a/time/gmtime.c +++ b/time/gmtime.c @@ -17,13 +17,17 @@ . */ #include +#include /* Return the `struct tm' representation of *T in UTC, using *TP to store the result. */ struct tm * __gmtime64_r (const __time64_t *t, struct tm *tp) { - return __tz_convert (*t, 0, tp); + __libc_lock_lock (__tzset_lock); + struct tm *result = __tz_convert (*t, 0, tp); + __libc_lock_unlock (__tzset_lock); + return result; } /* Provide a 32-bit variant if needed. */ @@ -48,7 +52,7 @@ weak_alias (__gmtime_r, gmtime_r) struct tm * __gmtime64 (const __time64_t *t) { - return __tz_convert (*t, 0, &_tmbuf); + return __gmtime64_r (t, &_tmbuf); } /* Provide a 32-bit variant if needed. */ diff --git a/time/localtime.c b/time/localtime.c index d1967ff11b..7f865a7053 100644 --- a/time/localtime.c +++ b/time/localtime.c @@ -17,6 +17,7 @@ . */ #include +#include /* C89 says that localtime and gmtime return the same pointer. Although C99 and later relax this to let localtime and gmtime @@ -30,7 +31,10 @@ struct tm _tmbuf; struct tm * __localtime64_r (const __time64_t *t, struct tm *tp) { - return __tz_convert (*t, 1, tp); + __libc_lock_lock (__tzset_lock); + struct tm *result = __tz_convert (*t, 1, tp); + __libc_lock_unlock (__tzset_lock); + return result; } /* Provide a 32-bit variant if needed. */ @@ -54,7 +58,7 @@ weak_alias (__localtime_r, localtime_r) struct tm * __localtime64 (const __time64_t *t) { - return __tz_convert (*t, 1, &_tmbuf); + return __localtime64_r (t, &_tmbuf); } libc_hidden_def (__localtime64) diff --git a/time/mktime-internal.h b/time/mktime-internal.h index 1da98b4373..215be914c2 100644 --- a/time/mktime-internal.h +++ b/time/mktime-internal.h @@ -19,6 +19,9 @@ #ifndef _LIBC # include +# define __libc_lock_lock(lock) ((void) 0) +# define __libc_lock_unlock(lock) ((void) 0) +# define __tzset_unlocked() tzset () #endif /* mktime_offset_t is a signed type wide enough to hold a UTC offset @@ -73,6 +76,8 @@ typedef int mktime_offset_t; /* Subroutine of mktime. Return the time_t representation of TP and normalize TP, given that a struct tm * maps to a time_t. If LOCAL, the mapping is performed by localtime_r, otherwise by gmtime_r. - Record next guess for localtime-gmtime offset in *OFFSET. */ + Record next guess for localtime-gmtime offset in *OFFSET. + + If _LIBC, the caller must lock __tzset_lock. */ extern __time64_t __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) attribute_hidden; diff --git a/time/mktime.c b/time/mktime.c index 7f7d33492b..3a951b2c8c 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -62,6 +62,9 @@ # define NEED_MKTIME_WORKING 0 #endif +#ifdef _LIBC +# include +#endif #include "mktime-internal.h" #if !defined _LIBC && (NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS) @@ -250,6 +253,7 @@ tm_diff (long_int year, long_int yday, int hour, int min, int sec, tp->tm_hour, tp->tm_min, tp->tm_sec); } +#ifndef _LIBC /* Convert T to a struct tm value in *TM. Use localtime64_r if LOCAL, otherwise gmtime64_r. T must be in range for __time64_t. Return TM if successful, NULL (setting errno) on failure. */ @@ -262,8 +266,8 @@ convert_time (long_int t, bool local, struct tm *tm) else return __gmtime64_r (&x, tm); } -/* Call it __tzconvert to sync with other parts of glibc. */ -#define __tz_convert convert_time +# define __tz_convert convert_time +#endif /* Convert *T to a broken down time in *TP (as if by localtime if LOCAL, otherwise as if by gmtime). If *T is out of range for @@ -320,7 +324,9 @@ ranged_convert (bool local, long_int *t, struct tm *tp) If *OFFSET's guess is correct, only one reverse mapping call is needed. If successful, set *TP to the canonicalized struct tm; otherwise leave *TP alone, return ((time_t) -1) and set errno. - This function is external because it is used also by timegm.c. */ + This function is external because it is used also by timegm.c. + + If _LIBC, the caller must lock __tzset_lock. */ __time64_t __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) { @@ -548,14 +554,15 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) __time64_t __mktime64 (struct tm *tp) { - /* POSIX.1 requires mktime to set external variables like 'tzname' - as though tzset had been called. */ - __tzset (); - # if defined _LIBC || NEED_MKTIME_WORKING + __libc_lock_lock (__tzset_lock); + __tzset_unlocked (); static mktime_offset_t localtime_offset; - return __mktime_internal (tp, true, &localtime_offset); + __time64_t result = __mktime_internal (tp, true, &localtime_offset); + __libc_lock_unlock (__tzset_lock); + return result; # else + __tzset (); # undef mktime return mktime (tp); # endif diff --git a/time/strftime_l.c b/time/strftime_l.c index f51d926b46..584df83d15 100644 --- a/time/strftime_l.c +++ b/time/strftime_l.c @@ -33,6 +33,7 @@ # define MULTIBYTE_IS_FORMAT_SAFE 1 # define STDC_HEADERS 1 # include "../locale/localeinfo.h" +# include #endif #if defined emacs && !defined HAVE_BCOPY diff --git a/time/timegm.c b/time/timegm.c index 4c2615b9fa..42115a5d7f 100644 --- a/time/timegm.c +++ b/time/timegm.c @@ -24,13 +24,20 @@ #include #include +#ifdef _LIBC +# include +#endif #include "mktime-internal.h" __time64_t __timegm64 (struct tm *tmp) { + __libc_lock_lock (__tzset_lock); + __tzset_unlocked (); static mktime_offset_t gmtime_offset; - return __mktime_internal (tmp, false, &gmtime_offset); + __time64_t result = __mktime_internal (tmp, false, &gmtime_offset); + __libc_lock_unlock (__tzset_lock); + return result; } #if defined _LIBC && __TIMESIZE != 64 diff --git a/time/tzfile.c b/time/tzfile.c index bca8b3f4ef..fa2be56580 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -27,6 +27,7 @@ #include #include #include +#include #include diff --git a/time/tzset.c b/time/tzset.c index 1a9a03a436..a0bf3382ba 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -16,13 +16,13 @@ . */ #include -#include #include #include #include #include #include #include +#include #include @@ -37,7 +37,7 @@ weak_alias (__daylight, daylight) weak_alias (__timezone, timezone) /* This locks all the state variables in tzfile.c and this file. */ -__libc_lock_define_initialized (static, tzset_lock) +__libc_lock_define_initialized (, __tzset_lock) /* This structure contains all the information about a timezone given in the POSIX standard TZ envariable. */ @@ -529,10 +529,8 @@ __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) #undef tzset void -__tzset (void) +__tzset_unlocked (void) { - __libc_lock_lock (tzset_lock); - tzset_internal (1); if (!__use_tzfile) @@ -541,9 +539,16 @@ __tzset (void) __tzname[0] = (char *) tz_rules[0].name; __tzname[1] = (char *) tz_rules[1].name; } +} - __libc_lock_unlock (tzset_lock); +void +__tzset (void) +{ + __libc_lock_lock (__tzset_lock); + __tzset_unlocked (); + __libc_lock_unlock (__tzset_lock); } + weak_alias (__tzset, tzset) /* Return the `struct tm' representation of TIMER in the local timezone. @@ -554,8 +559,6 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) long int leap_correction; int leap_extra_secs; - __libc_lock_lock (tzset_lock); - /* Update internal database according to current TZ setting. POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. This is a good idea since this allows at least a bit more parallelism. */ @@ -574,8 +577,6 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) leap_extra_secs = 0; } - __libc_lock_unlock (tzset_lock); - if (tp) { if (! use_localtime) diff --git a/time/tzset.h b/time/tzset.h new file mode 100644 index 0000000000..fb7984563e --- /dev/null +++ b/time/tzset.h @@ -0,0 +1,30 @@ +#ifndef _TZSET_H +#define _TZSET_H + +#include +#include + +/* Defined in tzset.c. */ +extern char *__tzstring (const char *string) attribute_hidden; + +extern int __use_tzfile attribute_hidden; + +extern void __tzfile_read (const char *file) attribute_hidden; +extern void __tzfile_compute (__time64_t timer, int use_localtime, + long int *leap_correct, int *leap_hit, + struct tm *tp) attribute_hidden; +extern void __tzset_parse_tz (const char *tz) attribute_hidden; +extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) + __THROW attribute_hidden; + +__libc_lock_define (extern, __tzset_lock attribute_hidden); + +extern void __tzset (void) attribute_hidden; +extern void __tzset_unlocked (void) attribute_hidden; + +/* Prototype for the internal function to get information based on TZ. + Caller needs to lock __tzset_lock. */ +extern struct tm *__tz_convert (__time64_t timer, int use_localtime, + struct tm *tp) attribute_hidden; + +#endif /* _TZSET_H */ From patchwork Sun Jan 5 05:56:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104059 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8FE9B3858428 for ; Sun, 5 Jan 2025 06:18:15 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 0175D3858D28 for ; Sun, 5 Jan 2025 05:58:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0175D3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0175D3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056717; cv=none; b=AGplpmoe4aM0/tqIyJKolcq60YBLszXsz6AESmcfgrHN2H/wbchZqHB6Y3qA2XOdraNy4qhgd2zGLVDAhjBKWds4yqwot1S6k7ZNB8zr5iZ7xfRHNKAs9llxKnLcqSrtp3OZy9M4NYjX/2H0Fyxl0WykIaV0pWsOXpIGPzMJ7so= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056717; c=relaxed/simple; bh=V228vBrTRbTqI31FXdb5HsVr0Fda+Z2Q0NY7amKxq8I=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=B7kdjeR22GuFFKYQhQ+OLlEEzjFf1JcpAv/GgicRUN/09T7yh8CH3TtcRoykr2c6rjDRIUn5VuWt/RP+cj1mTU03hyEu4G/X6wKwy2ZsPn+/eIq3XhccdV5YRUGhwK9QyvvDY4GZqjfg/ePsLyfnb3U03M8qA0kbAHaaKQXWdLU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0175D3858D28 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=kyzYjPFa Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 7B8413C123847 for ; Sat, 4 Jan 2025 21:58:36 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 6Y-M7b7RNJCI; Sat, 4 Jan 2025 21:58:36 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 416323C123843; Sat, 4 Jan 2025 21:58:36 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 416323C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056716; bh=EN+7VMth7zviVr14BQBMv7FtO1/JfHoshn+78tWgDNM=; h=From:To:Date:Message-ID:MIME-Version; b=kyzYjPFa2rkMc1bh/s/+BaoEel4lpqyzgbrmM0Gjf62AzPNDzs6BYEXB3BkdnnlNN FI5rjGEQ7m/n+ykRvWVOK8ZjcSyCfOniZTuq/OWJXkXhrvIV/uVu2IHyjzly03TJZ4 1ngJCiRORZif6ppNxw7A7429bndKR+QMWgmwaHxxiIilDu3lWljPxI7ujEX7SKuvef 9+YLtd2bK3S/WvhFD25muLgk9CDk9bBmOjw0enCFDB+Ogbm8rgEFS4Rl1h27KmWP18 ZQt11I2xZuGogg0+m2fpj2/f5zdl75vuhBoKkEzapVHgV1kLrh0d8ROOA8qk6LOlLb A19YBPHcuboPw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 7S7ZYyN4w7o2; Sat, 4 Jan 2025 21:58:36 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 20D8B3C082EB9; Sat, 4 Jan 2025 21:58:36 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 09/59] Omit parse_offset unnecessary *tz == '\0' Date: Sat, 4 Jan 2025 21:56:44 -0800 Message-ID: <20250105055750.1668721-10-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (parse_offset): Simplify by omitting unnecessary (*tz == '\0') test. --- time/tzset.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/time/tzset.c b/time/tzset.c index a0bf3382ba..9fb3a127f7 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -189,12 +189,13 @@ static bool parse_offset (const char **tzp, int whichrule) { const char *tz = *tzp; + bool leading_sign = *tz == '-' || *tz == '+'; if (whichrule == 0 - && (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))) + && (!leading_sign && !isdigit (*tz))) return false; int sign; - if (*tz == '-' || *tz == '+') + if (leading_sign) sign = *tz++ == '-' ? 1 : -1; else sign = -1; From patchwork Sun Jan 5 05:56:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104061 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 69F22385842A for ; Sun, 5 Jan 2025 06:18:50 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id DEAF33858C35 for ; Sun, 5 Jan 2025 05:58:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DEAF33858C35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DEAF33858C35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056720; cv=none; b=AAq+OiDlWAMlEQB58gwDA8+gG5ash8XtcUBKVvV3z+754vWWIp6+P56m7aphdcXVK+WMQDH0wYfWUkarrD9IuxW77Hqu605+EWB6sFoqlGYVAsYJet9g0oUSQw5q1VyIXIdnFZf3UPAn6U42H7SrS2JJlYegCUgJ8Ae/p/1KdLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056720; c=relaxed/simple; bh=qyXPQY2ydHTX7WEisr4OgDLKGWBhlmKlRvW9GLMImTI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=BwMrBrnapcGQkN5P+D2Kn+cwodnId7yLwLdC8c8qwdpHtH74CQFkziL4AFqcNdmvotyeM3UE1SwqQ9g9cY5cClUDyYQuqpfFGkIqy8MpzNtKeBOT+qIzoVkRhVfoWxwFzEUjJ3mJP36qO+USBS+3aUfpWvPf+YNHQf8IVSdHDxs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DEAF33858C35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=WVkCpjc/ Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 649AE3C123844 for ; Sat, 4 Jan 2025 21:58:39 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id omkUOMn99J41; Sat, 4 Jan 2025 21:58:39 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 26D0D3C123843; Sat, 4 Jan 2025 21:58:39 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 26D0D3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056719; bh=iDqI87RSUB6yqos9n7pLwrroI9zORu7NPzj+DvmiuIk=; h=From:To:Date:Message-ID:MIME-Version; b=WVkCpjc/kcZu90Ct6Qu/MOIqfB5xW7rVoxuupYJXIPkouXdymzMUG68CkkzBoiz0Q Gj5nGH30P1udOD2NH2mTnosVlwKWQLP+jWveS+SNRBg22Kx6+3Zm3Igl0HB1+g1pGH Gul80cD1cXxYsdn9gYzoYqqjhmtuFwq2AKDLi8C0pxQW+IH+Sm+7Qm+mBIH+2TmGJY E+nbVul0ORVME4nVNJuDxstUgGBd4gbuCLl8BFG6e9kFKzcYjWuQVCVNS6InDnPU6t DEeLMeUG5v0QCkwdhqIHJoOpJxXzhDShA/wJzfxPTcAry8tgCn2oMuhAQJ1S0+b29C 00LrlkaRwDhXQ== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id vhdwdfKQBi8J; Sat, 4 Jan 2025 21:58:39 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 065EF3C082EB9; Sat, 4 Jan 2025 21:58:39 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 10/59] =?utf-8?q?Don=E2=80=99t_assume_TZif_bloat_is_likely?= Date: Sat, 4 Jan 2025 21:56:45 -0800 Message-ID: <20250105055750.1668721-11-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): It’s no longer unlikely that timer >= transitions[num_transitions - 1], now that ‘zic -b slim’ is commonly the default. --- time/tzfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time/tzfile.c b/time/tzfile.c index fa2be56580..9d5f428f47 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -527,7 +527,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, ++j; } } - else if (__glibc_unlikely (timer >= transitions[num_transitions - 1])) + else if (timer >= transitions[num_transitions - 1]) { if (__glibc_unlikely (tzspec == NULL)) { From patchwork Sun Jan 5 05:56:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104040 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 145263858C56 for ; Sun, 5 Jan 2025 06:03:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 145263858C56 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=rObAA0uj X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 438863858CDA for ; Sun, 5 Jan 2025 05:58:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 438863858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 438863858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056722; cv=none; b=GAntke9kzYhQ3BDxsq5aJxCT90pFbI8r1+ZUjQUV06FOW/W/obn/h54sdx8l0Y4cMUc/4L+rGFryVlC3t8MA3PVlEFRtrZ3Lrb5qpXawtOvUJetjoM7XZjoRAdYhxppgIIRAK7Ea4O1T5/vY3om9t9mslDetcZdSOYoHhIvs5Sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056722; c=relaxed/simple; bh=WqaK9CXZnOzdjH1OJ0EqCDb7Ml2EWHTOHwNAzF7S2hc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=sh+xiTfa+KjyPHtspF0ygRoVECLgxL0X4xxoQEmcOH9cRLbwijXh7Ap4EfMIqHgkX8jtjY0U74UopVDSJO/EXpREio0DNyG9XamELwOemaVZJCHH6YON+EVPro4rf8VzImMB+LpatrMWD9kmpsm1M7dvKuR/H3ZDjUbH7eJgVTI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 438863858CDA Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id BD4E53C123845 for ; Sat, 4 Jan 2025 21:58:41 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 6Ht1cF5ih7PU; Sat, 4 Jan 2025 21:58:41 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 7DC8C3C123843; Sat, 4 Jan 2025 21:58:41 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 7DC8C3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056721; bh=8n1iMLK+w8z+peljK47e7gzRU45N4i/h/eH+CD72eC8=; h=From:To:Date:Message-ID:MIME-Version; b=rObAA0ujw4cREIJv3/+TTlOLYKYQwBcELepSqQHOuj/szhTbN7bE2p176q0uCBBTR 9yOpRFUq7TlR06XRcMxmzc6H9D/K3Svt4IIOTZVDLg+OKKkxjrsENIp9lhKdZvLLgC hLNkRr64Jf7uAijqE2iv+Ge6TtXEnc6jhUz/BHT+az/6sjP8mOXxUN7SimKJ0rMGIN MOSsSZo/SXZLCR0vXTx38/7lZ1cQy9PYnC2fHCbKMZKhinlvL5qdd1WGvF4hjAvRNs wW8l531Oqk+rxQypgJg4knoyg/X+Qm56tU9E8wLAtglEPKopHtaG0DsYX+MenUzujU Thp/5ouOWvbvw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id qL6tQ4KrclXA; Sat, 4 Jan 2025 21:58:41 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 670563C082EB9; Sat, 4 Jan 2025 21:58:41 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 11/59] tzset no longer calls ftello Date: Sat, 4 Jan 2025 21:56:46 -0800 Message-ID: <20250105055750.1668721-12-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): Compute file offset directly rather than invoking ftello. --- time/tzfile.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/time/tzfile.c b/time/tzfile.c index 9d5f428f47..15d1890892 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -168,6 +168,7 @@ __tzfile_read (const char *file) f = fopen (file, "rce"); if (f == NULL) goto ret_free_transitions; + off_t f_offset = 0; /* Get information about the file we are actually using. */ if (__fstat64_time64 (__fileno (f), &st) != 0) @@ -190,6 +191,7 @@ __tzfile_read (const char *file) || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0) goto lose; + f_offset += sizeof tzhead; num_transitions = (size_t) decode (tzhead.tzh_timecnt); num_types = (size_t) decode (tzhead.tzh_typecnt); chars = (size_t) decode (tzhead.tzh_charcnt); @@ -215,6 +217,7 @@ __tzfile_read (const char *file) if (fseek (f, to_skip, SEEK_CUR) != 0) goto lose; + f_offset += to_skip; goto read_again; } @@ -223,7 +226,7 @@ __tzfile_read (const char *file) size_t tzspec_len; if (trans_width == 8) { - off_t rem = st.st_size - __ftello (f); + off_t rem = st.st_size - f_offset; if (__builtin_expect (rem < 0 || (size_t) rem < (num_transitions * (8 + 1) + num_types * 6 From patchwork Sun Jan 5 05:56:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104049 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 91DD43858408 for ; Sun, 5 Jan 2025 06:10:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 91DD43858408 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=kCTMUDBx X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id AA4493858C3A for ; Sun, 5 Jan 2025 05:58:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA4493858C3A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AA4493858C3A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056725; cv=none; b=hWGnLZlaQNzs3IW2g1YbbTkPbcYpg4PRVqb7tolF4wmKJ6ReVQlgmkbuhmXTl8UKdmlQ/6lcWynOJjK5Y7fo3+i4na7hIby1iIJEtIvmiPmEWq93D9IW3P4nFbwzWsnr0esh9rClrxaPMYYd02Lyi4wV/Mdx865AJxUXMzVRYZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056725; c=relaxed/simple; bh=L2u+DbJ2d5mSmHtuUknzR+k3VYHDFoVGKPKRaCDLJ3A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wSTkBHu/mCF2sRIpz0z1JZU8kK4VKj9aaHzZ8r08kaK7wn44R5iiHuBuSxJpIz09VKMp05F6kJE7ht/kD+q6t7n2xjvH7SmVzl0cvLiinUUQ6acMZOcI2rUJLjcoek0vc2bHqu8t9iqDgiZ85rBGHnWuhAjQuvd0/SOk63P251Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA4493858C3A Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 301613C123844 for ; Sat, 4 Jan 2025 21:58:45 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id DUT6-MU-U1EQ; Sat, 4 Jan 2025 21:58:44 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 387703C123843; Sat, 4 Jan 2025 21:58:44 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 387703C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056724; bh=XjfdV507ELBykiT+FBme6P7euO2SusWywikmV2CcKm8=; h=From:To:Date:Message-ID:MIME-Version; b=kCTMUDBxeiNFxXtJ56VKbNqFu40Jnmo/GP4v7TU+eTeQ+Tsr+c/3lngrX0ZYK2QCe UD4gJnJQ5zpk105N0oiaZQCdL1920aYmUvSLwc7kQKi5TQTMfan/Lo4MzvGy2ZR2cD SOf/u8/IXMvmTYAJbOoVJsCll4D8TdmgOOJk+H5kgZDQ5bd8ju0LL43S+3G3SXMCPU vSKOk+qNTxH7W3xpE0WTuyHTcPk4rzE7jUIeksUq+xwwx69h2dgmBa4SR99PLAgwIe kJEqBDCOYjyyau2JayMbM4vsAW1zwWb0oLn1mOQjbJzt//C7WhzP4VRRQikR8H4JQa Z78UfUOxrtbDA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id rxVmQ4eso5lo; Sat, 4 Jan 2025 21:58:44 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 1B9D23C082EB9; Sat, 4 Jan 2025 21:58:44 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 12/59] Check TZif files more carefully Date: Sat, 4 Jan 2025 21:56:47 -0800 Message-ID: <20250105055750.1668721-13-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This tests the contents of TZif files more carefully for conformance to RFC 9636, in areas where non-conformance might plausibly mess up glibc or application behavior. It also removes some nonportable assumptions about alignment and arithmetic. * time/tzfile.c: Include stdalign.h, stdckdint.h. (tzidx): New type. Prefer it to size_t for TZif-related counts that must fit in 32-bit unsigned integer. This is mostly for clarity. (tzidx_decode): New function, for obtaining tzidx values. All uses of decode changed, when tzidx values are wanted. This fixes some potential misuse of values with the top bit set. (decode): Reimplement using tzidx_decode. (decode64): Don’t assume sizeof (int64_t) == 8; admittedly this is theoretical. Simplify the usual case. (__tzfile_read): Use unions to guarantee proper alignment. Check that there is at least one time type, since we always use at least the zeroth entry. Check for overflow in size and offset computations. Check that the TZif's file TZ string is either empty or at least 4 bytes long (not counting newlines). Avoid aliasing issues when unpacking transitions. Fix off-by-one bug when checking for bogus index. Check that all time zone abbreviations are null-terminated. Check that leap seconds are in ascending order, that they are all either insertions or deletions of a single second, and that they are at least 2419199 seconds apart. (__tzfile_default): Defend against negating INT_MIN. (__tzfile_compute): Args are now int * and bool *, not long int * and int *. All uses changed. Defend against unlikely integer overflows leading to misbehavior. Do not worry about adjacent leap seconds, as it cannot happen even though a bug in the original C standard said it could; this is why we can now use bool * not int * for leap hits. --- time/tzfile.c | 369 ++++++++++++++++++++++++++++++-------------------- time/tzset.c | 21 ++- time/tzset.h | 3 +- 3 files changed, 241 insertions(+), 152 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 15d1890892..e60c3c0b9c 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -17,6 +17,8 @@ #include #include +#include +#include #include #include #include @@ -48,17 +50,23 @@ struct ttinfo struct leap { __time64_t transition; /* Time the transition takes effect. */ - long int change; /* Seconds of correction to apply. */ + int change; /* Seconds of correction to apply. */ }; -static size_t num_transitions; +/* Internal index derived from a count taken from a TZif file. + These counts are at most 2**32 - 1; see Internet RFC 9636. + Although this type is currently unsigned to save time and/or space, + code ordinarily should not assume it is unsigned. */ +typedef unsigned int tzidx; + +static tzidx num_transitions; static __time64_t *transitions; static unsigned char *type_idxs; -static size_t num_types; +static tzidx num_types; static struct ttinfo *types; static char *zone_names; static long int rule_stdoff; -static size_t num_leaps; +static tzidx num_leaps; static struct leap *leaps; static char *tzspec; @@ -69,19 +77,25 @@ static int daylight_saved; #include #include -/* Decode the four bytes at PTR as a signed integer in network byte order. */ -static inline int +/* Decode the four bytes at PTR as a tzidx in network byte order. */ +static inline tzidx __attribute ((always_inline)) -decode (const void *ptr) +tzidx_decode (const void *ptr) { - if (BYTE_ORDER == BIG_ENDIAN && sizeof (int) == 4) - return *(const int *) ptr; - if (sizeof (int) == 4) - return bswap_32 (*(const int *) ptr); - - const unsigned char *p = ptr; - int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0; + if (sizeof (tzidx) == 4 + && offsetof (struct tzhead, tzh_timecnt) % alignof (tzidx) == 0 + && offsetof (struct tzhead, tzh_typecnt) % alignof (tzidx) == 0 + && offsetof (struct tzhead, tzh_charcnt) % alignof (tzidx) == 0 + && offsetof (struct tzhead, tzh_leapcnt) % alignof (tzidx) == 0 + && offsetof (struct tzhead, tzh_ttisstdcnt) % alignof (tzidx) == 0 + && offsetof (struct tzhead, tzh_ttisutcnt) % alignof (tzidx) == 0) + { + tzidx const *uptr = ptr, i = *uptr; + return BYTE_ORDER == BIG_ENDIAN ? i : bswap_32 (i); + } + unsigned char const *p = ptr; + unsigned long result = 0; result = (result << 8) | *p++; result = (result << 8) | *p++; result = (result << 8) | *p++; @@ -90,15 +104,37 @@ decode (const void *ptr) return result; } +/* Decode the four bytes at PTR as a signed int in network byte order. */ +static inline int +__attribute ((always_inline)) +decode (const void *ptr) +{ + tzidx i = tzidx_decode (ptr); + if ((UINT_MAX >> 31) == 1 && INT_MAX == UINT_MAX / 2 && (int) UINT_MAX == -1) + return i; + + /* An unusual platform. Explicitly extend the sign. */ + if (i <= INT_MAX) + return i; + int r = ~i; + return ~r; +} static inline int64_t __attribute ((always_inline)) decode64 (const void *ptr) { - if ((BYTE_ORDER == BIG_ENDIAN)) - return *(const int64_t *) ptr; + if (sizeof (int64_t) == 8) + { + int64_t const *p = ptr, i = *p; + return BYTE_ORDER == BIG_ENDIAN ? i : bswap_64 (i); + } - return bswap_64 (*(const int64_t *) ptr); + unsigned char const *p = ptr; + uint64_t result = 0; /* uint64_t avoids undefined behavior with <<. */ + for (int i = 0; i < 8; i++) + result = (result << 8) | p[i]; + return result; } @@ -106,11 +142,11 @@ void __tzfile_read (const char *file) { static const char default_tzdir[] = TZDIR; - size_t num_isstd, num_isgmt; + tzidx num_isstd, num_isgmt; FILE *f; - struct tzhead tzhead; - size_t chars; - size_t i; + union { struct tzhead tzhead; tzidx tzidx_aligned; } u; + tzidx chars; + tzidx i; int was_using_tzfile = __use_tzfile; int trans_width = 4; char *new = NULL; @@ -174,7 +210,7 @@ __tzfile_read (const char *file) if (__fstat64_time64 (__fileno (f), &st) != 0) goto lose; - free ((void *) transitions); + free (transitions); transitions = NULL; /* Remember the inode and device number and modification time. */ @@ -186,34 +222,41 @@ __tzfile_read (const char *file) __fsetlocking (f, FSETLOCKING_BYCALLER); read_again: - if (__builtin_expect (__fread_unlocked ((void *) &tzhead, sizeof (tzhead), - 1, f) != 1, 0) - || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0) + if (__builtin_expect (__fread_unlocked (&u, sizeof u.tzhead, 1, f) != 1, 0) + || memcmp (u.tzhead.tzh_magic, TZ_MAGIC, sizeof u.tzhead.tzh_magic) != 0) goto lose; + f_offset += sizeof u.tzhead; - f_offset += sizeof tzhead; - num_transitions = (size_t) decode (tzhead.tzh_timecnt); - num_types = (size_t) decode (tzhead.tzh_typecnt); - chars = (size_t) decode (tzhead.tzh_charcnt); - num_leaps = (size_t) decode (tzhead.tzh_leapcnt); - num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt); - num_isgmt = (size_t) decode (tzhead.tzh_ttisutcnt); + num_transitions = tzidx_decode (u.tzhead.tzh_timecnt); + num_types = tzidx_decode (u.tzhead.tzh_typecnt); + chars = tzidx_decode (u.tzhead.tzh_charcnt); + num_leaps = tzidx_decode (u.tzhead.tzh_leapcnt); + num_isstd = tzidx_decode (u.tzhead.tzh_ttisstdcnt); + num_isgmt = tzidx_decode (u.tzhead.tzh_ttisutcnt); - if (__glibc_unlikely (num_isstd > num_types || num_isgmt > num_types)) + if (__glibc_unlikely (num_isstd > num_types || num_isgmt > num_types + || !num_types)) goto lose; - if (trans_width == 4 && tzhead.tzh_version[0] != '\0') + if (trans_width == 4 && u.tzhead.tzh_version[0] != '\0') { /* We use the 8-byte format. */ trans_width = 8; /* Position the stream before the second header. */ - size_t to_skip = (num_transitions * (4 + 1) - + num_types * 6 - + chars - + num_leaps * 8 - + num_isstd - + num_isgmt); + off_t to_skip, product; + bool v = false; + v |= ckd_mul (&to_skip, num_transitions, 4 + 1); + v |= ckd_mul (&product, num_types, 6); + v |= ckd_add (&to_skip, to_skip, product); + v |= ckd_add (&to_skip, to_skip, chars); + v |= ckd_mul (&product, num_leaps, 8); + v |= ckd_add (&to_skip, to_skip, product); + v |= ckd_add (&to_skip, to_skip, num_isstd); + v |= ckd_add (&to_skip, to_skip, num_isgmt); + if (v) + goto lose; + if (fseek (f, to_skip, SEEK_CUR) != 0) goto lose; @@ -222,34 +265,29 @@ __tzfile_read (const char *file) } /* Compute the size of the POSIX time zone specification in the - file. */ - size_t tzspec_len; + file. This includes the trailing but not the leading newline. */ + size_t tzspec_size; if (trans_width == 8) { - off_t rem = st.st_size - f_offset; - if (__builtin_expect (rem < 0 - || (size_t) rem < (num_transitions * (8 + 1) - + num_types * 6 - + chars), 0)) - goto lose; - tzspec_len = (size_t) rem - (num_transitions * (8 + 1) - + num_types * 6 - + chars); - if (__builtin_expect (num_leaps > SIZE_MAX / 12 - || tzspec_len < num_leaps * 12, 0)) - goto lose; - tzspec_len -= num_leaps * 12; - if (__glibc_unlikely (tzspec_len < num_isstd)) - goto lose; - tzspec_len -= num_isstd; - if (__glibc_unlikely (tzspec_len == 0 || tzspec_len - 1 < num_isgmt)) - goto lose; - tzspec_len -= num_isgmt + 1; - if (tzspec_len == 0) + off_t rem = st.st_size - f_offset, product; + bool v = false; + v |= ckd_mul (&product, num_transitions, 8 + 1); + v |= ckd_sub (&rem, rem, product); + v |= ckd_mul (&product, num_types, 6); + v |= ckd_sub (&rem, rem, product); + v |= ckd_sub (&rem, rem, chars); + v |= ckd_mul (&product, num_leaps, 12); + v |= ckd_sub (&rem, rem, product); + v |= ckd_sub (&rem, rem, num_isstd); + v |= ckd_sub (&rem, rem, num_isgmt); + v |= rem <= 1; + v |= 3 <= rem && rem < sizeof "XYZ0\n"; + if (v) goto lose; + tzspec_size = rem - 1; } else - tzspec_len = 0; + tzspec_size = 0; /* The file is parsed into a single heap allocation, comprising of the following arrays: @@ -259,7 +297,7 @@ __tzfile_read (const char *file) struct ttinfo types[num_types]; unsigned char type_idxs[num_types]; char zone_names[chars]; - char tzspec[tzspec_len]; + char tzspec[tzspec_size]; The piece-wise allocations from buf below verify that no overflow/wraparound occurred in these computations. @@ -274,12 +312,18 @@ __tzfile_read (const char *file) "alignment of struct leap"); struct alloc_buffer buf; { - size_t total_size = (num_transitions * sizeof (__time64_t) - + num_leaps * sizeof (struct leap) - + num_types * sizeof (struct ttinfo) - + num_transitions /* type_idxs */ - + chars /* zone_names */ - + tzspec_len); + size_t total_size, product; + bool v = false; + v |= ckd_mul (&total_size, num_transitions, sizeof (__time64_t)); + v |= ckd_mul (&product, num_leaps, sizeof (struct leap)); + v |= ckd_add (&total_size, total_size, product); + v |= ckd_mul (&product, num_types, sizeof (struct ttinfo)); + v |= ckd_add (&total_size, total_size, product); + v |= ckd_add (&total_size, total_size, num_transitions); /* type_idxs */ + v |= ckd_add (&total_size, total_size, chars); /* zone_names */ + v |= ckd_add (&total_size, total_size, tzspec_size); + if (v) + goto lose; transitions = malloc (total_size); if (transitions == NULL) goto lose; @@ -294,18 +338,53 @@ __tzfile_read (const char *file) type_idxs = alloc_buffer_alloc_array (&buf, unsigned char, num_transitions); zone_names = alloc_buffer_alloc_array (&buf, char, chars); if (trans_width == 8) - tzspec = alloc_buffer_alloc_array (&buf, char, tzspec_len); + tzspec = alloc_buffer_alloc_array (&buf, char, tzspec_size); else tzspec = NULL; if (alloc_buffer_has_failed (&buf)) goto lose; - if (__glibc_unlikely (__fread_unlocked (transitions, trans_width, - num_transitions, f) - != num_transitions) - || __glibc_unlikely (__fread_unlocked (type_idxs, 1, num_transitions, f) - != num_transitions)) + if (trans_width == 4) + { + for (i = 0; i < num_transitions; i++) + { + union { unsigned char c[4]; tzidx tzidx_aligned; } x; + if (__fread_unlocked (&x, 1, 4, f) != 4) + goto lose; + transitions[i] = decode (&x); + if (i && transitions[i] <= transitions[i - 1]) + goto lose; + } + } + else if (sizeof (int64_t) == 8) + { + if (__fread_unlocked (transitions, 8, num_transitions, f) + != num_transitions) goto lose; + for (i = 0; i < num_transitions; i++) + { + if (BYTE_ORDER != BIG_ENDIAN) + transitions[i] = decode64 (&transitions[i]); + if (i && transitions[i] <= transitions[i - 1]) + goto lose; + } + } + else + { + for (i = 0; i < num_transitions; i++) + { + union { unsigned char c[8]; int64_t int64_t_aligned; } x; + if (__fread_unlocked (&x, 1, 8, f) != 8) + goto lose; + transitions[i] = decode64 (&x); + if (i && transitions[i] <= transitions[i - 1]) + goto lose; + } + } + + if (__glibc_unlikely (__fread_unlocked (type_idxs, 1, num_transitions, f) + != num_transitions)) + goto lose; /* Check for bogus indices in the data file, so we can hereafter safely use type_idxs[T] as indices into `types' and never crash. */ @@ -313,61 +392,67 @@ __tzfile_read (const char *file) if (__glibc_unlikely (type_idxs[i] >= num_types)) goto lose; - if (trans_width == 4) - { - /* Decode the transition times, stored as 4-byte integers in - network (big-endian) byte order. We work from the end of the - array so as not to clobber the next element to be - processed. */ - i = num_transitions; - while (i-- > 0) - transitions[i] = decode ((char *) transitions + i * 4); - } - else if (BYTE_ORDER != BIG_ENDIAN) - { - /* Decode the transition times, stored as 8-byte integers in - network (big-endian) byte order. */ - for (i = 0; i < num_transitions; ++i) - transitions[i] = decode64 ((char *) transitions + i * 8); - } - + unsigned char max_idx = 0; for (i = 0; i < num_types; ++i) { - unsigned char x[4]; + union { unsigned char c[4]; tzidx tzidx_aligned; } x; int c; - if (__builtin_expect (__fread_unlocked (x, 1, - sizeof (x), f) != sizeof (x), - 0)) + if (__builtin_expect (__fread_unlocked (&x, 1, 4, f) != 4, 0)) goto lose; c = __getc_unlocked (f); - if (__glibc_unlikely ((unsigned int) c > 1u)) + if (__glibc_unlikely (! (0 <= c && c <= 1))) goto lose; types[i].isdst = c; c = __getc_unlocked (f); - if (__glibc_unlikely ((size_t) c > chars)) + if (__glibc_unlikely (! (0 <= c && c < chars))) /* Bogus index in data file. */ goto lose; + if (max_idx < c) + max_idx = c; types[i].idx = c; - types[i].offset = decode (x); + types[i].offset = decode (&x); + + /* If long int is only 32 bits, reject offsets that cannot be negated. + RFC 9636 section 3.2 allows this. */ + long int negated_offset; + if (ckd_sub (&negated_offset, 0, types[i].offset)) + goto lose; } if (__glibc_unlikely (__fread_unlocked (zone_names, 1, chars, f) != chars)) goto lose; + if (__strnlen (zone_names + max_idx, chars - max_idx) == chars - max_idx) + goto lose; + int minimum_leap_gap = 2419199; /* See RFC 9636 section 3.2. */ + __time64_t prevtransition = -minimum_leap_gap; + int prevchange = 0; for (i = 0; i < num_leaps; ++i) { - unsigned char x[8]; - if (__builtin_expect (__fread_unlocked (x, 1, trans_width, f) - != trans_width, 0)) - goto lose; - if (trans_width == 4) - leaps[i].transition = decode (x); - else - leaps[i].transition = decode64 (x); - - if (__glibc_unlikely (__fread_unlocked (x, 1, 4, f) != 4)) - goto lose; - leaps[i].change = (long int) decode (x); + { + union { char c[8]; tzidx tzidx_aligned; int64_t int64_t_aligned; } x; + if (__builtin_expect ((__fread_unlocked (&x, 1, trans_width, f) + != trans_width), + 0)) + goto lose; + leaps[i].transition = trans_width == 4 ? decode (&x) : decode64 (&x); + if (leaps[i].transition < 0 + || leaps[i].transition - minimum_leap_gap < prevtransition) + goto lose; + prevtransition = leaps[i].transition; + } + + { + union { unsigned char c[4]; tzidx tzidx_aligned; } x; + if (__glibc_unlikely (__fread_unlocked (&x, 1, 4, f) != 4)) + goto lose; + leaps[i].change = decode (&x); + int delta_change; + if (ckd_sub (&delta_change, leaps[i].change, prevchange) + || ! (delta_change == 1 || delta_change == -1)) + goto lose; + prevchange = leaps[i].change; + } } for (i = 0; i < num_isstd; ++i) @@ -393,14 +478,16 @@ __tzfile_read (const char *file) /* Read the POSIX TZ-style information if possible. */ if (tzspec != NULL) { - assert (tzspec_len > 0); - /* Skip over the newline first. */ - if (__getc_unlocked (f) != '\n' - || (__fread_unlocked (tzspec, 1, tzspec_len - 1, f) - != tzspec_len - 1)) - tzspec = NULL; + char *nl; + assert (tzspec_size > 0); + /* Skip the leading newline, then grab everything up to the next + newline; ignore everything after that. */ + if (__getc_unlocked (f) == '\n' + && __fread_unlocked (tzspec, 1, tzspec_size, f) == tzspec_size + && (nl = memchr (tzspec, '\n', tzspec_size)) != NULL) + *nl = '\0'; else - tzspec[tzspec_len - 1] = '\0'; + tzspec = NULL; } /* Don't use an empty TZ string. */ @@ -484,16 +571,16 @@ __tzfile_read (const char *file) fclose (f); ret_free_transitions: free (new); - free ((void *) transitions); + free (transitions); transitions = NULL; } void __tzfile_compute (__time64_t timer, int use_localtime, - long int *leap_correct, int *leap_hit, + int *leap_correct, bool *leap_hit, struct tm *tp) { - size_t i; + tzidx i; if (use_localtime) { @@ -519,7 +606,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, __tzname[0] = __tzstring (&zone_names[types[i].idx]); if (__tzname[1] == NULL) { - size_t j = i; + tzidx j = i; while (j < num_types) if (types[j].isdst) { @@ -556,15 +643,16 @@ __tzfile_compute (__time64_t timer, int use_localtime, { /* Find the first transition after TIMER, and then pick the type of the transition before it. */ - size_t lo = 0; - size_t hi = num_transitions - 1; + tzidx lo = 0; + tzidx hi = num_transitions - 1; /* Assume that DST is changing twice a year and guess initial search spot from it. Half of a gregorian year has on average 365.2425 * 86400 / 2 = 15778476 seconds. - The value i can be truncated if size_t is smaller than - __time64_t, but this is harmless because it is just - a guess. */ - i = (transitions[num_transitions - 1] - timer) / 15778476; + Although i's value can be wrong if overflow occurs, + this is harmless because it is just a guess. */ + __time64_t tdiff; + ckd_sub (&tdiff, transitions[num_transitions - 1], timer); + ckd_add (&i, tdiff / 15778476, 0); if (i < num_transitions) { i = num_transitions - 1 - i; @@ -581,7 +669,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, } else { - if (i + 10 >= num_transitions || timer < transitions[i + 10]) + if (num_transitions - i <= 10 || timer < transitions[i + 10]) { /* Linear search. */ while (timer >= transitions[i]) @@ -596,7 +684,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ while (lo + 1 < hi) { - i = (lo + hi) / 2; + i = (lo >> 1) + (hi >> 1) + (lo & hi & 1); if (timer < transitions[i]) hi = i; else @@ -609,7 +697,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, && (i == num_transitions || timer < transitions[i])); */ __tzname[types[type_idxs[i - 1]].isdst] = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]); - size_t j = i; + tzidx j = i; while (j < num_transitions) { int type = type_idxs[j]; @@ -654,8 +742,8 @@ __tzfile_compute (__time64_t timer, int use_localtime, } leap: - *leap_correct = 0L; - *leap_hit = 0; + *leap_correct = 0; + *leap_hit = false; /* Find the last leap second correction transition time before TIMER. */ i = num_leaps; @@ -669,16 +757,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, if (timer == leaps[i].transition /* Exactly at the transition time. */ && (leaps[i].change > (i == 0 ? 0 : leaps[i - 1].change))) - { - *leap_hit = 1; - while (i > 0 - && leaps[i].transition == leaps[i - 1].transition + 1 - && leaps[i].change == leaps[i - 1].change + 1) - { - ++*leap_hit; - --i; - } - } + *leap_hit = true; } weak_alias (transitions, __libc_tzfile_freemem_ptr) diff --git a/time/tzset.c b/time/tzset.c index 9fb3a127f7..68e68f6cb9 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -557,8 +557,8 @@ weak_alias (__tzset, tzset) struct tm * __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) { - long int leap_correction; - int leap_extra_secs; + int leap_correction; + bool leap_extra_sec; /* Update internal database according to current TZ setting. POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. @@ -567,15 +567,15 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) if (__use_tzfile) __tzfile_compute (timer, use_localtime, &leap_correction, - &leap_extra_secs, tp); + &leap_extra_sec, tp); else { if (! __offtime (timer, 0, tp)) tp = NULL; else __tz_compute (timer, tp, use_localtime); - leap_correction = 0L; - leap_extra_secs = 0; + leap_correction = 0; + leap_extra_sec = false; } if (tp) @@ -588,7 +588,16 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) } if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp)) - tp->tm_sec += leap_extra_secs; + { + /* This assumes leap seconds can occur only when the local + time offset from UTC is a multiple of 60 seconds, + so the leap second occurs at HH:MM:60 local time. + Historically this has always been true, as the last + timezone to use some other UTC offset was Africa/Monrovia + in January 1972, whereas the first leap second did not + occur until almost six months later. */ + tp->tm_sec += leap_extra_sec; + } else tp = NULL; } diff --git a/time/tzset.h b/time/tzset.h index fb7984563e..d2d5853f0b 100644 --- a/time/tzset.h +++ b/time/tzset.h @@ -3,6 +3,7 @@ #include #include +#include /* Defined in tzset.c. */ extern char *__tzstring (const char *string) attribute_hidden; @@ -11,7 +12,7 @@ extern int __use_tzfile attribute_hidden; extern void __tzfile_read (const char *file) attribute_hidden; extern void __tzfile_compute (__time64_t timer, int use_localtime, - long int *leap_correct, int *leap_hit, + int *leap_correct, bool *leap_hit, struct tm *tp) attribute_hidden; extern void __tzset_parse_tz (const char *tz) attribute_hidden; extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) From patchwork Sun Jan 5 05:56:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104067 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5EBE13858427 for ; Sun, 5 Jan 2025 06:20:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id A7B093858C56 for ; Sun, 5 Jan 2025 05:58:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A7B093858C56 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A7B093858C56 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056727; cv=none; b=RFa8VIZiO8u2vQfG1JkqQEtd5bCF4KjbOhveUshw3QsN2KkuPrpk+6RdXznDTp04iaocdAv1oVPW1PXk8LBlYfG1F1jox+Fl3r+hHjWNPjAjBfJqZeRj6OjHzRMEvqfMJWg4mLLkL1o7pulVHHvDhW2Jl5YnTy/pVW5Nt93z7Is= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056727; c=relaxed/simple; bh=HrkqTURAcRV4m6pk0tGSqZAYLqvq3u8VApVuMroQJ0E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=vj2w1Ozoyt+6i84UbWbH+St5i7c9txluFSKUUgTNatLeZGw4Ldw46wQgTxEksOGaIUZfHmVBL4hZFZAl20gGfeNY8hQZopBm5hP3n4iTm4LPHX6Bc7drcN8H2UGWIsb1FrdAGSO2xJBQvLppnM+ExeomYTwcHj7Tk2yVrgmNmhQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7B093858C56 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=cORy3tWB Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 2CF1C3C123845 for ; Sat, 4 Jan 2025 21:58:47 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id fBku1mQL3huz; Sat, 4 Jan 2025 21:58:47 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id DE2123C123843; Sat, 4 Jan 2025 21:58:46 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu DE2123C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056726; bh=9Ga2igdBuo2lCWY0Qomf2dPzOvOQpJmeSo0R/iVYATY=; h=From:To:Date:Message-ID:MIME-Version; b=cORy3tWBY6yFJq6rghnY8HnCQZjAN6DjR9Q/QJupcmFLI2r7IOB4cpvpoF9Fi4yXV bp+BojFjvm0yDMBYzng6KMOx8BHTYRyQVzsgh1E8wxvB+f44KsUsqaEA/J0ENThtaP As71SFCi543ZCP0Uzc+co8r7dczqbnZ3+9lgOSAce4G9TKOTmtFDtAp+NZ+hw0HISB d69S0hkRT9+AxqhuTnhjIUhjGFA1O1esmqkEoQgQ+QPcrtcoMYqBLK6UiHDu7aE6u8 gdaWE6FG9LCp+T53oHjdIBouNgLiO1aFh2hPU0OMCSCnwO3fvn1aClgoW8ud0NAV6Q GLHxxPvQ7WsVg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id MxPx-rsRJDYK; Sat, 4 Jan 2025 21:58:46 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id C2A5D3C082EB9; Sat, 4 Jan 2025 21:58:46 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 13/59] Remove alignment assumption from __tzfile_read Date: Sat, 4 Jan 2025 21:56:48 -0800 Message-ID: <20250105055750.1668721-14-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): Allocate more portably, removing the need for a couple of _Static_asserts. --- time/tzfile.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index e60c3c0b9c..7125f5a8e3 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -302,14 +302,14 @@ __tzfile_read (const char *file) The piece-wise allocations from buf below verify that no overflow/wraparound occurred in these computations. - The order of the suballocations is important for alignment - purposes. __time64_t outside a struct may require more alignment - then inside a struct on some architectures, so it must come - first. */ - _Static_assert (__alignof (__time64_t) >= __alignof (struct leap), - "alignment of __time64_t"); - _Static_assert (__alignof (struct leap) >= __alignof (struct ttinfo), - "alignment of struct leap"); + The order of the suballocations is important for alignment purposes. + The calculation of ALIGNMENT_SLOP assumes arrays are allocated in + element order __time_64_t, struct leap, struct ttinfo. */ + size_t alignment_slop = + ((__alignof__ (struct leap) <= __alignof__ (__time64_t) + ? 0 : __alignof__ (struct leap) - __alignof__ (__time64_t)) + + (__alignof__ (struct ttinfo) <= __alignof__ (struct leap) + ? 0 : __alignof__ (struct ttinfo) - __alignof__ (struct leap))); struct alloc_buffer buf; { size_t total_size, product; @@ -322,6 +322,7 @@ __tzfile_read (const char *file) v |= ckd_add (&total_size, total_size, num_transitions); /* type_idxs */ v |= ckd_add (&total_size, total_size, chars); /* zone_names */ v |= ckd_add (&total_size, total_size, tzspec_size); + v |= ckd_add (&total_size, total_size, alignment_slop); if (v) goto lose; transitions = malloc (total_size); From patchwork Sun Jan 5 05:56:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104070 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 470613858CD1 for ; Sun, 5 Jan 2025 06:22:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 470613858CD1 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=i7ccOYkD X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 31A1E3858C66 for ; Sun, 5 Jan 2025 05:58:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 31A1E3858C66 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 31A1E3858C66 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056730; cv=none; b=pwJV95q1lhJhIBTFbQ777vOChXtx7RfDr/7YduMpsKAKqn1W42CEqKsdTafzTOgx7JojMGgPJXWudSdbmfG+izYXbWQV/+XPlF3a0FOA5KlMi6nod7pa4jelF1++b+81IfjMHxDMauGzSH/ElAo8eaeEyUWjep0VMFVwyTLiHFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056730; c=relaxed/simple; bh=Isf3FaLwppqgM/8Vo45uZt0M9DZzat58+9GwEKW6dn4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uYcIfa47rGq+F6jxZMi6ZTSKAxXTT/cXpFdeGhw4qak5sg15A9Q8U1GgtDFOsF2/C1vKKzCt9nhqdRdOVeFVBpTYb21Pr8ahtxfEL2TjNuhxMH4cvi+EzSMf7qutHFEM54FhwPIDYzYSsZ+C9HEqSvgPxfC6PGrt0o/ylxSEHus= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 31A1E3858C66 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id AB36C3C123844 for ; Sat, 4 Jan 2025 21:58:49 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id deE5JIqSrS_L; Sat, 4 Jan 2025 21:58:49 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 32B383C123843; Sat, 4 Jan 2025 21:58:49 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 32B383C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056729; bh=OND5Qk9PuLyLcAkPj/vzTTroC2Y1oyU6usWB2l0D5hw=; h=From:To:Date:Message-ID:MIME-Version; b=i7ccOYkDxPmyUofpDW4DilfeYcGK66IELCcC83gwzctbU7VEDxgLPNVHQNOnd+noV ZNaUmCLCCdpU+9XZU3QpPQaP0R2c5smNYZoBfSbCpddIt2o0Cxu4eHRSvMUGVZTWTh YjwhmhrIkW1PMjy7qxInHHB2j0HRCPyrui0qs9WfEIebpxV3i1BIvI/Q7ckZbMBd0D Vg7cIURISc1hczfx3ud1NidtFftc/N5vJ00IJk2LWR/D0vOvKW/EektieIRWYC5P9b gmf8JqpJ6OJIhoH8x7oqYtDQKYYFnuMjwLWr/jXGWvyJypJvJpDYppOUmXkZKm6mYw t6PIbh8XYFJ3Q== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id G3zmfOpq88M8; Sat, 4 Jan 2025 21:58:49 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 157B23C082EB9; Sat, 4 Jan 2025 21:58:49 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 14/59] Treat oddball UTC offset strings as zero Date: Sat, 4 Jan 2025 21:56:49 -0800 Message-ID: <20250105055750.1668721-15-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Also, extend support of offsets and times to the range INT_MIN to INT_MAX. tzfile already supports offsets in that range because the TZif format uses 32-bit signed numbers for offsets, so we might as well be consistent here, if only to allow easier testing via traditional POSIX TZ strings. * time/tzset.c: Include stdckdint.h, not stdio.h. (compute_offset): Remove. (parse_int, parse_hhmmss): New functions. (parse_offset): Use parse_hhmmss instead of sscanf to have well-defined behavior with outlandish input, such as numbers so large they overflow, or whose textual representations have more than INT_MAX bytes. Consistently treat overflow as 0 rather than as some randomish value. Treat values outside the POSIX range as overflows, as the rest of the code cannot deal with times greater than week or so or offsets greater than a day or so. (parse_rule): Use parse_int and parse_hhmmss instead of strtoul and sscanf, for similar reasons. --- time/tzset.c | 177 +++++++++++++++++++++++++++++---------------------- 1 file changed, 102 insertions(+), 75 deletions(-) diff --git a/time/tzset.c b/time/tzset.c index 68e68f6cb9..6783067053 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -17,8 +17,8 @@ #include #include +#include #include -#include #include #include #include @@ -133,18 +133,6 @@ update_vars (void) } -static unsigned int -compute_offset (unsigned int ss, unsigned int mm, unsigned int hh) -{ - if (ss > 59) - ss = 59; - if (mm > 59) - mm = 59; - if (hh > 24) - hh = 24; - return ss + mm * 60 + hh * 60 * 60; -} - /* Parses the time zone abbreviation at *TZP, and writes a pointer to an interned string to tz_rules[WHICHRULE].name. On success, advances *TZP, and returns true. Returns false otherwise. */ @@ -182,44 +170,90 @@ parse_tzname (const char **tzp, int whichrule) return true; } -/* Parses the time zone offset at *TZP, and writes it to - tz_rules[WHICHRULE].offset. Returns true if the parse was +/* Parse the prefix of *TZP into an int, and update *TZP. + Return the unsigned decimal integer represented by a prefix of S; however, + return -1 if the integer is missing or if exceeds INT_MAX. */ +static int +parse_int (char const **tzp) +{ + char const *tz = *tzp; + if (!isdigit (*tz)) + return -1; + int r = 0; + bool v = false; + do + { + v |= ckd_mul (&r, r, 10); + v |= ckd_add (&r, r, *tz - '0'); + tz++; + } + while (isdigit (*tz)); + + *tzp = tz; + return v ? -1 : r; +} + +/* Parse an optional [+-]HH[:MM[:SS]] prefix into an integer number of seconds. + It is a prefix of the text string *TZP; advance *TZP past the prefix. + HH, MM, and SS can be any positive number of digits. + The absolute value of the offset must be less than ABS_TOOLARGE. + Ordinarily, store the result into *OFFSET and return 0. + If the prefix is missing, store 0 and return a negative number. + If the result overflows, store 0 and return a positive number. */ +static int +parse_hhmmss (char const **tzp, int abs_toolarge, int *offset) +{ + const char *tz = *tzp; + char tz0 = *tz; + tz += tz0 == '-' || tz0 == '+'; + if (!isdigit (*tz)) + { + *offset = 0; + return -1; + } + + int hh = parse_int (&tz), mm = 0, ss = 0; + if (tz[0] == ':' && isdigit (tz[1])) + { + tz++, mm = parse_int (&tz); + if (tz[0] == ':' && isdigit (tz[1])) + tz++, ss = parse_int (&tz); + } + + bool v = (hh < 0) | (mm < 0) | (ss < 0); + int off, mm60; + v |= ckd_mul (&off, hh, 60 * 60); + v |= ckd_mul (&mm60, mm, 60); + v |= ckd_add (&off, off, mm60); + v |= ckd_add (&off, off, ss); + v |= abs_toolarge <= off; + if (tz0 == '-') + ckd_sub (&off, 0, off); /* Ignore any overflow. */ + *tzp = tz; + *offset = v ? 0 : off; + return v; +} + +/* Parse the time zone offset at *TZP, and write it to + tz_rules[WHICHRULE].offset. Return true if the parse was successful. */ static bool parse_offset (const char **tzp, int whichrule) { - const char *tz = *tzp; - bool leading_sign = *tz == '-' || *tz == '+'; - if (whichrule == 0 - && (!leading_sign && !isdigit (*tz))) - return false; + int offset; + int status = parse_hhmmss (tzp, (24 + 1) * 60 * 60, &offset); - int sign; - if (leading_sign) - sign = *tz++ == '-' ? 1 : -1; + if (status == 0) + offset = -offset; + else if (status < 0 && whichrule != 0) + { + /* A missing DST stands for one hour east of standard time. */ + offset = tz_rules[0].offset + 60 * 60; + } else - sign = -1; - *tzp = tz; + return false; - unsigned short int hh; - unsigned short mm = 0; - unsigned short ss = 0; - int consumed = 0; - if (sscanf (tz, "%hu%n:%hu%n:%hu%n", - &hh, &consumed, &mm, &consumed, &ss, &consumed) > 0) - tz_rules[whichrule].offset = sign * compute_offset (ss, mm, hh); - else - /* Nothing could be parsed. */ - if (whichrule == 0) - { - /* Standard time defaults to offset zero. */ - tz_rules[0].offset = 0; - return false; - } - else - /* DST defaults to one hour later than standard time. */ - tz_rules[1].offset = tz_rules[0].offset + (60 * 60); - *tzp = tz + consumed; + tz_rules[whichrule].offset = offset; return true; } @@ -237,30 +271,32 @@ parse_rule (const char **tzp, int whichrule) tz += *tz == ','; /* Get the date of the change. */ - if (*tz == 'J' || isdigit (*tz)) + bool is_J = *tz == 'J'; + if (is_J || isdigit (*tz)) { - char *end; - tzr->type = *tz == 'J' ? J1 : J0; - if (tzr->type == J1 && !isdigit (*++tz)) - return false; - unsigned long int d = strtoul (tz, &end, 10); - if (end == tz || d > 365) - return false; - if (tzr->type == J1 && d == 0) + tzr->type = is_J ? J1 : J0; + tz += is_J; + int d = parse_int (&tz); + if (! (is_J <= d && d <= 365)) return false; tzr->d = d; - tz = end; } else if (*tz == 'M') { tzr->type = M; - int consumed; - if (sscanf (tz, "M%hu.%hu.%hu%n", - &tzr->m, &tzr->n, &tzr->d, &consumed) != 3 - || tzr->m < 1 || tzr->m > 12 - || tzr->n < 1 || tzr->n > 5 || tzr->d > 6) + tz++; + int i = parse_int (&tz); + if (! (1 <= i && i <= 12 && *tz++ == '.')) + return false; + tzr->m = i; + i = parse_int (&tz); + if (! (1 <= i && i <= 5 && *tz++ == '.')) return false; - tz += consumed; + tzr->n = i; + i = parse_int (&tz); + if (! (0 <= i && i <= 6)) + return false; + tzr->d = i; } else if (*tz == '\0') { @@ -287,31 +323,22 @@ parse_rule (const char **tzp, int whichrule) else return false; + int secs; + if (*tz != '\0' && *tz != '/' && *tz != ',') return false; else if (*tz == '/') { /* Get the time of day of the change. */ - int negative; ++tz; - if (*tz == '\0') + if (parse_hhmmss (&tz, (167 + 1) * 60 * 60, &secs) != 0) return false; - negative = *tz == '-'; - tz += negative; - /* Default to 2:00 AM. */ - unsigned short hh = 2; - unsigned short mm = 0; - unsigned short ss = 0; - int consumed = 0; - sscanf (tz, "%hu%n:%hu%n:%hu%n", - &hh, &consumed, &mm, &consumed, &ss, &consumed);; - tz += consumed; - tzr->secs = (negative ? -1 : 1) * ((hh * 60 * 60) + (mm * 60) + ss); } else /* Default to 2:00 AM. */ - tzr->secs = 2 * 60 * 60; + secs = 2 * 60 * 60; + tzr->secs = secs; tzr->computed_for = -1; *tzp = tz; return true; @@ -491,7 +518,7 @@ compute_change (tz_rule *rule, int year) } /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want. - Just add the time of day and local offset from GMT, and we're done. */ + Just subtract the UT offset and add the time of day, and we're done. */ rule->change = t - rule->offset + rule->secs; rule->computed_for = year; From patchwork Sun Jan 5 05:56:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104055 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C8523858C78 for ; Sun, 5 Jan 2025 06:14:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C8523858C78 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=YJw6QNyF X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 8A7503858D3C for ; Sun, 5 Jan 2025 05:58:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A7503858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8A7503858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056732; cv=none; b=vwU8rafixPnQf8EWFE/uSTR0B6qI/vjSAt83as5oWONL0EJ7KdH+gFawotURycM0elL/ze3iVYDolV1pR1F9dE3QDdBUa9NRx8QCVfp0ll7fCStoYo0/6o6z0Z7o1eIJOx0QHa6COZgh6fvMt1zbWsmuFCwc8FxFBOGCF1UbiU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056732; c=relaxed/simple; bh=JRjPqgemaF8ByJnt8C8D1wN5VtRpz7q7yi6vrxjJ7nM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=of9gIRf1JJVne7AtK81zqzH1lsuv2SdKMxzhRPcpLSl3ALUq8NJS5Lih+pffJdOkeTrhK/b8+nbZNbEmPK37DZKXk70GBrNRoEyXI+Fn4pio6nQFML8ZfE12JBjGqFAybpejr6V7ehERqifE9i0f0AFS80gD6QRodhicTeMzk18= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8A7503858D3C Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 108ED3C123845 for ; Sat, 4 Jan 2025 21:58:52 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id dzoYgMQ9OLnF; Sat, 4 Jan 2025 21:58:51 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id B38163C123843; Sat, 4 Jan 2025 21:58:51 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu B38163C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056731; bh=IzS0CGx6z6DqXy0/OMrfWod0TdtFqA03OjsIvAbQTow=; h=From:To:Date:Message-ID:MIME-Version; b=YJw6QNyFJBwrVNHlLq018z0It4XSVsirLZj5Vy83+4qj96ycuRtIc3mSv5utxQ7zU c/EkuZ6dToCIW0D+iusAJlCisZqRsTJmr8KSL9AkJsZiR+tkXuSNaGm4KzPvDapmRU s9tXyz4BoDolzYA6pqi8u69lqjJgf3/WPSP+Sv2XAg8tAfep6jkLcQXrVtNcRl0fFq oLUbbrcOBL48dI62+ExVp/E3FkbG9bD1A0Vd5MuTgcuHLnMN1z84hhVzwL0YI//m8b syJ35W+RMQhfvuh+q54U0bJ46dX0WA7GzWRnVjpq9FLGBm8CXAkyBYk6jXmC6GEIRs ntGl12ng1nAqg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id bmkJESD7s23P; Sat, 4 Jan 2025 21:58:51 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 9C4B73C082EB9; Sat, 4 Jan 2025 21:58:51 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 15/59] Simplify __tz_compute Date: Sat, 4 Jan 2025 21:56:50 -0800 Message-ID: <20250105055750.1668721-16-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (__tz_compute): Omit last arg USE_LOCALTIME, since this function should be called only when using localtime. All callers changed. Clarify isdst computation. --- time/tzfile.c | 2 +- time/tzset.c | 35 ++++++++++++----------------------- time/tzset.h | 2 +- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 7125f5a8e3..5a7514e466 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -636,7 +636,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, goto use_last; /* Use the rules from the TZ string to compute the change. */ - __tz_compute (timer, tp, 1); + __tz_compute (timer, tp); goto leap; } diff --git a/time/tzset.c b/time/tzset.c index 6783067053..bd3509307e 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -525,32 +525,21 @@ compute_change (tz_rule *rule, int year) } -/* Figure out the correct timezone for TM and set `__tzname', - `__timezone', and `__daylight' accordingly. */ +/* Figure out the correct local timezone for TM. */ void -__tz_compute (__time64_t timer, struct tm *tm, int use_localtime) +__tz_compute (__time64_t timer, struct tm *tm) { compute_change (&tz_rules[0], 1900 + tm->tm_year); compute_change (&tz_rules[1], 1900 + tm->tm_year); - if (use_localtime) - { - int isdst; - - /* We have to distinguish between northern and southern - hemisphere. For the latter the daylight saving time - ends in the next year. */ - if (__builtin_expect (tz_rules[0].change - > tz_rules[1].change, 0)) - isdst = (timer < tz_rules[1].change - || timer >= tz_rules[0].change); - else - isdst = (timer >= tz_rules[0].change - && timer < tz_rules[1].change); - tm->tm_isdst = isdst; - tm->tm_zone = __tzname[isdst]; - tm->tm_gmtoff = tz_rules[isdst].offset; - } + /* Distinguish between northern and southern hemisphere. + For the latter the daylight saving time ends in the next year. */ + bool isdst = (__glibc_likely (tz_rules[0].change <= tz_rules[1].change) + ? tz_rules[0].change <= timer && timer < tz_rules[1].change + : ! (tz_rules[1].change <= timer && timer < tz_rules[0].change)); + tm->tm_isdst = isdst; + tm->tm_zone = __tzname[isdst]; + tm->tm_gmtoff = tz_rules[isdst].offset; } /* Reinterpret the TZ environment variable and set `tzname'. */ @@ -599,8 +588,8 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) { if (! __offtime (timer, 0, tp)) tp = NULL; - else - __tz_compute (timer, tp, use_localtime); + else if (use_localtime) + __tz_compute (timer, tp); leap_correction = 0; leap_extra_sec = false; } diff --git a/time/tzset.h b/time/tzset.h index d2d5853f0b..8c87ad6a24 100644 --- a/time/tzset.h +++ b/time/tzset.h @@ -15,7 +15,7 @@ extern void __tzfile_compute (__time64_t timer, int use_localtime, int *leap_correct, bool *leap_hit, struct tm *tp) attribute_hidden; extern void __tzset_parse_tz (const char *tz) attribute_hidden; -extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) +extern void __tz_compute (__time64_t timer, struct tm *tm) __THROW attribute_hidden; __libc_lock_define (extern, __tzset_lock attribute_hidden); From patchwork Sun Jan 5 05:56:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104041 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6FDCC3858402 for ; Sun, 5 Jan 2025 06:04:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6FDCC3858402 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=e8PXtat7 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 2BD1B3858C54 for ; Sun, 5 Jan 2025 05:58:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2BD1B3858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2BD1B3858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056735; cv=none; b=Z2QON5HB5q+zIZj+XupD3XuDDmjbPHWkWJM+DLo2nSZpqFWWhqHHzc/ym77rsGZc/UbdN1WHgtfY8DTAEQumeqanV+RfVbnWs2ByElVrecA9Hw13ve2p6B7E/H09VfPvtv7SVNS3FHKw8lHKV5ZfvfErwMGEHFOF7iJVVbxVqc4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056735; c=relaxed/simple; bh=EMyCWbZ4wdOkL0Li8KC0jde1Lb1T9WOTsGo3j6nSXWo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EZyZJvhbX2APFWdV49ryODJhJHeXzsYRT/SX3XD6bNAt/wOJHzyG35KCHTNYe5wEoIr5M3A64BS/ZUFU1ZQuuA0+lD0NPYjKC2+wLdTGXuN8Wrm5BrU0qaCgnvax973YrOcgircgHR+QaMPmlyDybosYp9YeylxctmIh1kVRHso= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2BD1B3858C54 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id A56633C123847 for ; Sat, 4 Jan 2025 21:58:54 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id D5Bf2UNQ1xMV; Sat, 4 Jan 2025 21:58:54 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 623AB3C123843; Sat, 4 Jan 2025 21:58:54 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 623AB3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056734; bh=V8UAec+L7CmuzqlJw992Fz0QZiWgkFDX1hXrDO49qPw=; h=From:To:Date:Message-ID:MIME-Version; b=e8PXtat79qlIehnf+XA2THOexRJCkm9cI7Sewi+gk1RkZGQ8qpCoAxJdqvtjl2P1Z NLQMl6XXnfyS62dLFPjt5Tm+DM/fe+0yyhROvLEeNq03GGgDE2BG1xcTSf5SIOSxeF MJmWWr5EKgvjiPhLcQtjQHFQ6clkF80g61xG4dgK6QlAiEGPhJvT6SyyowGrkC0kWI zGL7J2AcoVD2yn/c2xf4gWg0PP00aZ90OOrj7lIiPgxmH/PZGYrIpO3rmLm6QqSHD2 rf93wvwhud2Pcdfwzf/kTuTlb3+2zfzrVE4xZtMCD6v09+eIMj6LFiNzyKZGzx4Tuf NrfmYNDiYEBcQ== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id vGnYyVUSitqk; Sat, 4 Jan 2025 21:58:54 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 4C4803C082EB9; Sat, 4 Jan 2025 21:58:54 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 16/59] Improve performance in 2 BC Date: Sat, 4 Jan 2025 21:56:51 -0800 Message-ID: <20250105055750.1668721-17-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c: Include , for INT_MIN. (parse_rule): Initialize computed_for to INT_MIN, not -1, as INT_MIN is otherwise impossible after adding 1900. (compute_change): Simplify accordingly. --- time/tzset.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/time/tzset.c b/time/tzset.c index bd3509307e..e869fbf3fd 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -16,6 +16,7 @@ . */ #include +#include #include #include #include @@ -55,7 +56,8 @@ typedef struct /* We cache the computed time of change for a given year so we don't have to recompute it. */ __time64_t change; /* When to change to this zone. */ - int computed_for; /* Year above is computed for. */ + int computed_for; /* Year that CHANGE is computed for. + If INT_MIN, CHANGE is unspecified. */ } tz_rule; /* tz_rules[0] is standard, tz_rules[1] is daylight. */ @@ -339,7 +341,7 @@ parse_rule (const char **tzp, int whichrule) secs = 2 * 60 * 60; tzr->secs = secs; - tzr->computed_for = -1; + tzr->computed_for = INT_MIN; *tzp = tz; return true; } @@ -444,8 +446,7 @@ compute_change (tz_rule *rule, int year) { __time64_t t; - if (year != -1 && rule->computed_for == year) - /* Operations on times in 2 BC will be slower. Oh well. */ + if (rule->computed_for == year) return; /* First set T to January 1st, 0:00:00 GMT in YEAR. */ From patchwork Sun Jan 5 05:56:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104064 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 55D1B3858D3C for ; Sun, 5 Jan 2025 06:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 55D1B3858D3C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=FKlPcjxA X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 831C53858C51 for ; Sun, 5 Jan 2025 05:58:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 831C53858C51 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 831C53858C51 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056737; cv=none; b=ORQX5j+y4BXTZpRzJ7VXbuL4A+6R4wOyZ/dpMfingetvyUYjV0Y4wmlyiVd43YfuoSjjD+Ee/U7IlTuIaf7AWd7+dcFaIjcuFXRVkNoGIk5aKCVNoWrqxNUWJmTV8bCBI4O9FZlHZ4hpyzx8uY7iVWY/nEb0vGMfOhKES19kTro= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056737; c=relaxed/simple; bh=Uk8+GOCW5gaA16SvMkuebuqE0LANJevItCaQf+ADF1s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lf8sf9gy5VFc/mkW1SBv5jUEcZwlAK76luOMTwDmXqey3JgyfvpUyN1DmDym0g5Ba8VrgFH8xE49N6zLtXEbY8xXdtlKFANTLMIAG8XHhdzEIAPtjRTepLpY3lwTH6svocIcoFTAWUn6MtbOyqKMwuNNet7q765SHZflyR1vNFw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 831C53858C51 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 083E43C123844 for ; Sat, 4 Jan 2025 21:58:57 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id LOlr7JKth6pY; Sat, 4 Jan 2025 21:58:56 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 961803C123843; Sat, 4 Jan 2025 21:58:56 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 961803C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056736; bh=nlejPgF9wV+EO4Y4OB+M5ZORifzGSpvLkeqkCwYXZH8=; h=From:To:Date:Message-ID:MIME-Version; b=FKlPcjxAEYSUvond732BAQQXVNTJDVRCu4huzwc4574BPlUJIURN+f38+zHXDxaj+ r2Kq29Rujt1PTBY3Wd+ngwcOWAwgnpOvn00lTsVI7iBJedXczJxsqPMZgNFBEg6Gcy q5D0oqC0GYXiw3WxPnLV9JkKao+fLYmWoMxoVAhEuQKfupBMiLKem5ed98fMRU1FuR fokbN74a6b4BiA8Vq26p2HWMNpz+6V/PRg0s9/8U2MK3GxTibkCC6ysZ2S0lARED74 26HPOA4rhLmV+tm6YRCI/SOnHesO8SUVBJN7XiPa2y5gdgUzoYroJ/eZhKFuX3NeNn w8dFpCj8lIojA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id ZelUILFkXUB5; Sat, 4 Jan 2025 21:58:56 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 7F35B3C082EB9; Sat, 4 Jan 2025 21:58:56 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 17/59] =?utf-8?q?Don=E2=80=99t_mishandle_years_=3C_1970_in_c?= =?utf-8?q?ompute=5Fchange?= Date: Sat, 4 Jan 2025 21:56:52 -0800 Message-ID: <20250105055750.1668721-18-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tst-posixtz.c (tests): Test 1966 too. * time/tzset.c (tz_rule): Change computed_for member from int to long long int, so that we needn't worry about int overflow when adding 1900 to a year. All uses changed. * time/tzset.c (compute_change): Year arg is now long long int, not int. All uses changed. Do not mess up if the year is less than 1970. --- time/tst-posixtz.c | 13 +++++++++-- time/tzset.c | 54 ++++++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/time/tst-posixtz.c b/time/tst-posixtz.c index 936c5b8fc6..482bd4f2bc 100644 --- a/time/tst-posixtz.c +++ b/time/tst-posixtz.c @@ -29,8 +29,17 @@ struct { 919973892L, "EST+5EDT,M4.1.0/2,M10.5.0/2", "1999/02/25 15:18:12 dst=0 zone=EST" }, - /* Test Atlantic Standard / Atlantic Daylight transitions in 2024 and 2038, - with explicit DST rule. */ + /* Test Atlantic Standard / Atlantic Daylight transitions in 1966, + 2024 and 2038, with explicit DST rule. See Bug#32395 for why + 1966 is being tested. */ + { -120074401, "AST4ADT,M3.2.0,M11.1.0", + "1966/03/13 01:59:59 dst=0 zone=AST" }, + { -120074400, "AST4ADT,M3.2.0/2,M11.1.0/2", + "1966/03/13 03:00:00 dst=1 zone=ADT" }, + { -99514801, "AST4ADT,M3.2.0/02:00,M11.1.0/02:00", + "1966/11/06 01:59:59 dst=1 zone=ADT" }, + { -99514800, "AST4ADT,M3.2.0/02:00:00,M11.1.0/02:00:00", + "1966/11/06 01:00:00 dst=0 zone=AST" }, { 1710050399, "AST4ADT,M3.2.0,M11.1.0", "2024/03/10 01:59:59 dst=0 zone=AST" }, { 1710050400, "AST4ADT,M3.2.0/2,M11.1.0/2", diff --git a/time/tzset.c b/time/tzset.c index e869fbf3fd..7cc675334a 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -56,15 +56,15 @@ typedef struct /* We cache the computed time of change for a given year so we don't have to recompute it. */ __time64_t change; /* When to change to this zone. */ - int computed_for; /* Year that CHANGE is computed for. - If INT_MIN, CHANGE is unspecified. */ + long long int computed_for; /* Year that CHANGE is computed for. + If LLONG_MIN, CHANGE is unspecified. */ } tz_rule; /* tz_rules[0] is standard, tz_rules[1] is daylight. */ static tz_rule tz_rules[2]; -static void compute_change (tz_rule *rule, int year) __THROW; +static void compute_change (tz_rule *rule, long long int year) __THROW; static void tzset_internal (int always); /* List of buffers containing time zone strings. */ @@ -341,7 +341,7 @@ parse_rule (const char **tzp, int whichrule) secs = 2 * 60 * 60; tzr->secs = secs; - tzr->computed_for = INT_MIN; + tzr->computed_for = LLONG_MIN; *tzp = tz; return true; } @@ -442,7 +442,7 @@ tzset_internal (int always) when the change described by RULE will occur and put it in RULE->change, saving YEAR in RULE->computed_for. */ static void -compute_change (tz_rule *rule, int year) +compute_change (tz_rule *rule, long long int year) { __time64_t t; @@ -450,17 +450,15 @@ compute_change (tz_rule *rule, int year) return; /* First set T to January 1st, 0:00:00 GMT in YEAR. */ - if (year > 1970) - t = ((year - 1970) * 365 - + /* Compute the number of leapdays between 1970 and YEAR - (exclusive). There is a leapday every 4th year ... */ - + ((year - 1) / 4 - 1970 / 4) - /* ... except every 100th year ... */ - - ((year - 1) / 100 - 1970 / 100) - /* ... but still every 400th year. */ - + ((year - 1) / 400 - 1970 / 400)) * SECSPERDAY; - else - t = 0; + t = (((year - 1970) * 365 + /* Compute the number of leap days between 1970 and YEAR (exclusive). + There is a leap day every 4th year ... */ + + (((year - 1) >> 2) - 1970 / 4) + /* ... except every 100th year ... */ + - ((year - 1) / 100 - ((year - 1) % 100 < 0) - 1970 / 100) + /* ... but still every 400th year. */ + + ((year - 1) / 400 - ((year - 1) % 400 < 0) - 1970 / 400)) + * SECSPERDAY); switch (rule->type) { @@ -484,7 +482,7 @@ compute_change (tz_rule *rule, int year) /* Mm.n.d - Nth "Dth day" of month M. */ { unsigned int i; - int d, m1, yy0, yy1, yy2, dow; + int d, m1; const unsigned short int *myday = &__mon_yday[__isleap (year)][rule->m]; @@ -493,10 +491,12 @@ compute_change (tz_rule *rule, int year) /* Use Zeller's Congruence to get day-of-week of first day of month. */ m1 = (rule->m + 9) % 12 + 1; - yy0 = (rule->m <= 2) ? (year - 1) : year; - yy1 = yy0 / 100; - yy2 = yy0 % 100; - dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; + long long int yy0 = year - (rule->m <= 2); + int yy1 = yy0 / 100 - (yy0 % 100 < 0); + int yy2 = yy0 % 100 + (yy0 % 100 < 0 ? 100 : 0); + int dow = (((13 * m1 - 1) / 5 + 1 + yy2 + (yy2 >> 2) + + (yy1 >> 2) - 2 * yy1) + % 7); if (dow < 0) dow += 7; @@ -530,8 +530,11 @@ compute_change (tz_rule *rule, int year) void __tz_compute (__time64_t timer, struct tm *tm) { - compute_change (&tz_rules[0], 1900 + tm->tm_year); - compute_change (&tz_rules[1], 1900 + tm->tm_year); + _Static_assert (INT_MAX <= LLONG_MAX - 1900, + "long long int must be wider than int"); + long long int year = 1900LL + tm->tm_year; + compute_change (&tz_rules[0], year); + compute_change (&tz_rules[1], year); /* Distinguish between northern and southern hemisphere. For the latter the daylight saving time ends in the next year. */ @@ -574,6 +577,11 @@ weak_alias (__tzset, tzset) struct tm * __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) { + /* Check that __time64_t is wider than int, + so that __tz_convert cannot fail due to integer overflow. */ + _Static_assert (sizeof (int) < sizeof (__time64_t), + "__time64_t must be wider than int"); + int leap_correction; bool leap_extra_sec; From patchwork Sun Jan 5 05:56:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104068 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C40A3858D28 for ; Sun, 5 Jan 2025 06:22:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C40A3858D28 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=fSjKfQAG X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id CA0F1385840D for ; Sun, 5 Jan 2025 05:59:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA0F1385840D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CA0F1385840D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056741; cv=none; b=Bbf54vdZa00gBAnIMXaYjXvLhxLQ2H+KcIyYpw7+nnMEfjASso4xwQxSdE3JGMYlOHD3ggubNMs35CfQqJeBA5YG24dZPzKuHFBvq2ZJdn1Jo4IKysBna2PdJ+y8uMtvOeB3l/odpqTUIZXqyOzfBwhKalMS3KWbUm8EPlv614o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056741; c=relaxed/simple; bh=wpNjlcHpEifB01Z3I53dsCGBAuvEX2/YoF4hgiA0hZM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cKuCWniv9tb3XavyBTM9aTRHpfjYOg2LtM9IN4CPRz/lC4D59G4DMFzjDeZJo+Z4onr90oUdmJexd0XIjBm21ZJb11Ok/ABmYz9x7kcSAfJaJV0s7rL7KEYhSsqmPLpzDH5iccWIP8R39kXwbZrB/cKzOMoIfO6KaMoZQdUhwAM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA0F1385840D Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 501F03C123843 for ; Sat, 4 Jan 2025 21:59:00 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id d19GTaEVt0uG; Sat, 4 Jan 2025 21:58:59 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 7B2003C123845; Sat, 4 Jan 2025 21:58:59 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 7B2003C123845 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056739; bh=SCi0ELOzPgEm6kjNx4u8OrJKauqPsKNgFyW3FQLP+LU=; h=From:To:Date:Message-ID:MIME-Version; b=fSjKfQAGp6SWpcg40evVhqKDmi60tA8ij1VOPa/epCeTgcRZF125q7Ur5POfMxIOn 3TRwWZVZSJUf2OmIatsdh5IjVchcipTJqoBzraxiQQGYOlEVKBe2vhneTJfDUua5E0 G+kRZ9tsXT+VHeL7HNbuQKAMypPvUJHOoYYwg18QGbgECW3kR39DmmSkl2sr0+baW9 kfYfK1UzvrZNO8ASkBNnJnCpb5T00kJIcWyObtw1Ao7wL+vuaTqJLQk8ZCoDjv8V/F H1NMdrMK7OPoOqwnoP6KLzn7Xnw8cdEE7DRevjGXVYiVWdmhAHtawFarUzvWHCfB2i 3taRAn10h06mA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id xvqhR_2vgHfj; Sat, 4 Jan 2025 21:58:59 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 614033C082EB9; Sat, 4 Jan 2025 21:58:59 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 18/59] Match RFC 9636 names Date: Sat, 4 Jan 2025 21:56:53 -0800 Message-ID: <20250105055750.1668721-19-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Refactor to change spelling of identifiers to match RFC 9636. This eases conformance review. * time/tzfile.c (struct ttinfo, struct leap, __tzfile_read) (__tzfile_default, __tzfile_compute): Change member names and locals to match the RFC. All use changed. (timecnt, typecnt, leapcnt): Rename from num_transitions, num_types, num_leaps. All uses changed. --- time/tzfile.c | 217 +++++++++++++++++++++++++------------------------- 1 file changed, 109 insertions(+), 108 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 5a7514e466..dcd2bcc96d 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -40,17 +40,17 @@ static __time64_t tzfile_mtime; struct ttinfo { - int offset; /* Seconds east of GMT. */ - unsigned char isdst; /* Used to set tm_isdst. */ + int utoff; /* Seconds east of UT. */ + unsigned char dst; /* Used to set tm_isdst. */ unsigned char idx; /* Index into `zone_names'. */ unsigned char isstd; /* Transition times are in standard time. */ - unsigned char isgmt; /* Transition times are in GMT. */ + unsigned char isutc; /* Transition times are in UT. */ }; struct leap { - __time64_t transition; /* Time the transition takes effect. */ - int change; /* Seconds of correction to apply. */ + __time64_t occur; /* Time the transition takes effect. */ + int corr; /* Seconds of correction to apply. */ }; /* Internal index derived from a count taken from a TZif file. @@ -59,14 +59,14 @@ struct leap code ordinarily should not assume it is unsigned. */ typedef unsigned int tzidx; -static tzidx num_transitions; +static tzidx timecnt; static __time64_t *transitions; static unsigned char *type_idxs; -static tzidx num_types; +static tzidx typecnt; static struct ttinfo *types; static char *zone_names; static long int rule_stdoff; -static tzidx num_leaps; +static tzidx leapcnt; static struct leap *leaps; static char *tzspec; @@ -142,10 +142,10 @@ void __tzfile_read (const char *file) { static const char default_tzdir[] = TZDIR; - tzidx num_isstd, num_isgmt; + tzidx isstdcnt, isutcnt; FILE *f; union { struct tzhead tzhead; tzidx tzidx_aligned; } u; - tzidx chars; + tzidx charcnt; tzidx i; int was_using_tzfile = __use_tzfile; int trans_width = 4; @@ -227,15 +227,15 @@ __tzfile_read (const char *file) goto lose; f_offset += sizeof u.tzhead; - num_transitions = tzidx_decode (u.tzhead.tzh_timecnt); - num_types = tzidx_decode (u.tzhead.tzh_typecnt); - chars = tzidx_decode (u.tzhead.tzh_charcnt); - num_leaps = tzidx_decode (u.tzhead.tzh_leapcnt); - num_isstd = tzidx_decode (u.tzhead.tzh_ttisstdcnt); - num_isgmt = tzidx_decode (u.tzhead.tzh_ttisutcnt); + timecnt = tzidx_decode (u.tzhead.tzh_timecnt); + typecnt = tzidx_decode (u.tzhead.tzh_typecnt); + charcnt = tzidx_decode (u.tzhead.tzh_charcnt); + leapcnt = tzidx_decode (u.tzhead.tzh_leapcnt); + isstdcnt = tzidx_decode (u.tzhead.tzh_ttisstdcnt); + isutcnt = tzidx_decode (u.tzhead.tzh_ttisutcnt); - if (__glibc_unlikely (num_isstd > num_types || num_isgmt > num_types - || !num_types)) + if (__glibc_unlikely (isstdcnt > typecnt || isutcnt > typecnt + || !typecnt)) goto lose; if (trans_width == 4 && u.tzhead.tzh_version[0] != '\0') @@ -246,14 +246,14 @@ __tzfile_read (const char *file) /* Position the stream before the second header. */ off_t to_skip, product; bool v = false; - v |= ckd_mul (&to_skip, num_transitions, 4 + 1); - v |= ckd_mul (&product, num_types, 6); + v |= ckd_mul (&to_skip, timecnt, 4 + 1); + v |= ckd_mul (&product, typecnt, 6); v |= ckd_add (&to_skip, to_skip, product); - v |= ckd_add (&to_skip, to_skip, chars); - v |= ckd_mul (&product, num_leaps, 8); + v |= ckd_add (&to_skip, to_skip, charcnt); + v |= ckd_mul (&product, leapcnt, 8); v |= ckd_add (&to_skip, to_skip, product); - v |= ckd_add (&to_skip, to_skip, num_isstd); - v |= ckd_add (&to_skip, to_skip, num_isgmt); + v |= ckd_add (&to_skip, to_skip, isstdcnt); + v |= ckd_add (&to_skip, to_skip, isutcnt); if (v) goto lose; @@ -271,15 +271,15 @@ __tzfile_read (const char *file) { off_t rem = st.st_size - f_offset, product; bool v = false; - v |= ckd_mul (&product, num_transitions, 8 + 1); + v |= ckd_mul (&product, timecnt, 8 + 1); v |= ckd_sub (&rem, rem, product); - v |= ckd_mul (&product, num_types, 6); + v |= ckd_mul (&product, typecnt, 6); v |= ckd_sub (&rem, rem, product); - v |= ckd_sub (&rem, rem, chars); - v |= ckd_mul (&product, num_leaps, 12); + v |= ckd_sub (&rem, rem, charcnt); + v |= ckd_mul (&product, leapcnt, 12); v |= ckd_sub (&rem, rem, product); - v |= ckd_sub (&rem, rem, num_isstd); - v |= ckd_sub (&rem, rem, num_isgmt); + v |= ckd_sub (&rem, rem, isstdcnt); + v |= ckd_sub (&rem, rem, isutcnt); v |= rem <= 1; v |= 3 <= rem && rem < sizeof "XYZ0\n"; if (v) @@ -292,11 +292,11 @@ __tzfile_read (const char *file) /* The file is parsed into a single heap allocation, comprising of the following arrays: - __time64_t transitions[num_transitions]; - struct leap leaps[num_leaps]; - struct ttinfo types[num_types]; - unsigned char type_idxs[num_types]; - char zone_names[chars]; + __time64_t transitions[timecnt]; + struct leap leaps[leapcnt]; + struct ttinfo types[typecnt]; + unsigned char type_idxs[typecnt]; + char zone_names[charcnt]; char tzspec[tzspec_size]; The piece-wise allocations from buf below verify that no @@ -314,13 +314,13 @@ __tzfile_read (const char *file) { size_t total_size, product; bool v = false; - v |= ckd_mul (&total_size, num_transitions, sizeof (__time64_t)); - v |= ckd_mul (&product, num_leaps, sizeof (struct leap)); + v |= ckd_mul (&total_size, timecnt, sizeof (__time64_t)); + v |= ckd_mul (&product, leapcnt, sizeof (struct leap)); v |= ckd_add (&total_size, total_size, product); - v |= ckd_mul (&product, num_types, sizeof (struct ttinfo)); + v |= ckd_mul (&product, typecnt, sizeof (struct ttinfo)); v |= ckd_add (&total_size, total_size, product); - v |= ckd_add (&total_size, total_size, num_transitions); /* type_idxs */ - v |= ckd_add (&total_size, total_size, chars); /* zone_names */ + v |= ckd_add (&total_size, total_size, timecnt); /* type_idxs */ + v |= ckd_add (&total_size, total_size, charcnt); /* zone_names */ v |= ckd_add (&total_size, total_size, tzspec_size); v |= ckd_add (&total_size, total_size, alignment_slop); if (v) @@ -333,11 +333,11 @@ __tzfile_read (const char *file) /* The address of the first allocation is already stored in the pointer transitions. */ - (void) alloc_buffer_alloc_array (&buf, __time64_t, num_transitions); - leaps = alloc_buffer_alloc_array (&buf, struct leap, num_leaps); - types = alloc_buffer_alloc_array (&buf, struct ttinfo, num_types); - type_idxs = alloc_buffer_alloc_array (&buf, unsigned char, num_transitions); - zone_names = alloc_buffer_alloc_array (&buf, char, chars); + (void) alloc_buffer_alloc_array (&buf, __time64_t, timecnt); + leaps = alloc_buffer_alloc_array (&buf, struct leap, leapcnt); + types = alloc_buffer_alloc_array (&buf, struct ttinfo, typecnt); + type_idxs = alloc_buffer_alloc_array (&buf, unsigned char, timecnt); + zone_names = alloc_buffer_alloc_array (&buf, char, charcnt); if (trans_width == 8) tzspec = alloc_buffer_alloc_array (&buf, char, tzspec_size); else @@ -347,7 +347,7 @@ __tzfile_read (const char *file) if (trans_width == 4) { - for (i = 0; i < num_transitions; i++) + for (i = 0; i < timecnt; i++) { union { unsigned char c[4]; tzidx tzidx_aligned; } x; if (__fread_unlocked (&x, 1, 4, f) != 4) @@ -359,10 +359,10 @@ __tzfile_read (const char *file) } else if (sizeof (int64_t) == 8) { - if (__fread_unlocked (transitions, 8, num_transitions, f) - != num_transitions) + if (__fread_unlocked (transitions, 8, timecnt, f) + != timecnt) goto lose; - for (i = 0; i < num_transitions; i++) + for (i = 0; i < timecnt; i++) { if (BYTE_ORDER != BIG_ENDIAN) transitions[i] = decode64 (&transitions[i]); @@ -372,7 +372,7 @@ __tzfile_read (const char *file) } else { - for (i = 0; i < num_transitions; i++) + for (i = 0; i < timecnt; i++) { union { unsigned char c[8]; int64_t int64_t_aligned; } x; if (__fread_unlocked (&x, 1, 8, f) != 8) @@ -383,18 +383,18 @@ __tzfile_read (const char *file) } } - if (__glibc_unlikely (__fread_unlocked (type_idxs, 1, num_transitions, f) - != num_transitions)) + if (__glibc_unlikely (__fread_unlocked (type_idxs, 1, timecnt, f) + != timecnt)) goto lose; /* Check for bogus indices in the data file, so we can hereafter safely use type_idxs[T] as indices into `types' and never crash. */ - for (i = 0; i < num_transitions; ++i) - if (__glibc_unlikely (type_idxs[i] >= num_types)) + for (i = 0; i < timecnt; ++i) + if (__glibc_unlikely (type_idxs[i] >= typecnt)) goto lose; unsigned char max_idx = 0; - for (i = 0; i < num_types; ++i) + for (i = 0; i < typecnt; ++i) { union { unsigned char c[4]; tzidx tzidx_aligned; } x; int c; @@ -403,32 +403,33 @@ __tzfile_read (const char *file) c = __getc_unlocked (f); if (__glibc_unlikely (! (0 <= c && c <= 1))) goto lose; - types[i].isdst = c; + types[i].dst = c; c = __getc_unlocked (f); - if (__glibc_unlikely (! (0 <= c && c < chars))) + if (__glibc_unlikely (! (0 <= c && c < charcnt))) /* Bogus index in data file. */ goto lose; if (max_idx < c) max_idx = c; types[i].idx = c; - types[i].offset = decode (&x); + types[i].utoff = decode (&x); /* If long int is only 32 bits, reject offsets that cannot be negated. RFC 9636 section 3.2 allows this. */ long int negated_offset; - if (ckd_sub (&negated_offset, 0, types[i].offset)) + if (ckd_sub (&negated_offset, 0, types[i].utoff)) goto lose; } - if (__glibc_unlikely (__fread_unlocked (zone_names, 1, chars, f) != chars)) + if (__glibc_unlikely (__fread_unlocked (zone_names, 1, charcnt, f) + != charcnt)) goto lose; - if (__strnlen (zone_names + max_idx, chars - max_idx) == chars - max_idx) + if (__strnlen (zone_names + max_idx, charcnt - max_idx) == charcnt - max_idx) goto lose; int minimum_leap_gap = 2419199; /* See RFC 9636 section 3.2. */ __time64_t prevtransition = -minimum_leap_gap; int prevchange = 0; - for (i = 0; i < num_leaps; ++i) + for (i = 0; i < leapcnt; ++i) { { union { char c[8]; tzidx tzidx_aligned; int64_t int64_t_aligned; } x; @@ -436,45 +437,45 @@ __tzfile_read (const char *file) != trans_width), 0)) goto lose; - leaps[i].transition = trans_width == 4 ? decode (&x) : decode64 (&x); - if (leaps[i].transition < 0 - || leaps[i].transition - minimum_leap_gap < prevtransition) + leaps[i].occur = trans_width == 4 ? decode (&x) : decode64 (&x); + if (leaps[i].occur < 0 + || leaps[i].occur - minimum_leap_gap < prevtransition) goto lose; - prevtransition = leaps[i].transition; + prevtransition = leaps[i].occur; } { union { unsigned char c[4]; tzidx tzidx_aligned; } x; if (__glibc_unlikely (__fread_unlocked (&x, 1, 4, f) != 4)) goto lose; - leaps[i].change = decode (&x); + leaps[i].corr = decode (&x); int delta_change; - if (ckd_sub (&delta_change, leaps[i].change, prevchange) + if (ckd_sub (&delta_change, leaps[i].corr, prevchange) || ! (delta_change == 1 || delta_change == -1)) goto lose; - prevchange = leaps[i].change; + prevchange = leaps[i].corr; } } - for (i = 0; i < num_isstd; ++i) + for (i = 0; i < isstdcnt; i++) { int c = __getc_unlocked (f); if (__glibc_unlikely (c == EOF)) goto lose; types[i].isstd = c != 0; } - while (i < num_types) + while (i < typecnt) types[i++].isstd = 0; - for (i = 0; i < num_isgmt; ++i) + for (i = 0; i < isutcnt; i++) { int c = __getc_unlocked (f); if (__glibc_unlikely (c == EOF)) goto lose; - types[i].isgmt = c != 0; + types[i].isutc = c != 0; } - while (i < num_types) - types[i++].isgmt = 0; + while (i < typecnt) + types[i++].isutc = 0; /* Read the POSIX TZ-style information if possible. */ if (tzspec != NULL) @@ -498,7 +499,7 @@ __tzfile_read (const char *file) fclose (f); /* First "register" all time zone abbreviations. */ - for (i = 0; i < num_types; ++i) + for (i = 0; i < typecnt; ++i) if (__tzstring (&zone_names[types[i].idx]) == NULL) goto ret_free_transitions; @@ -507,10 +508,10 @@ __tzfile_read (const char *file) transitioned to earliest in time. */ __tzname[0] = NULL; __tzname[1] = NULL; - for (i = num_transitions; i > 0; ) + for (i = timecnt; i > 0; ) { int type = type_idxs[--i]; - int dst = types[type].isdst; + int dst = types[type].dst; if (__tzname[dst] == NULL) { @@ -533,20 +534,20 @@ __tzfile_read (const char *file) __tzname[1] = __tzname[0]; daylight_saved = 0; - if (num_transitions == 0) + if (timecnt == 0) /* Use the first rule (which should also be the only one). */ - rule_stdoff = types[0].offset; + rule_stdoff = types[0].utoff; else { rule_stdoff = 0; /* Search for the last rule with a standard time offset. This will be used for the global timezone variable. */ - i = num_transitions - 1; + i = timecnt - 1; do - if (!types[type_idxs[i]].isdst) + if (!types[type_idxs[i]].dst) { - rule_stdoff = types[type_idxs[i]].offset; + rule_stdoff = types[type_idxs[i]].utoff; break; } else @@ -557,7 +558,7 @@ __tzfile_read (const char *file) information will be used to set the global daylight variable. */ while (i-- > 0 && !daylight_saved) - daylight_saved = types[type_idxs[i]].isdst; + daylight_saved = types[type_idxs[i]].dst; } __daylight = daylight_saved; @@ -588,13 +589,13 @@ __tzfile_compute (__time64_t timer, int use_localtime, __tzname[0] = NULL; __tzname[1] = NULL; - if (__glibc_unlikely (num_transitions == 0 || timer < transitions[0])) + if (__glibc_unlikely (timecnt == 0 || timer < transitions[0])) { /* TIMER is before any transition (or there are no transitions). Choose the first non-DST type (or the first if they're all DST types). */ i = 0; - while (i < num_types && types[i].isdst) + while (i < typecnt && types[i].dst) { if (__tzname[1] == NULL) __tzname[1] = __tzstring (&zone_names[types[i].idx]); @@ -602,14 +603,14 @@ __tzfile_compute (__time64_t timer, int use_localtime, ++i; } - if (i == num_types) + if (i == typecnt) i = 0; __tzname[0] = __tzstring (&zone_names[types[i].idx]); if (__tzname[1] == NULL) { tzidx j = i; - while (j < num_types) - if (types[j].isdst) + while (j < typecnt) + if (types[j].dst) { __tzname[1] = __tzstring (&zone_names[types[j].idx]); break; @@ -618,12 +619,12 @@ __tzfile_compute (__time64_t timer, int use_localtime, ++j; } } - else if (timer >= transitions[num_transitions - 1]) + else if (timer >= transitions[timecnt - 1]) { if (__glibc_unlikely (tzspec == NULL)) { use_last: - i = num_transitions; + i = timecnt; goto found; } @@ -645,18 +646,18 @@ __tzfile_compute (__time64_t timer, int use_localtime, /* Find the first transition after TIMER, and then pick the type of the transition before it. */ tzidx lo = 0; - tzidx hi = num_transitions - 1; + tzidx hi = timecnt - 1; /* Assume that DST is changing twice a year and guess initial search spot from it. Half of a gregorian year has on average 365.2425 * 86400 / 2 = 15778476 seconds. Although i's value can be wrong if overflow occurs, this is harmless because it is just a guess. */ __time64_t tdiff; - ckd_sub (&tdiff, transitions[num_transitions - 1], timer); + ckd_sub (&tdiff, transitions[timecnt - 1], timer); ckd_add (&i, tdiff / 15778476, 0); - if (i < num_transitions) + if (i < timecnt) { - i = num_transitions - 1 - i; + i = timecnt - 1 - i; if (timer < transitions[i]) { if (i < 10 || timer >= transitions[i - 10]) @@ -670,7 +671,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, } else { - if (num_transitions - i <= 10 || timer < transitions[i + 10]) + if (timecnt - i <= 10 || timer < transitions[i + 10]) { /* Linear search. */ while (timer >= transitions[i]) @@ -695,14 +696,14 @@ __tzfile_compute (__time64_t timer, int use_localtime, found: /* assert (timer >= transitions[i - 1] - && (i == num_transitions || timer < transitions[i])); */ - __tzname[types[type_idxs[i - 1]].isdst] + && (i == timecnt || timer < transitions[i])); */ + __tzname[types[type_idxs[i - 1]].dst] = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]); tzidx j = i; - while (j < num_transitions) + while (j < timecnt) { int type = type_idxs[j]; - int dst = types[type].isdst; + int dst = types[type].dst; int idx = types[type].idx; if (__tzname[dst] == NULL) @@ -730,16 +731,16 @@ __tzfile_compute (__time64_t timer, int use_localtime, { /* This should only happen if there are no transition rules. In this case there should be only one single type. */ - assert (num_types == 1); + assert (typecnt == 1); __tzname[0] = __tzstring (zone_names); } if (__tzname[1] == NULL) /* There is no daylight saving time. */ __tzname[1] = __tzname[0]; - tp->tm_isdst = info->isdst; + tp->tm_isdst = info->dst; assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0); tp->tm_zone = __tzname[tp->tm_isdst]; - tp->tm_gmtoff = info->offset; + tp->tm_gmtoff = info->utoff; } leap: @@ -747,17 +748,17 @@ __tzfile_compute (__time64_t timer, int use_localtime, *leap_hit = false; /* Find the last leap second correction transition time before TIMER. */ - i = num_leaps; + i = leapcnt; do if (i-- == 0) return; - while (timer < leaps[i].transition); + while (timer < leaps[i].occur); /* Apply its correction. */ - *leap_correct = leaps[i].change; + *leap_correct = leaps[i].corr; - if (timer == leaps[i].transition /* Exactly at the transition time. */ - && (leaps[i].change > (i == 0 ? 0 : leaps[i - 1].change))) + if (timer == leaps[i].occur /* Exactly at the transition time. */ + && (leaps[i].corr > (i == 0 ? 0 : leaps[i - 1].corr))) *leap_hit = true; } From patchwork Sun Jan 5 05:56:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104044 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 38A743858D3C for ; Sun, 5 Jan 2025 06:07:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38A743858D3C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=Kx4QNpNi X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id CA41C3858D35 for ; Sun, 5 Jan 2025 05:59:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA41C3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CA41C3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056742; cv=none; b=yA5oS6yCjrCHdKnM/HsKULfULSMtScxW7z7pSDfiLr0D7lGVO+mvUu9j+dfuyJpZIK2Oir7aoypp5Hb1yVQxxRiq2jTIvpqY1ZOgdAfV83/JabDInrPZGbZ0rCdQiN2aUn2XZ4YhlWSfgs6zD8+deBbeMn+eskFGFP4+lzah0fw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056742; c=relaxed/simple; bh=0MtylsOBAEEqEWugPZYd/8cZeqmuczwiWpLHVjMRWwI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZM8flv2y8hG6gnz8P/B4g+9u7lXQEkGFjEAcEwV+3LZ9eSarX85tcbduExfhBzDnPQVOlj545nBvtbm161aVB+fY/9uhqYec6ldXYwQAPzNoZTLGyyMQ09CdVZ+FZN8kC0aIsl1HUCRGJi1VY8c6kcAO1W3Nd/8wBpA3LME+LWo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA41C3858D35 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 4FFF93C123845 for ; Sat, 4 Jan 2025 21:59:02 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 2NB2Ca9vpcFT; Sat, 4 Jan 2025 21:59:02 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 0AE0B3C123844; Sat, 4 Jan 2025 21:59:02 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 0AE0B3C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056742; bh=0AuSdk3yRVF1XnsMMNbW61RNQsu9tNnrMF/3mvS6YlM=; h=From:To:Date:Message-ID:MIME-Version; b=Kx4QNpNiQT1s/oPqSIShENhRufDcHyrym2RxM/6z6v/zwA8RuhDFk6yy+9csa4Pxs YPJtO73ydpMkx4HApUfRmIoCeLcauL9AJ4GHmLmiPhE9ZM2b/6q5o7ayZfnlYg9fln mnQH4S72cbQN+EgGF8IrNCK8dOvh0afZErp0aD2JOxqq3Ru2uLcVzZEIFmLgZJywSj yJxNHdniqt1XusdWiBptAAptJBXz3SaWBk0mkNW34CdYimuD3VZHDBZpADI62nvV+S QlsLWERfBFTPfBYVRZ3pbUU9xlczV4idBZ/oO3eNanGlBK6aWP3pxrdFwu4O5X6hM4 LPHfWyvANu4rg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id gu-m7Ixd23By; Sat, 4 Jan 2025 21:59:01 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id DEE363C082EB9; Sat, 4 Jan 2025 21:59:01 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 19/59] Trivial tzset_internal speedup Date: Sat, 4 Jan 2025 21:56:54 -0800 Message-ID: <20250105055750.1668721-20-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (tzset_internal): Omit useless initialization. All that matters is that the two change values are equal, which they are because they’ve just been zeroed. --- time/tzset.c | 1 - 1 file changed, 1 deletion(-) diff --git a/time/tzset.c b/time/tzset.c index 7cc675334a..8f7315dabe 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -430,7 +430,6 @@ tzset_internal (int always) tz_rules[0].name = tz_rules[1].name = "UTC"; if (J0 != 0) tz_rules[0].type = tz_rules[1].type = J0; - tz_rules[0].change = tz_rules[1].change = -1; update_vars (); return; } From patchwork Sun Jan 5 05:56:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104054 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 402C93858C54 for ; Sun, 5 Jan 2025 06:14:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 402C93858C54 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=pz+t0yyq X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 546AA3858C42 for ; Sun, 5 Jan 2025 05:59:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 546AA3858C42 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 546AA3858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056750; cv=none; b=HlR3zthxBne6l9uI8GhYJARHy2RbjGAS8zDJF3Nj8I6BrVPh8ba7ZfFtrnvcwhyg2DEc3P8m5tIFKm8oZ9bhhai8tGgqzkDCFiLapDjQccPGYxx/6UWAvHLnSbxFr3p1DFpaCJPIVBULy9dyn+IC1zaMdBp3OtnUqKAI25Bvs9M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056750; c=relaxed/simple; bh=NozrKb4yDIr+EL5pUtPWjjysAlNkrupbLhvc60oUf+g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aiwr+8s97vDmy0W4LI7C3J1KTxBNziQcGzEJ6Fiua3+ybgNqWJtYF1D6P8kljs6l7LaDb1S2uUIh++mHqFrqg1MVX8xNPEaZGh95HsOq8LtONpy8nk5dunWIlK21rcD4WQLXczIoUeRJCFRIB7CX7pZU7FBD2bVNa/WD+JkMx5U= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 546AA3858C42 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id CBE4F3C123847 for ; Sat, 4 Jan 2025 21:59:04 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 2LeEGSGzFNAT; Sat, 4 Jan 2025 21:59:04 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 6A3FA3C123844; Sat, 4 Jan 2025 21:59:04 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 6A3FA3C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056744; bh=7LXJhuF0yLw0rEdW2GpbFAE5tXZkLSgChXlV7fKceyA=; h=From:To:Date:Message-ID:MIME-Version; b=pz+t0yyqyT3xkpdacolw6g07h07wYaBRyeiFGG4NU0If73XxsjD8b1Nq5U3hVE9T1 mZVpTu3w0S8fuQnpM8RSqzrM1/z8pZGU4qjDvOAxYB3BfmqlIXMZlWahg/mVC6Ok4v 8oJhf2T6Kk9CNqMJ+7ZbNmElLd8zwnua2KFCZVTWqGLILNQ/3WYdFllfDHVPx9h+ZY Xg6QHGG/DQa7E77OimQYxNU9UGVmfA9JLF64c2iX7uZN4l+FcyH6OMPHhZU5Hl3Nlq A9mFoZJ+QP4jAq0qS5IPdF/jQU7/UZOmtQHvuhIqPOkwXYqiT4q3bvI6NXeDsxpX9k hfJKrRZr7TCMA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id G5O-M8kRmwsi; Sat, 4 Jan 2025 21:59:04 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 5364F3C082EB9; Sat, 4 Jan 2025 21:59:04 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 20/59] Fix obscure bug when stdoffset == dstoffset Date: Sat, 4 Jan 2025 21:56:55 -0800 Message-ID: <20250105055750.1668721-21-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tst-tzname.c (do_test): Test odd (but allowed by POSIX) zone that has the same UTC offset in both standard time and DST. * time/tzset.c (enum tz_rule_type): This type now has an enum tag. (NO_DST): New value for the type. (update_vars): Set __daylight depending the type, not on whether offsets differ. (parse_rule): Do not set rule type unless successful. (tzset_internal): Default rules to NO_DST, not to J0. (compute_change): Do not adjust if NO_DST. --- time/tst-tzname.c | 25 +++++++++++++++++++++++++ time/tzset.c | 19 ++++++++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/time/tst-tzname.c b/time/tst-tzname.c index 5009328e85..b9b4aea35e 100644 --- a/time/tst-tzname.c +++ b/time/tst-tzname.c @@ -43,6 +43,31 @@ do_test (void) printf ("FAIL: TZ=%s, tzname[0] = %s\n", TZDEFRULES, tzname[0]); result = 1; } + + static char const oddzone[] = "XST12XST12,M9.5.0,M4.1.0"; + setenv ("TZ", oddzone, 1); + tzset (); + if (strcmp (tzname[0], "XST") != 0) + { + printf ("FAIL: TZ=%s, tzname[0] = %s\n", oddzone, tzname[0]); + result = 1; + } + if (strcmp (tzname[1], "XST") != 0) + { + printf ("FAIL: TZ=%s, tzname[1] = %s\n", oddzone, tzname[1]); + result = 1; + } + if (daylight <= 0) + { + printf ("FAIL: TZ=%s, daylight = %d\n", oddzone, daylight); + result = 1; + } + if (timezone != 12 * 60 * 60) + { + printf ("FAIL: TZ=%s, timezone = %ld\n", oddzone, timezone); + result = 1; + } + return result; } diff --git a/time/tzset.c b/time/tzset.c index 8f7315dabe..e001a5898c 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -47,7 +47,7 @@ typedef struct const char *name; /* When to change. */ - enum { J0, J1, M } type; /* Interpretation of: */ + enum tz_rule_type { NO_DST, J0, J1, M } type; /* Interpretation of: */ unsigned short int m, n, d; /* Month, week, day. */ int secs; /* Time of day. */ @@ -128,7 +128,7 @@ static char *old_tz; static void update_vars (void) { - __daylight = tz_rules[0].offset != tz_rules[1].offset; + __daylight = tz_rules[1].type != NO_DST; __timezone = -tz_rules[0].offset; __tzname[0] = (char *) tz_rules[0].name; __tzname[1] = (char *) tz_rules[1].name; @@ -267,6 +267,7 @@ parse_rule (const char **tzp, int whichrule) { const char *tz = *tzp; tz_rule *tzr = &tz_rules[whichrule]; + enum tz_rule_type type; /* Ignore comma to support string following the incorrect specification in early POSIX.1 printings. */ @@ -276,7 +277,7 @@ parse_rule (const char **tzp, int whichrule) bool is_J = *tz == 'J'; if (is_J || isdigit (*tz)) { - tzr->type = is_J ? J1 : J0; + type = is_J ? J1 : J0; tz += is_J; int d = parse_int (&tz); if (! (is_J <= d && d <= 365)) @@ -285,7 +286,7 @@ parse_rule (const char **tzp, int whichrule) } else if (*tz == 'M') { - tzr->type = M; + type = M; tz++; int i = parse_int (&tz); if (! (1 <= i && i <= 12 && *tz++ == '.')) @@ -308,7 +309,7 @@ parse_rule (const char **tzp, int whichrule) of 2005 [Pub. L. no. 109-58, 119 Stat 594 (2005)]. Below is the equivalent of "M3.2.0,M11.1.0" [/2 not needed since 2:00AM is the default]. */ - tzr->type = M; + type = M; if (tzr == &tz_rules[0]) { tzr->m = 3; @@ -341,6 +342,7 @@ parse_rule (const char **tzp, int whichrule) secs = 2 * 60 * 60; tzr->secs = secs; + tzr->type = type; tzr->computed_for = LLONG_MIN; *tzp = tz; return true; @@ -428,8 +430,8 @@ tzset_internal (int always) { memset (tz_rules, '\0', sizeof tz_rules); tz_rules[0].name = tz_rules[1].name = "UTC"; - if (J0 != 0) - tz_rules[0].type = tz_rules[1].type = J0; + if (NO_DST != 0) + tz_rules[0].type = tz_rules[1].type = NO_DST; update_vars (); return; } @@ -461,6 +463,9 @@ compute_change (tz_rule *rule, long long int year) switch (rule->type) { + case NO_DST: + break; + case J1: /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years. In non-leap years, or if the day number is 59 or less, just From patchwork Sun Jan 5 05:56:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104060 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 825B2385840C for ; Sun, 5 Jan 2025 06:18:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 825B2385840C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=BhnXqMIi X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 955E33858C5F for ; Sun, 5 Jan 2025 05:59:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 955E33858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 955E33858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056752; cv=none; b=OY2VgJpu8eAIaUt573lTRxjYJfviqmNwPUEUHDa1CW4ij26AluhIuxAEwM6dIDo0gWPSNJ2C50S4dVry1twt4UXFBdPGdrw7+Bh0ytq/L8bi+wUn1B4W2aH2pnKsibQx07bECPLFBTHcTeFBzAig4RJmFxnprk74noPim3vxx2k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056752; c=relaxed/simple; bh=YsEoliBTfa7QZIWNib7a6NFienxGdK5/iCHqLqBqsoA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DtzWguWzwF8mLS4ljcphphaZ2MWKYPtqsMzBMqPliglbeJx843glhq/caaPlxZCA/AeYWCx42l7LCC/myJ2stOhffQPmkC2r9YQB21W2RhC3DWDM5w4lmIx7g8u1E9w3FNyg3WqtWZlvp6KpQN7ScLdTtLxzSAp366+d7lxAaIE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 955E33858C5F Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 1A4EF3C123844 for ; Sat, 4 Jan 2025 21:59:07 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id MhmA4ES9oP9M; Sat, 4 Jan 2025 21:59:06 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D413A3C123843; Sat, 4 Jan 2025 21:59:06 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu D413A3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056746; bh=zn6A1gsAOUxBbkI37+Fa4pvN+lIFWzqjRAhPvYLFcMc=; h=From:To:Date:Message-ID:MIME-Version; b=BhnXqMIiHwPak83bU6pORt5YYCQspzQNYFLPW54cBuaFolrZ1kDS7FIGUP37XxLdM zyZBSmd+P3J8/v3HsNiDpxG4HDcJn7c32FmwAtOQBlwkxjbGucKB79fuprVPZKAs6W HOO98sHEokfalNRbTB4Ga7DcQ6v91IXagKBmpPLKdO+hsFzCiSuT+Mq622kCoIJtyc maxm/bb2scDJVHVIqc7bE8/5N+nD+e6VK2mt4+Ck6RmoOO+W/dOhZVoU+ZzHOhJoib 9Lx8QuyFqTebulmLWM0N+GyjNIFEHJ5f4UYsVwpGBNaFBSsMDFj0LuLm6BQmpg6ZFZ 9EeW6PPeGDkew== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id Q9oyNB09cPeH; Sat, 4 Jan 2025 21:59:06 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id B3EDC3C082EB9; Sat, 4 Jan 2025 21:59:06 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 21/59] Document assumption that NO_DST == 0 Date: Sat, 4 Jan 2025 21:56:56 -0800 Message-ID: <20250105055750.1668721-22-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_NUMSUBJECT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (__tzset_parse_tz): Document assumption that NO_DST is zero here, just as it is already documentted in tzset_internal. This does not change the generated code when optimizing. --- time/tzset.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/time/tzset.c b/time/tzset.c index e001a5898c..a198b180eb 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -354,6 +354,8 @@ __tzset_parse_tz (const char *tz) { /* Clear out old state and reset to unnamed UTC. */ memset (tz_rules, '\0', sizeof tz_rules); + if (NO_DST != 0) + tz_rules[0].type = tz_rules[1].type = NO_DST; tz_rules[0].name = tz_rules[1].name = ""; /* Get the standard time zone abbreviations. */ From patchwork Sun Jan 5 05:56:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104048 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 699A13858408 for ; Sun, 5 Jan 2025 06:10:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 699A13858408 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=Jg87prQX X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 0029F3858C78 for ; Sun, 5 Jan 2025 05:59:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0029F3858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0029F3858C78 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056750; cv=none; b=JIO3EHOEncVMkFVO5hryuGA5jALx4WvF9od+0lSIYES5JhzcLqHHsjNLc31LEdzljYEwmunY0X2XqPBcVRY6voAqZ/fRPQJFJnSKKw7Z6+lrBwxYxDBeKiWuwOTcRN0xYlifsGCUuK2lQKksG8jYauewvwe09EffZgQeXjPwcMI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056750; c=relaxed/simple; bh=IZBgqVuZRULUUm03ZvcCHvDD+fAlVlwMV/6aLf6aohw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EGwbFZTJNCNmcAip7WY+U5UuD4EhnNxsaMp9RyKHDY+TT7JYFeVXWQeWeWwiu3bOx+rWZubQakjcVwrRBKQEwy8gIT0AOUVcSEzzs5faP4/WprRiFHxkiEisOKq368cvKxSApvEiipxWLYa5j1oAlsxFcnhQwbCAg5Kv7dENckU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0029F3858C78 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 7948C3C123845 for ; Sat, 4 Jan 2025 21:59:09 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id il1E18O8WNIo; Sat, 4 Jan 2025 21:59:09 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 385833C123843; Sat, 4 Jan 2025 21:59:09 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 385833C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056749; bh=7lPz9ySSOTkms6DdeDgIP/a7FKFWqetfZWEBZL9tieg=; h=From:To:Date:Message-ID:MIME-Version; b=Jg87prQXPVZkLxJv5FN3Q/dvW2SHuOGPm8gUaUjN8pSWPuH+WHDksoL0IVcORR6LR RxjohdqM5KjJF9a8nuDTXUtkfb4+YiR1fWZQaSeSFAFGiTFPYfxLCFuKz6vP2wEe+V 1BoEEg3oHRbG68IO7Moh5ZV++vI02FyKkZ9SfuxFQwptQAc3SsU0PRxVPzqC9J4Gyc pd292y4f4Ng2uQwQjoMoEj5q6FjubWqI+HqO2N2Ru+sWOTI3SVADtpdQhcENL88meP nav3o0goavFIfDkhcBIRSHMKhRYpdhw3J2aDhOR+Sz2TuKQuUfdW7WPg9qqFpgbB1r 82v9/blUtYujw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id RpS3Vf8Ohvs8; Sat, 4 Jan 2025 21:59:09 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 215B73C082EB9; Sat, 4 Jan 2025 21:59:09 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 22/59] __use_tzfile is boolean Date: Sat, 4 Jan 2025 21:56:57 -0800 Message-ID: <20250105055750.1668721-23-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__use_tzfile): Now bool instead of int. --- time/tzfile.c | 8 ++++---- time/tzset.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index dcd2bcc96d..8d8391acfe 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -33,7 +33,7 @@ #include -int __use_tzfile; +bool __use_tzfile; static dev_t tzfile_dev; static ino64_t tzfile_ino; static __time64_t tzfile_mtime; @@ -147,14 +147,14 @@ __tzfile_read (const char *file) union { struct tzhead tzhead; tzidx tzidx_aligned; } u; tzidx charcnt; tzidx i; - int was_using_tzfile = __use_tzfile; + bool was_using_tzfile = __use_tzfile; int trans_width = 4; char *new = NULL; _Static_assert (sizeof (__time64_t) == 8, "__time64_t must be eight bytes"); - __use_tzfile = 0; + __use_tzfile = false; if (file == NULL) /* No user specification; use the site-wide default. */ @@ -565,8 +565,8 @@ __tzfile_read (const char *file) __timezone = -rule_stdoff; done: - __use_tzfile = 1; free (new); + __use_tzfile = true; return; lose: diff --git a/time/tzset.h b/time/tzset.h index 8c87ad6a24..3687c044e3 100644 --- a/time/tzset.h +++ b/time/tzset.h @@ -8,7 +8,7 @@ /* Defined in tzset.c. */ extern char *__tzstring (const char *string) attribute_hidden; -extern int __use_tzfile attribute_hidden; +extern bool __use_tzfile attribute_hidden; extern void __tzfile_read (const char *file) attribute_hidden; extern void __tzfile_compute (__time64_t timer, int use_localtime, From patchwork Sun Jan 5 05:56:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104066 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 55F22385841C for ; Sun, 5 Jan 2025 06:20:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 9481F3858D38 for ; Sun, 5 Jan 2025 05:59:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9481F3858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9481F3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056757; cv=none; b=TDXs8q0xJtsg4LGdpKlOxgPW45ATOp/L5ytZQzeYN+fIwv6YzznoD6KwkElzctH4y3RwUKTMXNQ4jnT6eYdYIWXB/Vt8bWRU0Ct3HPk0dftGN+aRbShHxJnS3L8bwr6G3ROGBVWgeJdSQyvpPLOIT49wCQrC8Ls8L+oI2nBnsgY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056757; c=relaxed/simple; bh=+HP8a+wGPnE/fmJgyS3DZu2Gh6mDYQazYGbY5VLnVew=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PYUM+Ft3hCmYwxzC4A792/CUQIkAybqSmyHjvfiHXiHa8lVjxqTK/a8CUEGnW69cBNEUUEMzSe4ABd+KhRzzRBEPScQnZpWF5WsdP9bnWyRbmWn0AnzuqeVDxjMRMCDtDkejxRP46HMqDdxzETOBgYZpk/kTdJTCc+bRhioBNEk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9481F3858D38 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=ckOZwogX Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 175CB3C123848 for ; Sat, 4 Jan 2025 21:59:12 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id d-2hKM7l-CKU; Sat, 4 Jan 2025 21:59:11 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id C2E423C123843; Sat, 4 Jan 2025 21:59:11 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu C2E423C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056751; bh=Ongf2UJYbhQm5fEd8qmaXoLGhtvl16VnOdpJLSZblEk=; h=From:To:Date:Message-ID:MIME-Version; b=ckOZwogXsfiEr4kog5Fz8Zq6aa3lJC7KYOHJAc8D20tjlLwVWG9MVyed/TgLC8N0H qYOlfFe3x23/hLh/WsU3YJFPcPS+ElXwCIhUokJgLbw+IQFUTU0iKZOkYBB/R0g9zs zgaQ0iIhPNi+X9hBpZubAladfQihQeHrcmeh1nbZaYqZIsBtCNHG1Zc/Fyo98nTYjO IUvcTjrvzITEeimNQT/jYbfi2aRsLhxfZIpa41V6EjLz5e80sCcqi072A9/AdIHGZz pZ/8YTk4SjHq2X26CtXga2BMrwXaNCxQjl1J+jFWgKjBIOj4mEfmnVMMvuvU7LQPZM 5w2mzbco7Wm+A== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id f_oQEk-kGJCR; Sat, 4 Jan 2025 21:59:11 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id ABC563C082EB9; Sat, 4 Jan 2025 21:59:11 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 23/59] Minor tzfile.c clarity improvements Date: Sat, 4 Jan 2025 21:56:58 -0800 Message-ID: <20250105055750.1668721-24-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read, __tzfile_compute): Avoid some unnecesary unsigned char -> int conversions. Improve clarity of loop controlling. --- time/tzfile.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 8d8391acfe..291b3aa0dd 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -508,16 +508,14 @@ __tzfile_read (const char *file) transitioned to earliest in time. */ __tzname[0] = NULL; __tzname[1] = NULL; - for (i = timecnt; i > 0; ) + for (i = timecnt; i > 0; i--) { - int type = type_idxs[--i]; - int dst = types[type].dst; + struct ttinfo *ttype = &types[type_idxs[i - 1]]; + unsigned char dst = ttype->dst; if (__tzname[dst] == NULL) { - int idx = types[type].idx; - - __tzname[dst] = __tzstring (&zone_names[idx]); + __tzname[dst] = __tzstring (&zone_names[ttype->idx]); if (__tzname[1 - dst] != NULL) break; @@ -699,22 +697,19 @@ __tzfile_compute (__time64_t timer, int use_localtime, && (i == timecnt || timer < transitions[i])); */ __tzname[types[type_idxs[i - 1]].dst] = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]); - tzidx j = i; - while (j < timecnt) + + for (tzidx j = i; j < timecnt; j++) { - int type = type_idxs[j]; - int dst = types[type].dst; - int idx = types[type].idx; + struct ttinfo *ttype = &types[type_idxs[j]]; + unsigned char dst = ttype->dst; if (__tzname[dst] == NULL) { - __tzname[dst] = __tzstring (&zone_names[idx]); + __tzname[dst] = __tzstring (&zone_names[ttype->idx]); if (__tzname[1 - dst] != NULL) break; } - - ++j; } if (__glibc_unlikely (__tzname[0] == NULL)) From patchwork Sun Jan 5 05:56:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104074 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2C4D3858C3A for ; Sun, 5 Jan 2025 06:26:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2C4D3858C3A Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=gMx0D/hF X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 25AD83858CD1 for ; Sun, 5 Jan 2025 05:59:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 25AD83858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 25AD83858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056755; cv=none; b=WrtpIrIvduVyFTOwnJ8ozZEsV/Any1pUyY8i7Mcwl1Doy4lhyREhpc98eIxLDXEkg9H/peT715EJLquFvn7BGA0bClVoeMnbE1741XwZ2qjqxS7iw7U2NtC5QNAOwU5xcVaguPTyFGcH1XrJPaxCXGkm45VpCPsKcFrbYaOCI48= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056755; c=relaxed/simple; bh=UsYpeI/9s4KSySTAk1kXTOSZUaEaEpO+R2Uozinmmpo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=x8IsddLqmwOATCLUNiRfSKdvCidszPpjgseJUrRafITAM7b0My5ygmiTmpD5EPlFC1urdmvUuKXkTR6d2r47oLSSxS9FG0WtNXILjThm/fcMLbdG8tfT+6N3GPgYk0pbzrMB09vVKTPaAV44WYyP5QBlCeOtcp7BAbJ3B/mokc0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25AD83858CD1 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 978083C123843 for ; Sat, 4 Jan 2025 21:59:14 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 2FlY0Qpw26mv; Sat, 4 Jan 2025 21:59:14 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 578D93C123845; Sat, 4 Jan 2025 21:59:14 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 578D93C123845 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056754; bh=oc140CupW8dw0oUiNIYsldZT2IiDWWSw1dBhcWgQAXI=; h=From:To:Date:Message-ID:MIME-Version; b=gMx0D/hFGvnD7W7iGUNUWwSbKQLv7nePBJ6LkaoBwvLNly6eaegPQFwEvuo1K00Iq 0cZnTHs8rfZgKopnnJxVl/gi2UNpLZVzwP1gkcUGwA7s1Pr0b3q8EY7NmAiwqsbI6l 6AhLSi/2j7SSPJ/h5jZ/4THpQQA2LPrZjt48aDQeP1zPrRWI5j35ljvEqp2QvQo4+N xm4B4AB4mpsnGs4alXg3pof3vOu1q5feXu76opu8SeWN8DLd0uH+mWZbvPrkC3zyt4 PPg34L+9SwdO/+YfGqIdO2VeZGK287Gz8UevOOvboGYvov9FoH2TxZm9PhEJ1XYGTk UaWEKBEIKPYWw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id ESIHck8kI6kT; Sat, 4 Jan 2025 21:59:14 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 36A2C3C082EB9; Sat, 4 Jan 2025 21:59:14 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 24/59] Stop using zic's -y option Date: Sat, 4 Jan 2025 21:56:59 -0800 Message-ID: <20250105055750.1668721-25-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * timezone/Makefile (build-testdata): Omit -y option, as current zic merely warns and discards it. (zic-deps): Omit yearistype. --- timezone/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/timezone/Makefile b/timezone/Makefile index 6e28a2e670..0c55b72f3f 100644 --- a/timezone/Makefile +++ b/timezone/Makefile @@ -103,7 +103,7 @@ tst-bz28707-ENV = TZDIR=$(testdata) tst-bz29951-ENV = TZDIR=$(testdata) # Note this must come second in the deps list for $(built-program-cmd) to work. -zic-deps = $(objpfx)zic $(leapseconds) yearistype +zic-deps = $(objpfx)zic $(leapseconds) $(testdata)/America/New_York: northamerica $(zic-deps) $(build-testdata) From patchwork Sun Jan 5 05:57:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104076 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4ECAE3858D35 for ; Sun, 5 Jan 2025 06:30:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4ECAE3858D35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=YavC+icr X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 980D03858408 for ; Sun, 5 Jan 2025 05:59:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 980D03858408 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 980D03858408 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056757; cv=none; b=dH7GGP1+dcVwkIK3My9Y292xqAlbJzx83WgQORTtyG1DE8D4fMHrraNz8cRNNRXtu1cPoCRMMbqUeFhvPr8Oa83WTGN5j7lthGTZhsXR7Mg9zEYbKQoL8ry7Gd/7+NHaqw64mxahkA1D7sX5bMy5D3L0I6WSglAjas2EIMs80YA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056757; c=relaxed/simple; bh=LPPWFbKfcDD4VayXqeVMAt0CZoXoU5Xs3WWVpSySso0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=r0GxYG9z8H0UnWCLcF6ZEX22BF6oWG9whUcTQfkJyT1POnGu5tTeUT/D9mg91rNyffFVhpQgHUjz9OfIOPMfwmCU3oFQ2QDW9QmwP0ys55T9qUzB9MMZI+v6bByr0q5lrFOtvJwJa9luaCXFLD3tcpVpprz0Qsehw04i4Br+dnk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 980D03858408 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 1C41B3C123845 for ; Sat, 4 Jan 2025 21:59:17 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id UZmlT9EqhiuC; Sat, 4 Jan 2025 21:59:16 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id B147B3C123844; Sat, 4 Jan 2025 21:59:16 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu B147B3C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056756; bh=L280qU0/OIeXL1MFshoH+H8ln1M3TGrCdaqtK4+RTCQ=; h=From:To:Date:Message-ID:MIME-Version; b=YavC+icrNi1ZT03FZEp1Y9aFaRQWpK9X/3tw0XWz17Da7dF0tmlc19h4+MhJylRE/ AUqGMrfArx7DrRRLs2rj0vf0Iyn3Kl0C5+RrWmeMk2gjKQDYgt7XKU31/m8kOcLaZf D/RN6QoexWm8jEUoDs3jLuPU0+E0ZVrxt7W1NgbSFQHYmKesa74IOHq1IjWZj4Vh88 av4AwmoP0c2cd7y6/uP2ny1FkQhhB6gWd0W6tRlFZgVOPkAewf5ecu3x7pKGznejVm XL/Uqwsrt44LW+r9yxBFmLb0RGz4P/Asl04GcG0haF1xkhJNDaBJEvjkZAL89AtBex aVrjSynFk/yJw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 8pBCQXb6WFQW; Sat, 4 Jan 2025 21:59:16 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 9AEED3C082EB9; Sat, 4 Jan 2025 21:59:16 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 25/59] Fix mishandling of default DST rule Date: Sat, 4 Jan 2025 21:57:00 -0800 Message-ID: <20250105055750.1668721-26-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org For timezone settings like TZ="AST4ADT" where POSIX says the default is implementation-defined, time/tzfile.c has long had vestiges of an old attempt to be upward compatible with UNIX System V. This code has not worked for decades and evidently is not being used, so remove it and use the simpler fallback code already present in time/tzset.c, which uses US DST. Also, add a few test cases to demonstrate some bugs in the removed code. * time/tst-posixtz.c: Include stdckdint.h. (tests): WHEN is now intmax_t, not time_t, to support skipping tests after 2038 on 32-bit time_t. All uses changed. Make it const while we're at it. Add several tests for AST4ADT. * time/tzfile.c (rule_dstoff): Remove. All uses removed. (__tzfile_read): Remove args EXTRA, EXTRAP. All uses changed. (__tzfile_default): Remove. All uses removed. (__tzfile_compute): Remove no-longer-needed code dealing with __tzfile_default. * timezone/Makefile (build-testdata): Add '-p $(posixrules)' option if compiling 'northamerica', so that we can test non-use of posixrules. * timezone/tst-timezone.c: Include stdbool.h, stdckdint.h. (failed): Now bool, not int. All uses changed. (do_test): Test AST4ADT too. --- Makeconfig | 16 +++++++--------- NEWS | 11 +++++------ manual/time.texi | 8 +++----- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Makeconfig b/Makeconfig index d0108d2caa..a4be5b0e21 100644 --- a/Makeconfig +++ b/Makeconfig @@ -285,19 +285,17 @@ ifndef leapseconds leapseconds = /dev/null endif -# What timezone's DST rules should be used when a POSIX-style TZ -# environment variable doesn't specify any rules. For 1003.1 compliance -# this timezone must use rules that are as U.S. federal law defines DST. -# Run `make -C time echo-zonenames' to see a list of available zone names. -# This setting can be changed with `zic -p TIMEZONE' at any time. -# If you want POSIX.1 compatibility, use `America/New_York'. +# What timezone's DST rules to use when testing that glibc does +# not mishandle TZif files built with zic's obsolescent -p option. +# This macro no longer affects glibc's code or installation. ifndef posixrules posixrules = America/New_York endif -# Where to install the "posixrules" timezone file; this is file -# whose contents $(posixrules) specifies. If this is a relative -# pathname, it is relative to $(zonedir). +# Where to put the "posixrules" timezone file used during testing; this +# is the file whose contents $(posixrules) specifies. +# If this is a relative pathname, it is relative to $(zonedir). +# Like posixrules, this macro no longer affects glibc's code or installation. ifndef posixrules-file posixrules-file = posixrules endif diff --git a/NEWS b/NEWS index 3e6227a448..a62fb8136d 100644 --- a/NEWS +++ b/NEWS @@ -84,12 +84,11 @@ Deprecated and removed features, and other changes affecting compatibility: explicitly because of the executable bit in GNU_STACK, and the stack is not already executable. Instead, loading such objects will fail. -* The default daylight saving rules for old Unix System V style TZ - strings like TZ="AST4ADT" are now those of current US DST. Although - the rules were supposed to be those of /usr/share/zoneinfo/posixrules, - this feature, which has been disabled by default and marked obsolete - upstream in tzcode, did not work in either glibc or tzcode, and in - practice posixrules invariably specified current US DST anyway. +* The US daylight saving rule is now the default for incomplete TZ + strings like TZ="AST4ADT" that use the TZ syntax from circa-1980s + Unix System V. Formerly the rule was documented to be that of + /usr/share/zoneinfo/posixrules but this did not work and in practice + posixrules invariably specified the US rule anyway. Changes to build and runtime requirements: diff --git a/manual/time.texi b/manual/time.texi index 28d8912446..cff94cb48b 100644 --- a/manual/time.texi +++ b/manual/time.texi @@ -2759,11 +2759,9 @@ and offset for the corresponding daylight saving time zone; if the The remainder of the proleptic format, which starts with the first comma, describes when daylight saving time is in effect. This remainder is -optional and if omitted, @theglibc{} defaults to US daylight saving rules. -rules that would be used if @env{TZ} had the value @t{"posixrules"}. -However, other POSIX implementations default to different daylight -saving rules, so portable @env{TZ} settings should not omit the -remainder. +optional and if omitted, @theglibc{} defaults to the US daylight saving rule. +POSIX does not specify this default, so portable @env{TZ} settings +should not omit the remainder. In the remainder, the @var{start} field is when daylight saving time goes into effect and the @var{end} field is when the change is made back to standard From patchwork Sun Jan 5 05:57:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104071 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BEDB43858C42 for ; Sun, 5 Jan 2025 06:22:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BEDB43858C42 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=EPh9m5pO X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 643CB385840A for ; Sun, 5 Jan 2025 05:59:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 643CB385840A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 643CB385840A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056760; cv=none; b=w4zDQyuIacATE+hr0ASUq1UHR24lkdzs5fjIqDjijVzEqAX6RkfIdAxMzZBQh9K24qSu/QR5sO6lrHOEuiXMsP1Y1y0y2KwJ7zZOPin8mOxcUu0GXiPM66RcfoQqsZ88HvXfYMSP6zESpTvmvSxntSP6y4bFCzouqSrUM7WVeVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056760; c=relaxed/simple; bh=anaP0uYnlsw14TJteeoFajShTCQ+BGWXawY5t7hkvnA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fjNMUYY+IdFRiampP7kpi9t4JjiuFzsC+3GEzJdll5VD/C/qQo3cI/lMRlWEVGQri2eAYNEZSjzCwmSVCM/GG8ch4TB7KG4U2dUfGY1rLNmeD3GWkXB6TKuoT0szV89fU4B0YrreNyHieyVioM3mzRLoySVxztN4YwAJkhAhbbQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 643CB385840A Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id DB6D83C123843 for ; Sat, 4 Jan 2025 21:59:19 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id XSwJaZEH45eI; Sat, 4 Jan 2025 21:59:19 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 86EC23C123847; Sat, 4 Jan 2025 21:59:19 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 86EC23C123847 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056759; bh=T0/U1Rf09TwtZMCOHe4AS6kLCUzz53FCRNXoo4V68Vk=; h=From:To:Date:Message-ID:MIME-Version; b=EPh9m5pOlD5Vj4MvkPUfXZnTTuYrWmE9m0GE3GSMm980fU+pKsJm2v9tgmOGlSXHB JNmgVlZl6wRR2mUU2kLKRNTGCqV/+Lr0+l7exKpK6IPsK0DcjIZuG4yO7BbHkvqXJc GvBWcS/4Dow/Yu0GK3lDaNhSgkg47eLdxizo8ZE7MqZs3CP1jN2uM3B+3EC5E7jaBR +XmWBNGS+UEN7ANIhs8eXqf90xeKNpQe4AzEdKkFC8/O46mnbG+X24jEZSaOQ6XYTc aXip+oGn9ptQE4n7BoS/bqVevL4eak+Rijv6orOZGg+igTkfS97vh5Xp72Mehq6ZgQ 1YPrybGJiRTFA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id aDTAWWHkmfGI; Sat, 4 Jan 2025 21:59:19 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 6F3DA3C082EB9; Sat, 4 Jan 2025 21:59:19 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 26/59] Simplify treatment of missing and empty TZ Date: Sat, 4 Jan 2025 21:57:01 -0800 Message-ID: <20250105055750.1668721-27-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Refactor to help the compiler. * time/tzfile.c (__tzfile_read): Assume the arg is a nonempty string. Caller changed. * time/tzset.c (tzset_internal): Simplify due to the fact that TZDEFAULT is not a null pointer (tzfile.c is already assuming this). Do not pass an empty string to __tzfile_read. --- time/tzfile.c | 33 ++++++++++++--------------------- time/tzset.c | 24 ++++++++++++++---------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 291b3aa0dd..08cf0686df 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -156,28 +156,19 @@ __tzfile_read (const char *file) __use_tzfile = false; - if (file == NULL) - /* No user specification; use the site-wide default. */ - file = TZDEFAULT; - else if (*file == '\0') - /* User specified the empty string; use UTC with no leap seconds. */ + /* We must not allow to read an arbitrary file in a setuid + program. So we fail for any file which is not in the + directory hierarchy starting at TZDIR + and which is not the system wide default TZDEFAULT. */ + if (__libc_enable_secure + && ((*file == '/' + && strcmp (file, TZDEFAULT) != 0 + && (strncmp (file, default_tzdir, sizeof (default_tzdir) - 1) + != 0)) + || strstr (file, "../") != NULL)) + /* This test is certainly a bit too restrictive but it should + catch all critical cases. */ goto ret_free_transitions; - else - { - /* We must not allow to read an arbitrary file in a setuid - program. So we fail for any file which is not in the - directory hierarchy starting at TZDIR - and which is not the system wide default TZDEFAULT. */ - if (__libc_enable_secure - && ((*file == '/' - && strcmp (file, TZDEFAULT) != 0 - && (strncmp (file, default_tzdir, sizeof (default_tzdir) - 1) - != 0)) - || strstr (file, "../") != NULL)) - /* This test is certainly a bit too restrictive but it should - catch all critical cases. */ - goto ret_free_transitions; - } if (*file != '/') { diff --git a/time/tzset.c b/time/tzset.c index a198b180eb..b014bca8d3 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -394,25 +394,26 @@ tzset_internal (int always) /* Examine the TZ environment variable. */ tz = getenv ("TZ"); - if (tz && *tz == '\0') + + if (tz == NULL) + /* No user specification; use the site-wide default. */ + tz = TZDEFAULT; + + if (*tz == '\0') /* User specified the empty string; use UTC explicitly. */ tz = "Universal"; /* A leading colon means "implementation defined syntax". We ignore the colon and always use the same algorithm: try a data file, and if none exists parse the 1003.1 syntax. */ - if (tz && *tz == ':') + if (*tz == ':') ++tz; /* Check whether the value changed since the last run. */ - if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0) + if (old_tz != NULL && strcmp (tz, old_tz) == 0) /* No change, simply return. */ return; - if (tz == NULL) - /* No user specification; use the site-wide default. */ - tz = TZDEFAULT; - tz_rules[0].name = NULL; tz_rules[1].name = NULL; @@ -421,9 +422,12 @@ tzset_internal (int always) old_tz = __strdup (tz); /* Try to read a data file. */ - __tzfile_read (tz); - if (__use_tzfile) - return; + if (*tz != '\0') + { + __tzfile_read (tz); + if (__use_tzfile) + return; + } /* No data file found. Default to UTC if nothing specified or if TZDEFAULT is broken. */ From patchwork Sun Jan 5 05:57:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104079 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9ACE43858D28 for ; Sun, 5 Jan 2025 06:33:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9ACE43858D28 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=JP3pGpA5 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id F19E63858C98 for ; Sun, 5 Jan 2025 05:59:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F19E63858C98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F19E63858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056763; cv=none; b=jjeYXkw9rca+hUsb2xyfUqN+x4PIASxRIFGLPRzExppitSYf9e+n/FZ1qpdo5F9lagrwDcqAOIme70tj43rkrrKvZU2o5v/TWIh0URaA5/Lpk3qrDWprn8QQI8c4kUeVJWIMmV4JsdJi15wINghQB4o7xjPELxU0fR/dWOm+AB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056763; c=relaxed/simple; bh=w8uF30u06mUu0pQqJ+ZbqYeAePflxjXglCeM1fAh1Es=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Np76OkLgv4MmN3N0370iwxQj75f3ZIbhgqJujNGBwMrkz6ZbT+Hm/X6COFp40UEcr3RNR9KtssqAv/ydTnfiYB+cu1UzDhJxK15uMybu+YPtHoV/inFUH1YBBdt7WF6nQu3RnhKoNz30lpmFDSI0ibEtYMmbYFEvn2tFZskoOuw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F19E63858C98 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 773CD3C123847 for ; Sat, 4 Jan 2025 21:59:22 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id CgZy5fxZv5i5; Sat, 4 Jan 2025 21:59:22 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 1EE5A3C123844; Sat, 4 Jan 2025 21:59:22 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 1EE5A3C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056762; bh=dWT2TjtGlwFM6Hhm+Y2Hs15AiJuuVfN5gcQYpL+mFIM=; h=From:To:Date:Message-ID:MIME-Version; b=JP3pGpA520ThFZU+GbVRP1fzX6LPtkX3CABei4NuONaKWGVF60F64XjMoNSOmV9EL 1nmkTwqNer+UvXkZmR9DTf+prwUPifkHkWCWXS4DY4YUu2USOCVuh0sNZER97VRmuK Ff31JsBB+KxHzbvgIUGi8RrM1MFZQGrh3fiecCM5u1hc/xkLEOJ73as83NLCzn8OXw gB4HGtPmPz6DOD/i2WBlekVKLvd8vi2ZG4iEN1iFvbjJZS9o6hq08AXXrhNd/NxYF3 dZVLFIBq+00FFHOuI81TKILhWtsN028SGogGLcy2ed5t1DwlXH0Rv3sNZB/wGa5Hxa Bau3yXU6xEUVA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id esItpaiGvl17; Sat, 4 Jan 2025 21:59:22 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 083933C082EB9; Sat, 4 Jan 2025 21:59:22 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 27/59] TZ="" always means UTC sans leap seconds Date: Sat, 4 Jan 2025 21:57:02 -0800 Message-ID: <20250105055750.1668721-28-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org TZ="" now always means UTC without leap seconds. This agrees better with doc and with tzcode, where TZ="" is intended to be a performance hack. * manual/time.texi (TZ Variable): Clarify that TZ=":" is equivalent to TZ="", and that both mean UTC without leap seconds. * time/tzfile.c (__tzfile_read): Check for TZ="" here, so that __use_tzfile is set properly. * time/tzset.c (tzset_internal): Simplify by relying on __tzfile_read to check for TZ="". --- NEWS | 7 +++++++ manual/time.texi | 10 +++++----- time/tzfile.c | 3 +++ time/tzset.c | 13 +++---------- timezone/test-tz.c | 1 + 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index a62fb8136d..a737a0124b 100644 --- a/NEWS +++ b/NEWS @@ -90,6 +90,13 @@ Deprecated and removed features, and other changes affecting compatibility: /usr/share/zoneinfo/posixrules but this did not work and in practice posixrules invariably specified the US rule anyway. +* TZ="" now always agrees with TZ=":" and means UTC without leap seconds. + Previously TZ="" meant UTC with leap seconds on the rare systems + configured to use leap seconds by default. However, this behavior + disagreed with both the glibc manual (which said that TZ="" and TZ=":" + have the same behavior) and with tzcode upstream, and it did not work + in some cases. + Changes to build and runtime requirements: * On recent Linux kernels with vDSO getrandom support, getrandom does not diff --git a/manual/time.texi b/manual/time.texi index cff94cb48b..d5619534a5 100644 --- a/manual/time.texi +++ b/manual/time.texi @@ -2642,6 +2642,10 @@ EST+5EDT,M3.2.0/2,M11.1.0/2 <-02>+2<-01>,M3.5.0/-1,M10.5.0/0 @end smallexample +@item +As an extension to POSIX, when the value of @env{TZ} is the empty string, +@theglibc{} uses UTC without leap seconds. + @item The @dfn{colon format} begins with @samp{:}. Here is an example. @@ -2652,11 +2656,7 @@ The @dfn{colon format} begins with @samp{:}. Here is an example. @noindent Each operating system can interpret this format differently; in @theglibc{}, the @samp{:} is ignored and @var{characters} -are treated as if they specified the geographical or proleptic format. - -@item -As an extension to POSIX, when the value of @env{TZ} is the empty string, -@theglibc{} uses UTC. +are treated as if they specified one of the other formats. @end itemize @pindex /etc/localtime diff --git a/time/tzfile.c b/time/tzfile.c index 08cf0686df..4ca1f3c0c8 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -172,6 +172,9 @@ __tzfile_read (const char *file) if (*file != '/') { + if (*file == '\0') + goto ret_free_transitions; + const char *tzdir; tzdir = getenv ("TZDIR"); diff --git a/time/tzset.c b/time/tzset.c index b014bca8d3..c2575edad9 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -399,10 +399,6 @@ tzset_internal (int always) /* No user specification; use the site-wide default. */ tz = TZDEFAULT; - if (*tz == '\0') - /* User specified the empty string; use UTC explicitly. */ - tz = "Universal"; - /* A leading colon means "implementation defined syntax". We ignore the colon and always use the same algorithm: try a data file, and if none exists parse the 1003.1 syntax. */ @@ -422,12 +418,9 @@ tzset_internal (int always) old_tz = __strdup (tz); /* Try to read a data file. */ - if (*tz != '\0') - { - __tzfile_read (tz); - if (__use_tzfile) - return; - } + __tzfile_read (tz); + if (__use_tzfile) + return; /* No data file found. Default to UTC if nothing specified or if TZDEFAULT is broken. */ diff --git a/timezone/test-tz.c b/timezone/test-tz.c index 642b45a0ed..597096a5c0 100644 --- a/timezone/test-tz.c +++ b/timezone/test-tz.c @@ -8,6 +8,7 @@ struct { time_t expected; } tests[] = { {"MST", 832935315}, + {":", 832910115}, {"", 832910115}, {":UTC", 832910115}, {"UTC", 832910115}, From patchwork Sun Jan 5 05:57:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104050 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D7BDB3858D39 for ; Sun, 5 Jan 2025 06:11:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D7BDB3858D39 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=EpVBtwOh X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 7E5E73858402 for ; Sun, 5 Jan 2025 05:59:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7E5E73858402 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7E5E73858402 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056765; cv=none; b=cWxiR+yaxdzaR53Z7bEmWudNp2UlvGo+Nwc6lkb5tfH0Q3zWGiHBa33/ohrCo+U7ncS/oKwK8+oSxYLI9NPp5uak3UJUUgslByE3ihk3p3c7IibrIFf1ohtcVaPn96W2fY2jxM4YIC5T6fiYNLNX4gXbvxZtUEzFtASasElKdHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056765; c=relaxed/simple; bh=ybmDr7Hnjcy4RgMOGWS4bEF2F1zRSuDkuX/KiwGRALQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=reabNnquonew0Tpf9d6UN/JBs1zA6huZ+c38be72AsZx9GKXQLXsH0XtZxNeWY6/Rv0aXOiMAcSMJqKXX0N1Ucjo9PLUaDACgy6qzDfbOXUXxC23JJKqz4zTaVAg6olLn6y4Q85XPw6rArbyN9wAsm4xWHKu7cjnz+6hQhBTOuY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E5E73858402 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 01D5E3C123844 for ; Sat, 4 Jan 2025 21:59:25 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id xVTlEZOesgz8; Sat, 4 Jan 2025 21:59:24 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 8EEF83C123843; Sat, 4 Jan 2025 21:59:24 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 8EEF83C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056764; bh=H5W/IzKfvPwP1zzzg18tXFvfoMcifNorl1F5NsJQEqg=; h=From:To:Date:Message-ID:MIME-Version; b=EpVBtwOhh84rUyL6Phr0Nn0fczBLDc20ZOi9+e5C5UtUWSM6tqd5e5GffSPnazLBn vN795nIUF3GdAJEBGsbL/frUh9mHqoA20fi3EL57xGYUsHB1cfB3/ZErb93K038Voc TDQ1FYOjolJe2kLZUDV/+EKtLJ0v0TpTIYpYb4UnxCpN15KZDXhE0cl34gFCYVm1XG 9BLXBAhVDuSt34z9YgGgz/KD6Z+QF/71pcvng3qHexu2ZFqEBzICyo/EX3wVmd5fJX 5J8vIz6om5nhAN78E+57DqWx7u6izHTqH4EMz7tV/8d1hpZUtsYtdYQqZMzaF/QnH1 Q/S/7jOBa0CZw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id pFEQDl5X0-oi; Sat, 4 Jan 2025 21:59:24 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 795643C082EB9; Sat, 4 Jan 2025 21:59:24 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 28/59] Prefer "UTC" for UTC Date: Sat, 4 Jan 2025 21:57:03 -0800 Message-ID: <20250105055750.1668721-29-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * NEWS: Mention this. * resolv/ns_date.c, sysdeps/generic/netinet/in_systm.h: * sysdeps/nptl/pthread.h, time/bits/types/struct_timeb.h: * time/strftime_l.c, time/sys/time.h, time/tzfile.c, time/tzset.c: In strings and commentary, prefer "UTC" to "GMT" when referring to Coordinated Universal Time. POSIX.1-2024 requires this and other systems do it this way. --- NEWS | 5 +++++ resolv/ns_date.c | 2 +- sysdeps/generic/netinet/in_systm.h | 2 +- sysdeps/nptl/pthread.h | 2 +- time/bits/types/struct_timeb.h | 2 +- time/strftime_l.c | 2 +- time/sys/time.h | 2 +- time/tzset.c | 10 +++++----- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/NEWS b/NEWS index a737a0124b..69a730a72c 100644 --- a/NEWS +++ b/NEWS @@ -84,6 +84,11 @@ Deprecated and removed features, and other changes affecting compatibility: explicitly because of the executable bit in GNU_STACK, and the stack is not already executable. Instead, loading such objects will fail. +* gmtime and similar functions now use "UTC", not "GMT", to abbreviate + Coordinated Universal Time (often called "GMT", though they are not + quite the same). This is more compatible with other platforms and is + required by POSIX.1-2024. + * The US daylight saving rule is now the default for incomplete TZ strings like TZ="AST4ADT" that use the TZ syntax from circa-1980s Unix System V. Formerly the rule was documented to be that of diff --git a/resolv/ns_date.c b/resolv/ns_date.c index 8e94d1c4e8..d931c70df8 100644 --- a/resolv/ns_date.c +++ b/resolv/ns_date.c @@ -35,7 +35,7 @@ static int datepart(const char *, int, int, int, int *); /*% * Convert a date in ASCII into the number of seconds since - * 1 January 1970 (GMT assumed). Format is yyyymmddhhmmss, all + * 1 January 1970 (UTC assumed). Format is yyyymmddhhmmss, all * digits required, no spaces allowed. */ diff --git a/sysdeps/generic/netinet/in_systm.h b/sysdeps/generic/netinet/in_systm.h index cd09598926..1b8b7de710 100644 --- a/sysdeps/generic/netinet/in_systm.h +++ b/sysdeps/generic/netinet/in_systm.h @@ -33,7 +33,7 @@ __BEGIN_DECLS typedef uint16_t n_short; /* short as received from the net */ typedef uint32_t n_long; /* long as received from the net */ -typedef uint32_t n_time; /* ms since 00:00 GMT, byte rev */ +typedef uint32_t n_time; /* ms since 00:00 UTC, byte rev */ __END_DECLS diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h index b643546dc8..5ebfadc2bd 100644 --- a/sysdeps/nptl/pthread.h +++ b/sysdeps/nptl/pthread.h @@ -1137,7 +1137,7 @@ extern int pthread_cond_wait (pthread_cond_t *__restrict __cond, /* Wait for condition variable COND to be signaled or broadcast until ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an absolute time specification; zero is the beginning of the epoch - (00:00:00 GMT, January 1, 1970). + (00:00:00 UTC, January 1, 1970). This function is a cancellation point and therefore not marked with __THROW. */ diff --git a/time/bits/types/struct_timeb.h b/time/bits/types/struct_timeb.h index 1fe60c7131..b4939ba4b3 100644 --- a/time/bits/types/struct_timeb.h +++ b/time/bits/types/struct_timeb.h @@ -8,7 +8,7 @@ struct timeb { time_t time; /* Seconds since epoch, as from 'time'. */ unsigned short int millitm; /* Additional milliseconds. */ - short int timezone; /* Minutes west of GMT. */ + short int timezone; /* Minutes west of UTC. */ short int dstflag; /* Nonzero if Daylight Savings Time used. */ }; diff --git a/time/strftime_l.c b/time/strftime_l.c index 584df83d15..2c79eeddae 100644 --- a/time/strftime_l.c +++ b/time/strftime_l.c @@ -543,7 +543,7 @@ __strftime_internal (CHAR_T *s, size_t maxsize, const CHAR_T *format, if (ut) { if (! (zone && *zone)) - zone = "GMT"; + zone = "UTC"; } #endif diff --git a/time/sys/time.h b/time/sys/time.h index 34ccee0e0e..0eee32bbce 100644 --- a/time/sys/time.h +++ b/time/sys/time.h @@ -51,7 +51,7 @@ __BEGIN_DECLS This is obsolete and should never be used. */ struct timezone { - int tz_minuteswest; /* Minutes west of GMT. */ + int tz_minuteswest; /* Minutes west of UTC. */ int tz_dsttime; /* Nonzero if DST is ever in effect. */ }; #endif diff --git a/time/tzset.c b/time/tzset.c index c2575edad9..2bbf8f055b 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -29,7 +29,7 @@ #define SECSPERDAY ((__time64_t) 86400) -char *__tzname[2] = { (char *) "GMT", (char *) "GMT" }; +char *__tzname[2] = { (char *) "UTC", (char *) "UTC" }; int __daylight = 0; long int __timezone = 0L; @@ -51,7 +51,7 @@ typedef struct unsigned short int m, n, d; /* Month, week, day. */ int secs; /* Time of day. */ - int offset; /* Seconds east of GMT (west if < 0). */ + int offset; /* Seconds east of UTC (west if < 0). */ /* We cache the computed time of change for a given year so we don't have to recompute it. */ @@ -449,7 +449,7 @@ compute_change (tz_rule *rule, long long int year) if (rule->computed_for == year) return; - /* First set T to January 1st, 0:00:00 GMT in YEAR. */ + /* First set T to YEAR-01-01 00:00:00 UT. */ t = (((year - 1970) * 365 /* Compute the number of leap days between 1970 and YEAR (exclusive). There is a leap day every 4th year ... */ @@ -521,7 +521,7 @@ compute_change (tz_rule *rule, long long int year) break; } - /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want. + /* T is now the Epoch-relative time of 00:00:00 UT on the day we want. Just subtract the UT offset and add the time of day, and we're done. */ rule->change = t - rule->offset + rule->secs; @@ -611,7 +611,7 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) if (! use_localtime) { tp->tm_isdst = 0; - tp->tm_zone = "GMT"; + tp->tm_zone = "UTC"; tp->tm_gmtoff = 0L; } From patchwork Sun Jan 5 05:57:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104082 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9413E3858D35 for ; Sun, 5 Jan 2025 06:36:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9413E3858D35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=byzjCFUu X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 1AE593858C32 for ; Sun, 5 Jan 2025 05:59:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1AE593858C32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1AE593858C32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056768; cv=none; b=BeIdpWiXmQWjpYCVw4wcxsWnCHLo23kpNtTzdu95gLvGntF5Xs+rAO3J7YOkjlutRzGKQsn9Ja86clqzwnbnGnQeGaxPkeYb87cLjKOfhsf+ktLP36WWUn6T1Wya1OpWltDqq3mSka3Ey3Iv+qE7EDabDALzwRTDAsdFZNXVUUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056768; c=relaxed/simple; bh=MCGyY8282PHa3drc4155VYR2U4s5jmlV1fIRzT9NAmk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gBGjuz+vP1xIvzDjneVl+qxbElvnMlDwBfm4S2kbPAKcfnPbFRnQB00EQ8zAD7kvz34pVQmBT8FPIA/LyHANUcjE2JwYA6YPogBsNqxI5eFazWLlzg5lXM/imBCOhK77yNLmaYdtQszxIrijXd39l2FAnwfG3jbGWnzE0ZPGtas= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1AE593858C32 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 9365C3C123845 for ; Sat, 4 Jan 2025 21:59:27 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id RaUXsAoRvkC2; Sat, 4 Jan 2025 21:59:27 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 4FCED3C123843; Sat, 4 Jan 2025 21:59:27 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 4FCED3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056767; bh=q63RYMdoGKRPyCG1gFptL+QL6LlTrIDiAA992Gahue4=; h=From:To:Date:Message-ID:MIME-Version; b=byzjCFUu9WkuRTtPu+0yNEUQ+Ptkk1BNEvLbLgYMkRAUn1KmY2Q8j/xhzijECmG5R KFBzZuYlJOm+FWqwAKoCJexsq5DC+U3xyjVpH8oTKlclUwiBla/vntJonbaSG/ZXsq K0Ezd0iIuSf67vYyBAFuUkcJcN2gAuPiRn9wO1U3u7mkI3aiuaahQ92a1/Y9gyrS6k kBzKGr4eKj9AgOXL53rjX80XqQ9TsEBPyEvG0UXTMScWDlo6AqxaLs+QE7/URewKsT gxKciRxTrrwqSA1+ClgpFlr3XRDCzviHgqrsdRuCkcqhy+D1gSwnXrkskKyVZoFiwq TPMejNpUTw84g== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id zxMk0nj_cCh8; Sat, 4 Jan 2025 21:59:27 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 391B43C082EB9; Sat, 4 Jan 2025 21:59:27 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 29/59] Simplify setting tz_rules to UTC Date: Sat, 4 Jan 2025 21:57:04 -0800 Message-ID: <20250105055750.1668721-30-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (__tzset_parse_tz): Reset to named UTC, not unnamed. (tzset_internal): Rely on __tzset_parse_tz to reset state. --- time/tzset.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/time/tzset.c b/time/tzset.c index 2bbf8f055b..1873ad205b 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -352,11 +352,11 @@ parse_rule (const char **tzp, int whichrule) void __tzset_parse_tz (const char *tz) { - /* Clear out old state and reset to unnamed UTC. */ + /* Clear out old state and reset to UTC. */ memset (tz_rules, '\0', sizeof tz_rules); if (NO_DST != 0) tz_rules[0].type = tz_rules[1].type = NO_DST; - tz_rules[0].name = tz_rules[1].name = ""; + tz_rules[0].name = tz_rules[1].name = "UTC"; /* Get the standard time zone abbreviations. */ if (parse_tzname (&tz, 0) && parse_offset (&tz, 0)) @@ -410,9 +410,6 @@ tzset_internal (int always) /* No change, simply return. */ return; - tz_rules[0].name = NULL; - tz_rules[1].name = NULL; - /* Save the value of `tz'. */ free (old_tz); old_tz = __strdup (tz); @@ -422,18 +419,10 @@ tzset_internal (int always) if (__use_tzfile) return; - /* No data file found. Default to UTC if nothing specified or if + /* No data file found. Default to UTC without leap seconds if TZDEFAULT is broken. */ - - if (*tz == '\0' || strcmp (tz, TZDEFAULT) == 0) - { - memset (tz_rules, '\0', sizeof tz_rules); - tz_rules[0].name = tz_rules[1].name = "UTC"; - if (NO_DST != 0) - tz_rules[0].type = tz_rules[1].type = NO_DST; - update_vars (); - return; - } + if (strcmp (tz, TZDEFAULT) == 0) + tz = ""; __tzset_parse_tz (tz); } From patchwork Sun Jan 5 05:57:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104073 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D66693858D21 for ; Sun, 5 Jan 2025 06:26:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D66693858D21 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=TahbdRSw X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 2D00A3858405 for ; Sun, 5 Jan 2025 05:59:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D00A3858405 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2D00A3858405 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056771; cv=none; b=xsJLYclQSwFFW1w93OaHP8jnaP3d58tYfxnLsCnWm7FT261mjlZ8+KXPjwVeegr3qcc163SVz2oIkERnSFqFvWCPBM0NEvEGPY/bCMHfGygOFY28HChdri7CzUxfJCSLvJ2RyHCwK1ntlKA8Q2MVPtfSgz7XZHfx5WPrgx2JdNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056771; c=relaxed/simple; bh=z2xkbZl7NqJZH5akYn3Cy/YE2HqYLx2cfje2fjp5VZM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=L3NZnFtQwC7i094QzMOaqunT8m/a96I/hLOlKWuvKWR/oZu7HZToaHke1X8fLQzh3aHhlQeYBKtPbVa197G994XbT4br92S1i26ct26/A9MxCx/9Izw6SYsHHAZnpf0rwegbu3W4Adr4MpXCjkhqD4YAnDHa+8tiLu4fGUyYYNY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D00A3858405 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id A693B3C123844 for ; Sat, 4 Jan 2025 21:59:30 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id vX6dn_DiWPs2; Sat, 4 Jan 2025 21:59:30 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 3C5DB3C123843; Sat, 4 Jan 2025 21:59:30 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 3C5DB3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056770; bh=0k6555OtSTcALgN5JJCkvxT1gpzJCJotW0YfnXoDORQ=; h=From:To:Date:Message-ID:MIME-Version; b=TahbdRSwogNMRuBshhJY6uIkMgZqhwZMWj3gZSz9ixiypzFQk5GOgfz7xvPQLhhCC d0QZ3Pw861cSwyVXh99ikcTHAkWJOhAKl3UEUNxZvvD4CHQQ8nWdX/w/l/h7EltlJP rxj9zFr48rN+OwHfDrfsOfAMzGSPMDn5RNtNSJDHBcHWrnHPy8cJNkMgISO8itM++T nzo+Va2/ZLg5CcSfS5vg4vxoDSUyJ+xxb9LE/TJpIwQv9yAQjyQc+W7ZE3KxhLAOAa A6S3BZw4+lCxPufQfTPiTVW70bWSQJWbgSTj8BVviZ/bXlKoJIYj/cxJpMm1D1J/5B bXPxokVUlHFiA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id SEQtEXhpQJWN; Sat, 4 Jan 2025 21:59:30 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 2411B3C082EB9; Sat, 4 Jan 2025 21:59:30 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 30/59] Update tzname etc. even if TZ is unchanged Date: Sat, 4 Jan 2025 21:57:05 -0800 Message-ID: <20250105055750.1668721-31-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org POSIX says that external variables like tzname[0] are updated by tzset even if TZ has not changed. * time/tst-tzname.c: Include limits.h. (do_test): Test this. * time/tzset.c (tzset_internal): Update external vars even if TZ has not changed. When __tzfile_read succeeds, save its results. (__tzset_unlocked): Do not set __tzname here, as tzset_internal has already done it. --- time/tst-tzname.c | 85 ++++++++++++++++++++++++++++++++++++++++++++--- time/tzset.c | 30 +++++++++++------ 2 files changed, 100 insertions(+), 15 deletions(-) diff --git a/time/tst-tzname.c b/time/tst-tzname.c index b9b4aea35e..a70d5aee6a 100644 --- a/time/tst-tzname.c +++ b/time/tst-tzname.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -29,13 +30,87 @@ do_test (void) setenv ("TZ", TZDEFRULES, 1); tzset (); const char *stdtz = strdup (tzname[0]); - setenv ("TZ", "STD-1DST", 1); - tzset (); - if (strcmp (tzname[0], "STD") != 0) + + /* Test that proleptic TZ settings set tzname and other external vars. + Do not test geographic or empty settings, as POSIX does not require + external vars to be set for those cases. */ + struct tz_test + { + char const *tz; + int daylight; + long int timezone; + char const *tzname[2]; + } const tzs_to_test[] = { - printf ("FAIL: TZ=STD-1DST, tzname[0] = %s\n", tzname[0]); - result = 1; + { ":STD-1DST", 1, - 1 * 60 * 60, { "STD", "DST" } }, + { ":AST4ADT", 1, 4 * 60 * 60, { "AST", "ADT" } }, + { ":QQQ5RRR5", 1, 5 * 60 * 60, { "QQQ", "RRR" } }, + { ":NZST-12NZDT,M9.5.0,M4.1.0/3", 1, -12 * 60 * 60, { "NZST", "NZDT" } }, + { ":<-02>2<-01>,M3.5.0/-1,M10.5.0/0", 1, 2 * 60 * 60, { "-02", "-01" } }, + { ":UTC0", 0, 0, { "UTC", } }, + }; + for (int i = 0; i < sizeof tzs_to_test / sizeof *tzs_to_test; i++) + { + for (int j = 0; j < 2; j++) + { + char const *tz = tzs_to_test[i].tz + j; + if (setenv ("TZ", tz, 1) < 0) + { + printf ("FAIL: setenv (\"TZ\", \"%s\", 1)\n", tz); + result = 1; + continue; + } + + struct tm *tm; + for (int k = 0; k < 5; k++) + { + time_t t = 0; + daylight = INT_MIN; + timezone = LONG_MIN; + tzname[0] = tzname[1] = (char *) "XYZ"; + + char const *method; + switch (k) + { + case 0: + case 1: method = "tzset"; tzset (); break; + case 2: method = "localtime"; tm = localtime (&t); break; + case 3: method = "mktime", mktime (tm); break; + case 4: method = "ctime"; ctime (&t); break; + } + + if (daylight < 0 + || (daylight != 0) != tzs_to_test[i].daylight) + { + printf ("FAIL: TZ=%s, %s, daylight = %d\n", + tz, method, daylight); + result = 1; + } + if (timezone != tzs_to_test[i].timezone) + { + printf ("FAIL: TZ=%s, %s, timezone = %ld\n", + tz, method, timezone); + result = 1; + } + if (tzname[0] == NULL + || strcmp (tzname[0], tzs_to_test[i].tzname[0]) != 0) + { + printf ("FAIL: TZ=%s, %s, tzname[0] = %s\n", + tz, method, tzname[0] == NULL ? "(NULL)" : tzname[0]); + result = 1; + } + if (tzs_to_test[i].tzname[1] != NULL + && (tzname[1] == NULL + || strcmp (tzname[1], tzs_to_test[i].tzname[1]) != 0)) + { + printf ("FAIL: TZ=%s, %s, tzname[1] = %s\n", + tz, method, tzname[1] == NULL ? "(NULL)" : tzname[0]); + result = 1; + } + } + } } + setenv ("TZ", TZDEFRULES, 1); tzset (); if (strcmp (tzname[0], stdtz) != 0) diff --git a/time/tzset.c b/time/tzset.c index 1873ad205b..270383e383 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -125,6 +125,7 @@ __tzstring (const char *s) static char *old_tz; +/* Update POSIX-required external variables to their saved values. */ static void update_vars (void) { @@ -407,8 +408,11 @@ tzset_internal (int always) /* Check whether the value changed since the last run. */ if (old_tz != NULL && strcmp (tz, old_tz) == 0) - /* No change, simply return. */ - return; + { + /* No change, simply update external vars. */ + update_vars (); + return; + } /* Save the value of `tz'. */ free (old_tz); @@ -417,7 +421,20 @@ tzset_internal (int always) /* Try to read a data file. */ __tzfile_read (tz); if (__use_tzfile) - return; + { + /* Save equivalent of 'daylight' for later use by update_vars. + Although the other external variables have unspecified values + and so need not be saved in the usual case, save them anyway, + as POSIX requires this for the rare case of file-backed proleptic + TZ strings like "EST5EDT", and it is more likely to match user + expectations for geographical TZ strings. */ + enum tz_rule_type some_DST = J0; /* anything but NO_DST */ + tz_rules[1].type = NO_DST + __daylight * (some_DST - NO_DST); + tz_rules[0].offset = -__timezone; + tz_rules[0].name = __tzname[0]; + tz_rules[1].name = __tzname[1]; + return; + } /* No data file found. Default to UTC without leap seconds if TZDEFAULT is broken. */ @@ -545,13 +562,6 @@ void __tzset_unlocked (void) { tzset_internal (1); - - if (!__use_tzfile) - { - /* Set `tzname'. */ - __tzname[0] = (char *) tz_rules[0].name; - __tzname[1] = (char *) tz_rules[1].name; - } } void From patchwork Sun Jan 5 05:57:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104078 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B4D483858D21 for ; Sun, 5 Jan 2025 06:31:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B4D483858D21 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=G4Et33qR X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id A9D313858403 for ; Sun, 5 Jan 2025 05:59:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A9D313858403 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A9D313858403 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056773; cv=none; b=UiSUo4QhUtnOyeitdQLcBuaBDYSWBbDscwgDZ3ip0kKEFPycH0S+SoTEjbYktD2quJ9kKtrnlW4qAzbyWyeTDXBeo/Ki7JD5JYoEldZXY675KmlCZEKX2+bIPO7w0QAIQmySINF2probl6qxGW9uZ7ZboP8zysJBpk45vCRTcGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056773; c=relaxed/simple; bh=T/S8OkNotIiwk+qtrJxG6EkcwT0Hp+auR5/HwaxgmAE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VZPbm+r2nnqhg8li+wAO4yPOe9cbDmCnMsiIFbjVV7lLbZQBB0wtS3Af/573X4ShemvSJvqHeeKEN8wJ5US5I+TE9JCpqA1oleUrHEhXtpZG6ovUNFN7wWYwTnYgRmdCIkEY1HQ5LCiG1LiUXZ+Fwo2Pil9K8AdAMvTrMXWJXX0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A9D313858403 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 2B6003C123845 for ; Sat, 4 Jan 2025 21:59:33 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id vXmLfVYYtr4S; Sat, 4 Jan 2025 21:59:32 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D1C373C123843; Sat, 4 Jan 2025 21:59:32 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu D1C373C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056772; bh=qUJvycwbCT9RIJXXcvZ4Cvhb7C3KgkzASZlTcFgDWdA=; h=From:To:Date:Message-ID:MIME-Version; b=G4Et33qRF7kesPemqKIUGi67zpjWT67xTKbVWh0f1nmJVJpAd65MJIdMmRn1FZjy9 VFc6uFh0qC4J8yaSrxKe0zLxKasgV3VPKBLqnBHSLWpTU4rmMS9P2itk6CSw2ciLcC QYv3L6KqvNFDcLsr1U65wwze38I1Sm8L5IDaZ4ISlqP+6uwHN4FnAqwhgl5tC4hriP myOCq0KL9fcatesq7OzqLayi/FYSqLaC7W4PChYkpHEIc6TbHthsIYsLHT3FLC9d5A UCnkSbiTjSSZhxyNEJpcIpBRq+O62v4rrB+spYN7eveCFeiy7QJUtopTXNzNrn4e/L DPFofJv8D+aKQ== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id dKXhH_l8xgat; Sat, 4 Jan 2025 21:59:32 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id BAFAF3C082EB9; Sat, 4 Jan 2025 21:59:32 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 31/59] "POSIX TZ" -> "proleptic TZ" Date: Sat, 4 Jan 2025 21:57:06 -0800 Message-ID: <20250105055750.1668721-32-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Since POSIX.1-2024 specifies geographical TZ as well as the traditional proleptic TZ settings, change commentary to say "proleptic TZ" rather than "POSIX TZ". --- time/tst-mktime2.c | 4 ++-- time/tzfile.c | 6 +++--- time/tzset.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/time/tst-mktime2.c b/time/tst-mktime2.c index 09f85134d2..f63e205b4c 100644 --- a/time/tst-mktime2.c +++ b/time/tst-mktime2.c @@ -55,9 +55,9 @@ spring_forward_gap (void) /* glibc (up to about 1998-10-07) failed this test. */ struct tm tm; - /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0" + /* Use the proleptic 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 + on systems that don't support POSIX.1-2024, or don't have the full zoneinfo tables installed. */ set_timezone ("PST8PDT,M4.1.0,M10.5.0"); diff --git a/time/tzfile.c b/time/tzfile.c index 4ca1f3c0c8..d30faedfa2 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -258,7 +258,7 @@ __tzfile_read (const char *file) goto read_again; } - /* Compute the size of the POSIX time zone specification in the + /* Compute the size of the proleptic time zone specification in the file. This includes the trailing but not the leading newline. */ size_t tzspec_size; if (trans_width == 8) @@ -471,7 +471,7 @@ __tzfile_read (const char *file) while (i < typecnt) types[i++].isutc = 0; - /* Read the POSIX TZ-style information if possible. */ + /* Read the proleptic TZ information if possible. */ if (tzspec != NULL) { char *nl; @@ -620,7 +620,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, goto found; } - /* Parse the POSIX TZ-style string. */ + /* Parse the proleptic TZ string. */ __tzset_parse_tz (tzspec); /* Convert to broken down structure. If this fails do not diff --git a/time/tzset.c b/time/tzset.c index 270383e383..7fdce9de7b 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -41,7 +41,7 @@ weak_alias (__timezone, timezone) __libc_lock_define_initialized (, __tzset_lock) /* This structure contains all the information about a - timezone given in the POSIX standard TZ envariable. */ + timezone given in a proleptic TZ string. */ typedef struct { const char *name; @@ -349,7 +349,7 @@ parse_rule (const char **tzp, int whichrule) return true; } -/* Parse the POSIX TZ-style string. */ +/* Parse a proleptic TZ string. */ void __tzset_parse_tz (const char *tz) { From patchwork Sun Jan 5 05:57:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104053 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB7AE3858CD1 for ; Sun, 5 Jan 2025 06:14:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB7AE3858CD1 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=MbIOrHD0 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 8801E385840B for ; Sun, 5 Jan 2025 05:59:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8801E385840B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8801E385840B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056776; cv=none; b=DGJftRJ/qTvqETcsyzOMudVh6clOrJczf3jHwwyzdt3y23CRXqd5Pkw/J7FTgcqMoKaDXj/Eeqgkdhl9Pf5ckGKH6D4NdWHgYX5OxgRf8gq1vl1nUNy10279nBSp7IRkHBIaqogOVuoF4NoZcX35jxXd9+vk7qdPVyULJHeF4Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056776; c=relaxed/simple; bh=Bsv4QX1gJ+akXRaZhetiV1j0SGmT/2Pfr7Up1yDsPF4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZF2jgZRTFSkAKpqbLWQMEPJX9uJQZUPlU+p/nbkYZJOMxzjWedzSt3CvpK/rZc58vN/9Gm3ahe6aBv/zf0zDKyBbncjztE9YAgIliZPR9cM5SOslco8QpmPs+VydLMgAh96at02jPirN9I6EsZv5SncKfTvB9pawodBKinvEDig= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8801E385840B Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 0D2FE3C123847 for ; Sat, 4 Jan 2025 21:59:36 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 8sFXDk7KqAAq; Sat, 4 Jan 2025 21:59:35 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id B36003C123843; Sat, 4 Jan 2025 21:59:35 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu B36003C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056775; bh=NbaUcA7gndo2+Nwe3K+e7aXTfMBGuqslaClMM05HxfE=; h=From:To:Date:Message-ID:MIME-Version; b=MbIOrHD08JfXc6Qd0IHrrmbkybVKHLxJNYzdOr/Sm2+5p5O8t6JIOZ4gv/cy4+5ro 8LknVwPKdjViuC7v4ZTb2VtWtWoo80rBweQ80eDna2Nuef1jjTRP48oUSCJgSENFFr vYOyaJg5vdyHNo1S25qcSCw1I5PANdYJBQzNH3gORDmfvI+zZvFOwIyYJWhn6h2R1i r6/0AN4V5dV6Eb1Vv54NDna0q8dKK+cfo6shK4x6NZU18J23hAqTYPNqIDOku5YW2l STeJJ65vmnjSmDc2kR48sdmTJUQ3S0YhDng6AZ1vORMZs5giPJvUBxV6Aq7pp0FBDG DjTxFikB5PK9A== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id iuWg9yeIZNAl; Sat, 4 Jan 2025 21:59:35 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 9C70A3C082EB9; Sat, 4 Jan 2025 21:59:35 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 32/59] Reject invalid TZ strings in TZif files Date: Sat, 4 Jan 2025 21:57:07 -0800 Message-ID: <20250105055750.1668721-33-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Previously, the code could have had quirky behavior if a TZif file's TZ string was corrupted. Now, the code reliably ignores such a string. * time/tzfile.c (__tzfile_compute): If the proleptic TZ string is invalid, do not use it. * time/tzset.c (__tzset_parse_tz): Return a success indication. Do not try to parse DST rule if there is no DST. --- time/tzfile.c | 5 +++-- time/tzset.c | 15 ++++++--------- time/tzset.h | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index d30faedfa2..dae6b2b613 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -620,8 +620,9 @@ __tzfile_compute (__time64_t timer, int use_localtime, goto found; } - /* Parse the proleptic TZ string. */ - __tzset_parse_tz (tzspec); + /* Parse the proleptic TZ string. If this fails do not use it. */ + if (__glibc_unlikely (! __tzset_parse_tz (tzspec))) + goto use_last; /* Convert to broken down structure. If this fails do not use the string. */ diff --git a/time/tzset.c b/time/tzset.c index 7fdce9de7b..e4fbf729b8 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -350,7 +350,7 @@ parse_rule (const char **tzp, int whichrule) } /* Parse a proleptic TZ string. */ -void +bool __tzset_parse_tz (const char *tz) { /* Clear out old state and reset to UTC. */ @@ -360,17 +360,13 @@ __tzset_parse_tz (const char *tz) tz_rules[0].name = tz_rules[1].name = "UTC"; /* Get the standard time zone abbreviations. */ - if (parse_tzname (&tz, 0) && parse_offset (&tz, 0)) + bool ok = parse_tzname (&tz, 0) && parse_offset (&tz, 0); + if (ok) { /* Get the DST time zone abbreviation (if any). */ if (*tz != '\0') - { - if (parse_tzname (&tz, 1)) - parse_offset (&tz, 1); - /* Figure out the standard <-> DST rules. */ - if (parse_rule (&tz, 0)) - parse_rule (&tz, 1); - } + ok = (parse_tzname (&tz, 1) && parse_offset (&tz, 1) + && parse_rule (&tz, 0) && parse_rule (&tz, 1)); else { /* There is no DST. */ @@ -380,6 +376,7 @@ __tzset_parse_tz (const char *tz) } update_vars (); + return ok; } /* Interpret the TZ envariable. */ diff --git a/time/tzset.h b/time/tzset.h index 3687c044e3..61c85fba75 100644 --- a/time/tzset.h +++ b/time/tzset.h @@ -14,7 +14,7 @@ extern void __tzfile_read (const char *file) attribute_hidden; extern void __tzfile_compute (__time64_t timer, int use_localtime, int *leap_correct, bool *leap_hit, struct tm *tp) attribute_hidden; -extern void __tzset_parse_tz (const char *tz) attribute_hidden; +extern bool __tzset_parse_tz (const char *tz) attribute_hidden; extern void __tz_compute (__time64_t timer, struct tm *tm) __THROW attribute_hidden; From patchwork Sun Jan 5 05:57:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104072 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17B653858C5F for ; Sun, 5 Jan 2025 06:25:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17B653858C5F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=cyLxFmmW X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 49DDC3858D21 for ; Sun, 5 Jan 2025 05:59:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 49DDC3858D21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 49DDC3858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056779; cv=none; b=TUie6LFeBzuvBXLJLGbEdh7aKzCjBXz/P+MIy7dLTkzee3Dl/ukn5hK23Ufeu803brLbs/Ovlb4HMIm/8a7vHkI0wGa8gwAhQdKAxM5/ZYkpj9V1B0jcR41C0VF1+mnhlg6ENlC+NZUHb8WuU4mABjfsTyZrGuNDfjAWz86iEwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056779; c=relaxed/simple; bh=xvPa61cZXTHY4BKLN4nGkugoJ7CdQH+sUfXZoPEPXN4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VzUajq95ccqQ7IRKzWCJN51afS2UUCbjCjwBGKPnreE1gbjWH1ZOr7mKefMRkCcBaLsFq1zrI51hkpp8k7XwocHqF+qTRh6S6Yo2WdK8d2LFB+YShpGzaE5PHVp8b2zwyjMrqEuoOVCwHuymrMfy7HKyPrn2es/ywuNsbu/dyGk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 49DDC3858D21 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id C38BB3C123844 for ; Sat, 4 Jan 2025 21:59:38 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id KBTVhslHnlwQ; Sat, 4 Jan 2025 21:59:38 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 7CFA13C123843; Sat, 4 Jan 2025 21:59:38 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 7CFA13C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056778; bh=urwbf9178kdO2qau6nLYbDeRVb6x+lgOLT6rMhKflIU=; h=From:To:Date:Message-ID:MIME-Version; b=cyLxFmmWUfidogCXswbuxv8+BfcTBDsHtB5jU1D+Wmz8eQ+3OK7DE5sqpruNfFEkG zM8oMXUFVWnJHf+EMzYWO2ybuE2U1zLoSLWOF/7WeYEWfLXSIVaqnBbNB/EevQLqbl /fdyj/CZXwb20U+GtqdrJMZJjOpsW2tcxBtfOSSa1TlSlP12+vUnoGD946bYT7vwUz wN+OcVET8Ymn7ZnVaajEiOfAzbJQLM/GFC9ePV4njoKihlcWpbZRjpZsaTHoeCCHs7 hTypxnFN6nO+NhsLw1V15DhC/oxoryFYLyOrUZp6FOc+Tkz32RG9/B5yMEswxKzCI8 zuH/5WJbrRMCA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id cqMplm4aOkyt; Sat, 4 Jan 2025 21:59:38 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 6625A3C082EB9; Sat, 4 Jan 2025 21:59:38 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 33/59] Reject TZif files containing '\0' in TZ string Date: Sat, 4 Jan 2025 21:57:08 -0800 Message-ID: <20250105055750.1668721-34-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): Don't allow TZif strings that contain '\0'. If a TZif file is longer than needed, perhaps due to future TZif format, don't bother reading the excess data. --- time/tzfile.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index dae6b2b613..744c7690ab 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -258,7 +258,7 @@ __tzfile_read (const char *file) goto read_again; } - /* Compute the size of the proleptic time zone specification in the + /* Compute a bound on the size of the proleptic time zone specification in the file. This includes the trailing but not the leading newline. */ size_t tzspec_size; if (trans_width == 8) @@ -472,24 +472,33 @@ __tzfile_read (const char *file) types[i++].isutc = 0; /* Read the proleptic TZ information if possible. */ - if (tzspec != NULL) + if (tzspec_size != 0) { - char *nl; - assert (tzspec_size > 0); - /* Skip the leading newline, then grab everything up to the next - newline; ignore everything after that. */ - if (__getc_unlocked (f) == '\n' - && __fread_unlocked (tzspec, 1, tzspec_size, f) == tzspec_size - && (nl = memchr (tzspec, '\n', tzspec_size)) != NULL) - *nl = '\0'; - else + /* Don't use a TZ string not preceded by newline. */ + if (__getc_unlocked (f) != '\n') + goto lose; + size_t len = 0; + /* Grab everything up to the next newline. Ignore the remaining + part of the file as it may be a later TZif version. */ + for (int ch; (ch = __getc_unlocked (f)) != '\n'; ) + { + /* Don't use a truncated TZ string, or one containing '\0'. */ + if (ch <= 0) + goto lose; + + tzspec[len++] = ch; + + /* Don't use a TZ string that lacks a trailing newline. */ + if (len == tzspec_size) + goto lose; + } + tzspec[len] = '\0'; + + /* Don't use an empty TZ string. */ + if (len == 0) tzspec = NULL; } - /* Don't use an empty TZ string. */ - if (tzspec != NULL && tzspec[0] == '\0') - tzspec = NULL; - fclose (f); /* First "register" all time zone abbreviations. */ From patchwork Sun Jan 5 05:57:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104046 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A36FE3858D38 for ; Sun, 5 Jan 2025 06:08:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A36FE3858D38 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=O73+QAi/ X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id E57AF385841C for ; Sun, 5 Jan 2025 05:59:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E57AF385841C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E57AF385841C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056782; cv=none; b=Ac/4b1kjcEkvdMDEttfK+9zLYlRRMnbtsqZ9xi9sXMeCMuHKAWIXn7FzuSDfrIsVFMY4zSvwCELb8+7Ce410jAdlEc4VeVqrLIXl6oFOsbmVVwJyljxA2AHK8QQixxrRBdHqo1lWL7H296Ozsb/1jRmf/IXYBYQfEJM5NTGO0vI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056782; c=relaxed/simple; bh=Udq8jfVEc+SMcPJIhJj8EFLGkA14KFS5dq67mXzsQzc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=SOJryhUwLej9Fv4/55p3Avp0f6z6YSq9AGjsZximOwTne1rJqwf/9mhlCyBJVIj/8vehXMq9FcrhCR22fj+4QnPS7FIfoqYMxMwpjqxYgAGKRr5OFhv7lSJOgXpG1N/kWIz4srbjpnOvq0VAAwN2arkXqbJtWkm/UBuI41oToKA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E57AF385841C Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 6ADF23C123845 for ; Sat, 4 Jan 2025 21:59:41 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id Jz0q8diz4I-o; Sat, 4 Jan 2025 21:59:41 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 2D35B3C123843; Sat, 4 Jan 2025 21:59:41 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 2D35B3C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056781; bh=+m42DbhtTIE0ItwcB02tzHasNolSCUIqwjM8FvTr7co=; h=From:To:Date:Message-ID:MIME-Version; b=O73+QAi/vTU879RUYkSiaI5MFQE1APOpWw1dcbH7+G3+uIJL4Jok3zAtKn/ZeOVmR LRy8nxQkil7rnFgTtrI/2FmvRs98tj5Szh4Tt/tOc3UiBwj7jecao3R3Ll1aNOxOvU WReLh9TIQ5klAcTyYg+i+Ccgub/FGaB2NUa2G6t1Rx9AOUbQQeAl9U6TY9bmi3TweO 90huIq/tg1XTLI1x0EFC+h7OutYfamM8VHvTrFLKzIi+vgbJJIkyoouSuONY3wWylk qyzabXcY+je9c0s7LF2LfPMzOjIx7HyCogWFwtHohaDZCWsZdidRCawQcD0WJnRDXj eqIcFV7rpWhIg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 8XEzEHlqkZb6; Sat, 4 Jan 2025 21:59:41 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 1666E3C082EB9; Sat, 4 Jan 2025 21:59:41 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 34/59] mktime should not consult 'daylight' Date: Sat, 4 Jan 2025 21:57:09 -0800 Message-ID: <20250105055750.1668721-35-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * lib/mktime.c (__mktime_internal): Do not consult __daylight as it has unreliable contents when a geographical timezone is used, and the user can set it. Instead, use tm_isdst for mktime, and ignore tm_isdst for timegm. This patch is cherry-picked from Gnulib commit 72abb08f4495bf232736f4d13a24bced72a9c327 dated Thu Nov 14 10:40:24 2024 -0700. --- time/mktime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/time/mktime.c b/time/mktime.c index 3a951b2c8c..0981cd1d1d 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -356,8 +356,8 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) int mon = tp->tm_mon; int year_requested = tp->tm_year; - /* If the timezone never observes DST, ignore any tm_isdst request. */ - int isdst = local && __daylight ? tp->tm_isdst : 0; + /* Ignore any tm_isdst request for timegm. */ + int isdst = local ? tp->tm_isdst : 0; /* 1 if the previous probe was DST. */ int dst2 = 0; From patchwork Sun Jan 5 05:57:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104051 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D23653858D35 for ; Sun, 5 Jan 2025 06:12:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D23653858D35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=bfSJUyZr X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id E014A3858C60 for ; Sun, 5 Jan 2025 05:59:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E014A3858C60 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E014A3858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056786; cv=none; b=rJJSiYofyLD4/2+MKrBTqWCae2E/AlYG4/cvIv3BR2RYlWs6oZtYtAtcTaumYCQWHz2TeZiFFHeL5JWudXOeN09u6J706z5ZKkdnoczJIrE4LbfKdvpOrVipFMuBuf42CYk5Zg32Gq18doPyB1eH5o3elstjzdumWjcYv0sIwmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056786; c=relaxed/simple; bh=X4u9/25YACjwNnyzVAhcu98AF0zj1tWhvuEjTDl6c1o=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EMlg8n1H23k21KKcaG+PH8YLyaT26zKIGfEQ78ef/BCFIHASspke02JSiioCcs3ZKyqUnNi5AN0K5vS2arhjhWg5YFSbYkNLnke2WMlFTaDI3Yk9rLRrYmYtmiFMK94X6S3Ba2/XN5wqwy2xh4cPIO0KUnFsG4dfBIe6NAcCrcY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E014A3858C60 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 5B80A3C123844 for ; Sat, 4 Jan 2025 21:59:44 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id ElAYM0SwBgJe; Sat, 4 Jan 2025 21:59:43 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D84223C123843; Sat, 4 Jan 2025 21:59:43 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu D84223C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056783; bh=NvQSd5E6QYHUo7PAAaLeB11BO+QZnauW4+RgpFkz7Hw=; h=From:To:Date:Message-ID:MIME-Version; b=bfSJUyZrx49HLqpGHfR5LyXwnL9kX3ExUkItpMmoGGjmix3+QdRas4muCmDAANiA8 DM+yMyCFDB32yI8u9xbp1HY8I9sT5PNXjmnKm0mYrHlqOkAX6GHlOcg0BhJZWuBvB4 txtT/vbdGbjVryPKPcPJ2pf6gt0NRYIiML9FJF36hFraOIiSudZBdzTcRh9dLbhuyv h58xKJKzhTbsdYa7aSVpscWus5xPcvdwnmKFsX+lg2zl7yOWBrCpCiovp0VTk/r2eK 2sK0TFh0putlNhhn7gcpWCjvIGIK7rGVBKql04iNTwQ3qRG7sC234XKdcj0bPSSN2j +VSpduQA6w6dQ== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 8s6WxDz6-PKC; Sat, 4 Jan 2025 21:59:43 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id C09D13C082EB9; Sat, 4 Jan 2025 21:59:43 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 35/59] Remove __use_tzfile Date: Sat, 4 Jan 2025 21:57:10 -0800 Message-ID: <20250105055750.1668721-36-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This refactoring removes a hidden extern and shrinks static state a bit. * time/tzfile.c (__use_tzfile): Remove static var. All uses removed. (__tzfile_read): Use (tzfile_mtime != 0) instead of __use_tzfile. Return bool. Caller changed. * time/tzset.c (tzset_internal): Accept and return bool. All callers changed. Use old_tz instead of is_initialized to test whether we are initialized, removing the need for the is_initialized static. New static use_tzfile instead, which means what the old __use_tzfile meant. --- time/tzfile.c | 23 +++++----- time/tzset.c | 117 +++++++++++++++++++++++++------------------------- time/tzset.h | 4 +- 3 files changed, 69 insertions(+), 75 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 744c7690ab..bd6df16b00 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -33,7 +33,6 @@ #include -bool __use_tzfile; static dev_t tzfile_dev; static ino64_t tzfile_ino; static __time64_t tzfile_mtime; @@ -138,7 +137,7 @@ decode64 (const void *ptr) } -void +bool __tzfile_read (const char *file) { static const char default_tzdir[] = TZDIR; @@ -147,15 +146,12 @@ __tzfile_read (const char *file) union { struct tzhead tzhead; tzidx tzidx_aligned; } u; tzidx charcnt; tzidx i; - bool was_using_tzfile = __use_tzfile; int trans_width = 4; char *new = NULL; _Static_assert (sizeof (__time64_t) == 8, "__time64_t must be eight bytes"); - __use_tzfile = false; - /* We must not allow to read an arbitrary file in a setuid program. So we fail for any file which is not in the directory hierarchy starting at TZDIR @@ -187,7 +183,7 @@ __tzfile_read (const char *file) /* If we were already using tzfile, check whether the file changed. */ struct __stat64_t64 st; - if (was_using_tzfile + if (tzfile_mtime != 0 && __stat64_time64 (file, &st) == 0 && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev && tzfile_mtime == st.st_mtime) @@ -207,11 +203,6 @@ __tzfile_read (const char *file) free (transitions); transitions = NULL; - /* Remember the inode and device number and modification time. */ - tzfile_dev = st.st_dev; - tzfile_ino = st.st_ino; - tzfile_mtime = st.st_mtime; - /* No threads reading this stream. */ __fsetlocking (f, FSETLOCKING_BYCALLER); @@ -565,10 +556,14 @@ __tzfile_read (const char *file) __daylight = daylight_saved; __timezone = -rule_stdoff; + /* Remember the inode and device number and modification time. */ + tzfile_dev = st.st_dev; + tzfile_ino = st.st_ino; + tzfile_mtime = st.st_mtime; + done: free (new); - __use_tzfile = true; - return; + return true; lose: fclose (f); @@ -576,6 +571,8 @@ __tzfile_read (const char *file) free (new); free (transitions); transitions = NULL; + tzfile_mtime = 0; + return false; } void diff --git a/time/tzset.c b/time/tzset.c index e4fbf729b8..af397d97dd 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -65,7 +65,6 @@ static tz_rule tz_rules[2]; static void compute_change (tz_rule *rule, long long int year) __THROW; -static void tzset_internal (int always); /* List of buffers containing time zone strings. */ struct tzstring_l @@ -379,66 +378,67 @@ __tzset_parse_tz (const char *tz) return ok; } -/* Interpret the TZ envariable. */ -static void -tzset_internal (int always) +/* Interpret the TZ envariable. If ALWAYS, always do so; otherwise do + so only if TZ has changed. Return true if TZ stands for a TZif file. */ +static bool +tzset_internal (bool always) { - static int is_initialized; - const char *tz; - - if (is_initialized && !always) - return; - is_initialized = 1; - - /* Examine the TZ environment variable. */ - tz = getenv ("TZ"); - - if (tz == NULL) - /* No user specification; use the site-wide default. */ - tz = TZDEFAULT; - - /* A leading colon means "implementation defined syntax". - We ignore the colon and always use the same algorithm: - try a data file, and if none exists parse the 1003.1 syntax. */ - if (*tz == ':') - ++tz; - - /* Check whether the value changed since the last run. */ - if (old_tz != NULL && strcmp (tz, old_tz) == 0) - { - /* No change, simply update external vars. */ - update_vars (); - return; - } - - /* Save the value of `tz'. */ - free (old_tz); - old_tz = __strdup (tz); + static bool use_tzfile; - /* Try to read a data file. */ - __tzfile_read (tz); - if (__use_tzfile) + if (old_tz == NULL || always) { - /* Save equivalent of 'daylight' for later use by update_vars. - Although the other external variables have unspecified values - and so need not be saved in the usual case, save them anyway, - as POSIX requires this for the rare case of file-backed proleptic - TZ strings like "EST5EDT", and it is more likely to match user - expectations for geographical TZ strings. */ - enum tz_rule_type some_DST = J0; /* anything but NO_DST */ - tz_rules[1].type = NO_DST + __daylight * (some_DST - NO_DST); - tz_rules[0].offset = -__timezone; - tz_rules[0].name = __tzname[0]; - tz_rules[1].name = __tzname[1]; - return; + /* Examine the TZ environment variable. */ + char const *tz = getenv ("TZ"); + + if (tz == NULL) + /* No user specification; use the site-wide default. */ + tz = TZDEFAULT; + + /* A leading colon means "implementation defined syntax". + We ignore the colon and always use the same algorithm: + try a data file, and if none exists parse the 1003.1 syntax. */ + if (*tz == ':') + ++tz; + + /* If the value changed has not since the last run, + simply update internal vars. */ + if (old_tz != NULL && strcmp (tz, old_tz) == 0) + update_vars (); + else + { + /* Save the value of 'tz'. */ + free (old_tz); + old_tz = __strdup (tz); + + /* Try to read a data file. */ + use_tzfile = __tzfile_read (tz); + if (use_tzfile) + { + /* Save equivalent of 'daylight' for later use by update_vars. + Although the other external variables have unspecified values + and so need not be saved in the usual case, save them anyway, + as POSIX requires this for rare file-backed proleptic + TZ strings like "EST5EDT", and it is more likely to match user + expectations for geographical TZ strings. */ + enum tz_rule_type some_DST = J0; /* anything but NO_DST */ + tz_rules[1].type = NO_DST + __daylight * (some_DST - NO_DST); + tz_rules[0].offset = -__timezone; + tz_rules[0].name = __tzname[0]; + tz_rules[1].name = __tzname[1]; + } + else + { + /* No data file found. Default to UTC without leap seconds if + TZDEFAULT is broken. */ + if (strcmp (tz, TZDEFAULT) == 0) + tz = ""; + + __tzset_parse_tz (tz); + } + } } - /* No data file found. Default to UTC without leap seconds if - TZDEFAULT is broken. */ - if (strcmp (tz, TZDEFAULT) == 0) - tz = ""; - - __tzset_parse_tz (tz); + return use_tzfile; } /* Figure out the exact time (as a __time64_t) in YEAR @@ -558,7 +558,7 @@ __tz_compute (__time64_t timer, struct tm *tm) void __tzset_unlocked (void) { - tzset_internal (1); + tzset_internal (true); } void @@ -587,9 +587,8 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) /* Update internal database according to current TZ setting. POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. This is a good idea since this allows at least a bit more parallelism. */ - tzset_internal (tp == &_tmbuf && use_localtime); - if (__use_tzfile) + if (tzset_internal (tp == &_tmbuf && use_localtime)) __tzfile_compute (timer, use_localtime, &leap_correction, &leap_extra_sec, tp); else diff --git a/time/tzset.h b/time/tzset.h index 61c85fba75..959f38c7ab 100644 --- a/time/tzset.h +++ b/time/tzset.h @@ -8,9 +8,7 @@ /* Defined in tzset.c. */ extern char *__tzstring (const char *string) attribute_hidden; -extern bool __use_tzfile attribute_hidden; - -extern void __tzfile_read (const char *file) attribute_hidden; +extern bool __tzfile_read (const char *file) attribute_hidden; extern void __tzfile_compute (__time64_t timer, int use_localtime, int *leap_correct, bool *leap_hit, struct tm *tp) attribute_hidden; From patchwork Sun Jan 5 05:57:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104085 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 232793858CDA for ; Sun, 5 Jan 2025 06:39:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 232793858CDA Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=juoh9OOG X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 140F73858C62 for ; Sun, 5 Jan 2025 05:59:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 140F73858C62 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 140F73858C62 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056787; cv=none; b=S48gdk/ZpoXzAuamV4DnSDQguJtSlr7pXVk+xEoag+tSUJn9f7yZdf77D/axvTQpEzFPgVZSniEimoOQSAlAiPEQ431uIhyUL+cXAk0m40LoMqZ5byqMM4GPGQJn3wKzGd+Qi6eoJDkN8H60krDquq8mfzOp3Eq/WRqrzl6tISg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056787; c=relaxed/simple; bh=SjvYLuskKuzsJYKa5+EMgos0FzaIZqi8cbdOqSCDvJQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=t4iBZTlLY4R2U/Q94iyGjNHsAZ7wZD9oYFAXWHzhdGua2OKdDX4pO7mlXd3oJN5iWKAEeM2UeEnhyXrMG/esG0Q8S8xw2J8eFyWMLq1eg5TvusYNTxNCGOPG5bS/YRNrhy1WoG/21joeRQ6y5OHcvA04QU+6TJCG4K6BOjLWTy8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 140F73858C62 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 8E5213C123843 for ; Sat, 4 Jan 2025 21:59:46 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 1MyJdLlGmcpr; Sat, 4 Jan 2025 21:59:46 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 529DF3C123847; Sat, 4 Jan 2025 21:59:46 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 529DF3C123847 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056786; bh=rTvZLqH+eFY+1CA8pu39hYHO/5IwfHnS3IDnIi2JnEY=; h=From:To:Date:Message-ID:MIME-Version; b=juoh9OOG1zg48lBx4IMRWJP1tYyDQ3fz/ScxY7yX0iAdFfGdbzYDqWHL6CMfs3Wev n/SfmbZ1kQbi7cBjf2pWJGAVMBPoJa6P0Pe2ya6wvDFhLt7mSjVIW1v17xWCZuAp/T tVLoirCBEsPz25FwnBT178AnlkTl3zxtS+BkB+kivf/49DvrRQIV7jsHScWeDVm9qk fbkWRihN3BBFuet1EU+d9G0Wq93XiSkC4gvfS+XdLJJ3LLhSo0uGMEFEwS7krLclm7 ZfssA0lfz8DGuWuSNxUmhbEZ+3EigF7cREoTxxDd8bTPC15u2mIHZsq+m4aqDqY0xB qLDWctVgplXmw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id JEHj1Fi1mDz4; Sat, 4 Jan 2025 21:59:46 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 3B86A3C082EB9; Sat, 4 Jan 2025 21:59:46 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 36/59] Remove arbitrary limit on TZ, TZDIR lengths Date: Sat, 4 Jan 2025 21:57:11 -0800 Message-ID: <20250105055750.1668721-37-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): Don't use __asprintf, as it mishandles strings longer than INT_MAX. --- time/tzfile.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/time/tzfile.c b/time/tzfile.c index bd6df16b00..678ecdd58b 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -176,8 +176,15 @@ __tzfile_read (const char *file) tzdir = getenv ("TZDIR"); if (tzdir == NULL || *tzdir == '\0') tzdir = default_tzdir; - if (__asprintf (&new, "%s/%s", tzdir, file) == -1) + + size_t tzdirlen = strlen (tzdir); + size_t filelen = strlen (file); + new = malloc (tzdirlen + 1 + filelen + 1); + if (new == NULL) goto ret_free_transitions; + char *newp = __mempcpy (new, tzdir, tzdirlen); + *newp++ = '/'; + __mempcpy (newp, file, filelen + 1); file = new; } From patchwork Sun Jan 5 05:57:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104075 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D63843858D3C for ; Sun, 5 Jan 2025 06:28:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D63843858D3C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=AQLitVm5 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 56D43385841E for ; Sun, 5 Jan 2025 05:59:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 56D43385841E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 56D43385841E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056790; cv=none; b=O4c56wGwUvHuVszR7zfODy7loxjlTFjUCKIqKmDPJvPAQGyCJvKH3Wf+LYIIqdH2DZMG0bvefu9f+YVmpPCrnN6hlrA9iyef/uwCxTMJfz+54EByjyNox/rd5wQLotJP042OlttMo26ddZb7kKUbcxqM2RzZBGtfyP/8ttgZlZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056790; c=relaxed/simple; bh=l+ztc6Twnxh2Nobh2N2cKl2nih9DDKgAFbJLULEfNUU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZvnLM/fuWKUbGsIu9VnCJyEWzgMQxHqrfQqXTnVxSVuT0FzHF9ELS/Z7IE+IUjNhRt3W004Eproovey7upIvC0DM4ihqoHt6o9Pv3FfiCyf8pKw7cqorksmXmvFlaTLyHmViQCAyUEpHLAQKiWs5ci0K5rq0cKxDICIePbGoSeI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 56D43385841E Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D04E63C123847 for ; Sat, 4 Jan 2025 21:59:49 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id Vyr2Ln9MRCyx; Sat, 4 Jan 2025 21:59:49 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 94FC93C123845; Sat, 4 Jan 2025 21:59:49 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 94FC93C123845 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056789; bh=2Czm5/9ZgJc/480euiNE2l0CK7+Zyn/EvuPFHL9a/2s=; h=From:To:Date:Message-ID:MIME-Version; b=AQLitVm5PqfmXYaOi9DgBV2nIVXYxme2uGv+MjWqhh+XfhzpqvgfLAuSgB9Kd/Kea 5KbD17R/3Y6CSAAqvBAwaik18Wu5W6ofPG8cXzBa6Y9Q1w9U7tcNIdwuniD8zw6/GW yuT6W4gvlpZZn5cPNvByh4JLJSZea7h+XcHY5DPvkKGKBO56MMz0pBBELu5xNbXu6h uWVoH1l1lpugdqFF1ENWpV1yy9DOt+OOO9GwkDLoEZ5lHMuabssgCytrJqxcYJretL lvBXJ67Xz0IszjQ0dBGgTxcasd4z3ygePJhpD7vfthol/mFKMTGYZQWppZ+wlPWpbm MEsx/CnQY9eWw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id n76qEIuSJmv2; Sat, 4 Jan 2025 21:59:49 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 7E72E3C082EB9; Sat, 4 Jan 2025 21:59:49 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 37/59] Tighten setuid TZif file name check Date: Sat, 4 Jan 2025 21:57:12 -0800 Message-ID: <20250105055750.1668721-38-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): In a setuid program, reject TZ settings like "/usr/share/zoneinfobad/Foo" which have a prefix "/usr/share/zoneinfo" but do not refer to /usr/share/zoneinfo/*. --- time/tzfile.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/time/tzfile.c b/time/tzfile.c index 678ecdd58b..8efe5b2ec9 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -140,7 +140,7 @@ decode64 (const void *ptr) bool __tzfile_read (const char *file) { - static const char default_tzdir[] = TZDIR; + static const char default_tzdir[] = TZDIR "/"; tzidx isstdcnt, isutcnt; FILE *f; union { struct tzhead tzhead; tzidx tzidx_aligned; } u; @@ -178,6 +178,7 @@ __tzfile_read (const char *file) tzdir = default_tzdir; size_t tzdirlen = strlen (tzdir); + tzdirlen -= tzdir[tzdirlen - 1] == '/'; size_t filelen = strlen (file); new = malloc (tzdirlen + 1 + filelen + 1); if (new == NULL) From patchwork Sun Jan 5 05:57:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104056 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E8C73858D35 for ; Sun, 5 Jan 2025 06:15:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E8C73858D35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=OMYK3P4N X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 2345F3858410 for ; Sun, 5 Jan 2025 05:59:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2345F3858410 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2345F3858410 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056793; cv=none; b=LiSoDgHEtOoM6sySCE3X2kJgl25wzvlABw4t64+qZvDuBXS9slGdRdvmdHUnLf8p43UpgcblkT4VwzUlElEQBP3g+ynMs492nLCXPdmKKOnmnPW2vheNJmrsDJe08U+N+U/5dJYoQG3yBGfBi/wUwul8yKO4SD3XUN0uPo+0JrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056793; c=relaxed/simple; bh=u1HnbceFcdrmyzb6RjHzpslqbL+46YF1ijkZvBagIf8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hEWjNiOrNyFMNYL0abUPDORExUMlVZgQ+AjYlrNPjN1ScXMqVkiyPueuZWEYUOG/lZc8j6LLZgEYyjlJqLTRqN7Q+Z7ekvCorx2ffejs38OiA9qgMtpwB7YJ+u0YPYtO+ctdwxwRUE3MOEJPgfa7zUO/idOl8prg986yqLY4y3U= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2345F3858410 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 9CE863C123843 for ; Sat, 4 Jan 2025 21:59:52 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 0f7mmS7cPcsY; Sat, 4 Jan 2025 21:59:52 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 5B4603C123844; Sat, 4 Jan 2025 21:59:52 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 5B4603C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056792; bh=A6XbqXOj71AWjADXDp1rY7OXBgsB974gSMuso+7W8eI=; h=From:To:Date:Message-ID:MIME-Version; b=OMYK3P4NRb+09ZPqC0sPBJ7iZsnNhcYe8tTNEwQXAK6G3rmDyQRqWU7+cHNEC5Aak BoM7C6WEcQtu7bHnWGgQ7b3qmlzI6GR4vRnQ9YtKsrFIvo6zB3trbrumqccZU4F8sp HQG6DHrBJ+rwUa4OYcXcKNtFkS6ZLujAFOoqR0dr5U10yFpb9Y1fjvgiz8l+tBasGN HyffEXlLv3iwaOCM+DgAyOE0NdTk5agg2ZnXHOJIBD8ufcNx2zH65AlRkNWbVwlSVQ gx6eTgxueN7vyOJCu1PzkMxKTIlPd/iQe/Ingj9QJkDMQdh9r7fAKn8QMrmKk1f6x4 EzJWMNF+FGCSA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id LMiy1zyQeVf7; Sat, 4 Jan 2025 21:59:52 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 4462A3C082EB9; Sat, 4 Jan 2025 21:59:52 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 38/59] Avoid a malloc in __tzfile_read Date: Sat, 4 Jan 2025 21:57:13 -0800 Message-ID: <20250105055750.1668721-39-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): Avoid a malloc in the usual case, by reusing a stack buffer. --- time/tzfile.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 8efe5b2ec9..09ebc7d8ea 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -143,7 +143,7 @@ __tzfile_read (const char *file) static const char default_tzdir[] = TZDIR "/"; tzidx isstdcnt, isutcnt; FILE *f; - union { struct tzhead tzhead; tzidx tzidx_aligned; } u; + union { struct tzhead tzhead; tzidx tzidx_aligned; char tzfilename[64]; } u; tzidx charcnt; tzidx i; int trans_width = 4; @@ -180,13 +180,20 @@ __tzfile_read (const char *file) size_t tzdirlen = strlen (tzdir); tzdirlen -= tzdir[tzdirlen - 1] == '/'; size_t filelen = strlen (file); - new = malloc (tzdirlen + 1 + filelen + 1); - if (new == NULL) - goto ret_free_transitions; - char *newp = __mempcpy (new, tzdir, tzdirlen); - *newp++ = '/'; - __mempcpy (newp, file, filelen + 1); - file = new; + size_t absfile_size = tzdirlen + 1 + filelen + 1; + char *absfile; + if (absfile_size <= sizeof u.tzfilename) + absfile = u.tzfilename; + else + { + absfile = new = malloc (absfile_size); + if (absfile == NULL) + goto ret_free_transitions; + } + char *p = __mempcpy (absfile, tzdir, tzdirlen); + *p++ = '/'; + __mempcpy (p, file, filelen + 1); + file = absfile; } /* If we were already using tzfile, check whether the file changed. */ From patchwork Sun Jan 5 05:57:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104088 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AB7B83858C51 for ; Sun, 5 Jan 2025 06:43:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AB7B83858C51 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=ACWQpiY8 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 22E623858414 for ; Sun, 5 Jan 2025 05:59:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 22E623858414 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 22E623858414 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056796; cv=none; b=Lpk3e2i5zi7QA6ZnW58vJC4u7ELCmYTzdTP0wtXRmyeY9b0EhhMiCdaFzb951t5kKWJSqFH0UumhdqWh483KSJbAxQksQdswJ+QAG3iXZgf/cp7qwBbEMf1ou6bzVSFZmv68U/t9zt04Db+YwabN2aQMRB7D7wtMfuyV12iwS6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056796; c=relaxed/simple; bh=OYubJBNtwLpBRIAN9kdvbMCZzn+oHVRtkjgQ6OyqVdQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=F+RE8iECmayRKecZO3+lxbwvVPRwVf/i26e7Hoc07bZ1nn3P8nPuPFPrFue8nFXL0XeNLo4sneR0kvsHzae+RTOGmreDNimaSXjwrIg8LZJYwierPYA3ZkPK+xL+6FrhYWrKTKVyUXV0cviZoh17uBkuUd3sf9wnfjuzZGlSE4A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22E623858414 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 9CFC23C123845 for ; Sat, 4 Jan 2025 21:59:55 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id U09BHGQ89rjL; Sat, 4 Jan 2025 21:59:55 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 5E2583C123844; Sat, 4 Jan 2025 21:59:55 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 5E2583C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056795; bh=uosd6B/mbOD+KmXFZO64o//cjgLwBxejtaeuJNXV2/g=; h=From:To:Date:Message-ID:MIME-Version; b=ACWQpiY8kpF+EvaTl92OoH2O43PS9MZgi2rDI+Le5SsEqoyCHgplVg1kBBHikiEs2 gCQQ96fKSA3HflJBb4Ywqpsd47F1DAMQ4+sonPrw4k4C104tf2pB2orh8xvT9lbpFm iH2A8YO4E2TRH2r1Sy71CcWydYU9GTScAEKiXZwVeZr6KuoPli9C6dfTGVggCTp+wx K1vrm8phlgf1/7w6LJvxDP9/E3nlCaVYH2YMCjUuwTBy4SQKmL1nXCTejDhOH5BXx5 jPBoiUYc+Ko7Jul9QbTt6GOtXnkteCwgtaFtC0risYXG7B4CIMZ9Yb/cHLv8MncFUL E+pWQ//xEqsrQ== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id yebEHtoql9TW; Sat, 4 Jan 2025 21:59:55 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 472063C082EB9; Sat, 4 Jan 2025 21:59:55 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 39/59] Free earlier in __tzfile_read Date: Sat, 4 Jan 2025 21:57:14 -0800 Message-ID: <20250105055750.1668721-40-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): Free NEW earlier, to help the heap manager and remove the need for a goto. --- time/tzfile.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 09ebc7d8ea..d600b88e89 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -202,11 +202,15 @@ __tzfile_read (const char *file) && __stat64_time64 (file, &st) == 0 && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev && tzfile_mtime == st.st_mtime) - goto done; /* Nothing to do. */ + { + free (new); + return true; + } /* Note the file is opened with cancellation in the I/O functions disabled and if available FD_CLOEXEC set. */ f = fopen (file, "rce"); + free (new); if (f == NULL) goto ret_free_transitions; off_t f_offset = 0; @@ -576,14 +580,11 @@ __tzfile_read (const char *file) tzfile_ino = st.st_ino; tzfile_mtime = st.st_mtime; - done: - free (new); return true; lose: fclose (f); ret_free_transitions: - free (new); free (transitions); transitions = NULL; tzfile_mtime = 0; From patchwork Sun Jan 5 05:57:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104062 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 223EE3858288 for ; Sun, 5 Jan 2025 06:19:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 223EE3858288 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=J3U1ZMf9 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 233643858415 for ; Sun, 5 Jan 2025 05:59:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 233643858415 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 233643858415 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056799; cv=none; b=PuQV4R/mvi3r0RBSO3/zYVRfV7YKFk5lWtOkAU/glSOQduh6ABJ2EKyktNC0OEI1lXL2mD8bjlm+2uSmY22+oSv8WPYJscqe/Wjy5/lrrMHbSg0qMzPTcliRLpZvUBMWVRBFZ5ierSspPCnBWkSv46eRmGKfm3vaiMv7gNdQbq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056799; c=relaxed/simple; bh=Zr3AndlXgfa+WZ9dozIKgiXUtXxNUl8CshgKW4xAx+8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=IE7hlrP4qky25rPiPW7k7ouKrSddUDQBTzCl1Jh27ffmjPRP3PxHtml3Kdbz5I8t2nKoM9+mJWmzAuNjcS238+1GlQzfYYtlwIkjX6jHOCHFD28q5py+yg9TBOWlHvNKVzQkJive/h3u6+Me/w76D4X2Ty31keOJ9VKMoHm2JZA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 233643858415 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 92B0B3C123848 for ; Sat, 4 Jan 2025 21:59:58 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id HbcxlcoV-icJ; Sat, 4 Jan 2025 21:59:58 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 5A39D3C123844; Sat, 4 Jan 2025 21:59:58 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 5A39D3C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056798; bh=sEsW0JSNVXWgkB3Twjn6yZMx0xWWj9gPF8BTwywlkbo=; h=From:To:Date:Message-ID:MIME-Version; b=J3U1ZMf9dVLHpfjeUnkL2HxB/p8xSs93sC4e3pBnjmL+AD1uQ4doo3KgnTxxqraBx oLPk01VrVaxsJntE1BDydC1SVq9zejQqqc165wozP3ixpLA147dODOGDVnvXH645dk HbQesOJiUUOnCKwuX6m2CqMBx2qyxZo4SqdDLWS5Vp6wrmgzKbAMy7QtBfV1txt5mM y7zQpE1K1d8XFm1Zqt7jecqm5o/aKzjU0jsgpRkNPvJosIMTlhAxmuWAHHxfmJv32A 4NL+VyZaBa6jpziVxHQb0Ko5HjjI+V7QY7Cw8cTy5ItU9FCfddrOP/Q/x7O2en6zPt a36OVSbwOeKdw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id bG2vWyXgZXOV; Sat, 4 Jan 2025 21:59:58 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 3A2973C082EB9; Sat, 4 Jan 2025 21:59:58 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 40/59] Improve __tzfile_read TZ="" comment Date: Sat, 4 Jan 2025 21:57:15 -0800 Message-ID: <20250105055750.1668721-41-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_read): Improve comment about empty TZ strings. --- time/tzfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time/tzfile.c b/time/tzfile.c index d600b88e89..3ca08d16d4 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -504,7 +504,7 @@ __tzfile_read (const char *file) } tzspec[len] = '\0'; - /* Don't use an empty TZ string. */ + /* An empty TZ string means to use the last transition's type. */ if (len == 0) tzspec = NULL; } From patchwork Sun Jan 5 05:57:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104090 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 987313858D35 for ; Sun, 5 Jan 2025 06:45:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 987313858D35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=ReQoMUQ8 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 0607E385840C for ; Sun, 5 Jan 2025 06:00:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0607E385840C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0607E385840C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056802; cv=none; b=V93TPrRH6buiYK3wPJuW2UQfKZh4dl4DUnsNwtcHypdcQvulyab3wPIMIxn/emkJCa6o7ujuD6NWGwXSqopsQWHOgtc3D2HrLB+bu9pK+f0TLfV8v0eGnjDfjNvgtMzCO7dj7iwMUU2iA4jH0QilhL1A34NjCfR6CSNqUzjK9ZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056802; c=relaxed/simple; bh=fjKl4ALhN3YFb3ngqGf+M7fzojzXRahVubW5y3V8dGE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XZtSP5zyrYZrOjNS+xi/BUQZr38cegvqDdOwU+++JRIAxvHN/6gsjP9qUyaeNw3tmP7MJ8uW40Ophtgh2jfRifLmyoWlPjSie4Fd9dXjOWbt4VYqRQrogsKelPIQcqpoCPq2wfSKAZZ6uPfFrPlPJpT/OBxvjT2RZZHukWtsXR4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0607E385840C Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 7B6753C12385D for ; Sat, 4 Jan 2025 22:00:01 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id jkszgUdapwyw; Sat, 4 Jan 2025 22:00:01 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 3BC703C123844; Sat, 4 Jan 2025 22:00:01 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 3BC703C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056801; bh=2jPYN6dbZn4rCKXY2XD6yTO0fqUyKN4Alye336b+cFY=; h=From:To:Date:Message-ID:MIME-Version; b=ReQoMUQ8GkvHLrzD3Ed2PvhmUluEPcCyhm86uiu5l83Ck9e9EsEcPfih5px8A3DNd MMPRhhAKvvfd2+7bKEhovL1eJBMv7ilBQQCKS8tSg9jVvxVf0jvp1/NvpDA5VH8fHU wSi0YjHtsjdLnR5NBpN2ghIxNApF8V3WhUZfS8jcIlvmdgBYSHP85OUEavmC5T9OGK qDAoPj4IK+3ZAhsxWt41KxfMg2LGnahonavMNd4UPg/C4h5R1zJPqdtN86HjjgivNh J7GKBxY9zF7AoY/iFWlLd/82sy8PQ1faEvn+ZBdeiMxbNZ4wjLYKKJVujVAIFV+etZ mDUlizXXKuNnw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id QGkcXJei_EjE; Sat, 4 Jan 2025 22:00:01 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 20B3B3C082EB9; Sat, 4 Jan 2025 22:00:01 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 41/59] Refactor enum tz_rule_type Date: Sat, 4 Jan 2025 21:57:16 -0800 Message-ID: <20250105055750.1668721-42-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (enum tz_rule_type): Break out into a separate declaration and add comments. --- time/tzset.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/time/tzset.c b/time/tzset.c index af397d97dd..90ecdf4210 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -40,6 +40,23 @@ weak_alias (__timezone, timezone) /* This locks all the state variables in tzfile.c and this file. */ __libc_lock_define_initialized (, __tzset_lock) +/* Type of a DST rule date given in a proleptic TZ string. */ +enum tz_rule_type + { + /* No DST. */ + NO_DST, + + /* Zero-based Julian day counting any Feb. 29, e.g., ",59" + for Feb. 29 in leap years, Mar. 1 in nonleap years. */ + J0, + + /* One-based Julian day ignoring any Feb. 29, e.g., ",J59" for Mar. 1. */ + J1, + + /* Month m, week n, weekday d, e.g., ",M10.5.0" for Oct.'s last Sunday. */ + M + }; + /* This structure contains all the information about a timezone given in a proleptic TZ string. */ typedef struct @@ -47,7 +64,7 @@ typedef struct const char *name; /* When to change. */ - enum tz_rule_type { NO_DST, J0, J1, M } type; /* Interpretation of: */ + enum tz_rule_type type; /* Interpretation of: */ unsigned short int m, n, d; /* Month, week, day. */ int secs; /* Time of day. */ From patchwork Sun Jan 5 05:57:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104092 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AA5D53858C51 for ; Sun, 5 Jan 2025 06:48:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA5D53858C51 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=FJIPKJ/Y X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id EEF5A3858423 for ; Sun, 5 Jan 2025 06:00:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EEF5A3858423 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EEF5A3858423 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056805; cv=none; b=XdQPKk7cue3RhbWzXlJfgwvXGrmHAKnZXG3f8J1CZHaAFRYEVnNr0nnhHTpUkC4pxVwNDUAjTrQiR913ZAWGFfGkDzhgvEdsKERDbi7dv8AO/qs0LmnJl10HOLTuyHlWUJDbbkRkElFPQsaWgK2Ll9Lh8VDFYxIjmEficvvAB58= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056805; c=relaxed/simple; bh=N6s8aNO54J0iI6GJDY5Jk0CUuPS2+rEGLLVwoeafVC4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZsMUjPUKvPAWVnGLSpYhV14GsASYKrs/qG8EJx2+P09agQ0OK5HAoaKfGSflElmD83ybsBcDoXFXWNxaJHJR4r1Kw01O2HfBvm0uCM0xJCpqKTKcWpYotqTqG3+UBTxk7I0LmxdbCB3SUk2eT85FJd7hsfaviGkkkddf2Ae7ZlI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EEF5A3858423 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 6F5F63C12385E for ; Sat, 4 Jan 2025 22:00:04 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id OEkSoj3Wjq4E; Sat, 4 Jan 2025 22:00:04 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id F24153C123844; Sat, 4 Jan 2025 22:00:03 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu F24153C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056804; bh=AlAFCY850XYLwr/7FrRXkPmF+H/w6pZ97erZLkwf+nA=; h=From:To:Date:Message-ID:MIME-Version; b=FJIPKJ/YMi1Ed36zRRVivI3OFzdzQJrte8T/JyFNLaI0trfMe8NZowR55gk0alq4b Qngkrpjdiw5HPq5H6jf0/D+dSta/500eUr4BZ6Ybhqp2G5qylybRAanFkzbnVIQk8L tB/zytN1neIYICiFtb6MHAupxeJsch569r37sDIMP5aUSUtoJ61DsEtJK5o2U0km7A 76jwN0CLKygc4EnkmrAqKSnn/XHPnarHRKLgEG8128EWEim/CtP72vlfpJ4n5qFlBk ki+We5U246azODiKBL/KZL23zzCXo5gaph4oVObGDfP328mtSwQgwURwv1lrcVPsB3 j84ZvotW7IIiw== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id FrVz7h7GWVck; Sat, 4 Jan 2025 22:00:03 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id D2BAC3C082EB9; Sat, 4 Jan 2025 22:00:03 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 42/59] __offtime now returns struct tm * Date: Sat, 4 Jan 2025 21:57:17 -0800 Message-ID: <20250105055750.1668721-43-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/offtime.c (__offtime): Refactor by returning struct tm * instead of int. All callers changed. --- include/time.h | 8 ++++---- time/offtime.c | 8 ++++---- time/tzfile.c | 2 +- time/tzset.c | 7 +++---- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/include/time.h b/include/time.h index e9c083646a..186ea657cb 100644 --- a/include/time.h +++ b/include/time.h @@ -250,10 +250,10 @@ libc_hidden_proto (__gettimeofday64) /* Compute the `struct tm' representation of T, offset OFFSET seconds east of UTC, and store year, yday, mon, mday, wday, hour, min, sec into *TP. - Return nonzero if successful. */ -extern int __offtime (__time64_t __timer, - long int __offset, - struct tm *__tp) attribute_hidden; + Return TP if successful, a null pointer otherwise. */ +extern struct tm *__offtime (__time64_t __timer, + long int __offset, + struct tm *__tp) attribute_hidden; extern char *__asctime_r (const struct tm *__tp, char *__buf) attribute_hidden; diff --git a/time/offtime.c b/time/offtime.c index c94573e931..89cdcc4b15 100644 --- a/time/offtime.c +++ b/time/offtime.c @@ -24,8 +24,8 @@ /* Compute the `struct tm' representation of T, offset OFFSET seconds east of UTC, and store year, yday, mon, mday, wday, hour, min, sec into *TP. - Return nonzero if successful. */ -int + Return TP if successful, a null pointer otherwise. */ +struct tm * __offtime (__time64_t t, long int offset, struct tm *tp) { __time64_t days, rem, y; @@ -73,7 +73,7 @@ __offtime (__time64_t t, long int offset, struct tm *tp) { /* The year cannot be represented due to overflow. */ __set_errno (EOVERFLOW); - return 0; + return NULL; } tp->tm_yday = days; ip = __mon_yday[__isleap(y)]; @@ -82,5 +82,5 @@ __offtime (__time64_t t, long int offset, struct tm *tp) days -= ip[y]; tp->tm_mon = y; tp->tm_mday = days + 1; - return 1; + return tp; } diff --git a/time/tzfile.c b/time/tzfile.c index 3ca08d16d4..9c05811e22 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -648,7 +648,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, /* Convert to broken down structure. If this fails do not use the string. */ - if (__glibc_unlikely (! __offtime (timer, 0, tp))) + if (__glibc_unlikely (__offtime (timer, 0, tp) == NULL)) goto use_last; /* Use the rules from the TZ string to compute the change. */ diff --git a/time/tzset.c b/time/tzset.c index 90ecdf4210..671394f02d 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -610,9 +610,8 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) &leap_extra_sec, tp); else { - if (! __offtime (timer, 0, tp)) - tp = NULL; - else if (use_localtime) + tp = __offtime (timer, 0, tp); + if (tp && use_localtime) __tz_compute (timer, tp); leap_correction = 0; leap_extra_sec = false; @@ -627,7 +626,7 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) tp->tm_gmtoff = 0L; } - if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp)) + if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp) != NULL) { /* This assumes leap seconds can occur only when the local time offset from UTC is a multiple of 60 seconds, From patchwork Sun Jan 5 05:57:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104094 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 51E063858D21 for ; Sun, 5 Jan 2025 06:51:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 51E063858D21 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=ar6z/b0a X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 8D0E8385840F for ; Sun, 5 Jan 2025 06:00:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D0E8385840F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8D0E8385840F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056807; cv=none; b=YwCs/F3YzABp55mzp053sMWbTp4lUcL9RWd8ACJxCUpvg/jSQaDED+NSVXYf1Bg3sMZjkSahvQnLAlvJ2QfvN7wbAt8l73VjaOyhcgb3SovDXOkt3dWBggkrIFHhBI7nV9U4H1G5aynzMLsGnFWx0Hm0MzbNmrKTFfsN5PFdfN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056807; c=relaxed/simple; bh=QNjXjNvTJg/RhL0ANO4kcsvIwUkfJHcVMQ6kUh1MV0Q=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nU/Ujix74rN4b5+85hiCbH1OtBluDBoOhaFQbD8RBuFW67wlC6NI8YfljrSH7svXW4LM2ftfMr8hbTyRsALxZqpymgu7JPIxiAvv81P2u/+hTWh+RduRhgHcwY/5r/uqwvlYBAIHM4ZbIexVF4P83eJuXxZ+n1IYJ2j0gXck6N0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D0E8385840F Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 08CC33C12385F for ; Sat, 4 Jan 2025 22:00:07 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id e17NlgCA9B31; Sat, 4 Jan 2025 22:00:06 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 67C6F3C123844; Sat, 4 Jan 2025 22:00:06 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 67C6F3C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056806; bh=rfDw/Bx2Tujfkws3JkTBEcoFwhv8WVBdxW7LCkYI5w0=; h=From:To:Date:Message-ID:MIME-Version; b=ar6z/b0aMOYI06ZDuQWqTo54rTx03GRqGUoc0Vq7mFOhKwMmU/zfmBeehx5VsI2W3 Gl2iP3wbziyapu7SYujTCr5dtMrU2VWl0iw4EkS+wBaST48KjAqJrB5gJHh+LzCn2I ycRT5eXBmqCPQ7/ZFN61q9hfvcydEIW/3T32VKaILW6q1BSmn4g68pKPSQ6JUZ3/ZI udcGjKFEGA/sbomdCrYTwbG+2pEx2iYTGJrtshxEJzIarfq3So41IAWemHnGHminhl +aTZsEVwn6uXVNXiAEa3I3dIcQl4ahlT47lBjlaoX8sTivEXxghu2PgYbXRCGS1T6a q5OpRkgOIRdeA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id bGgm75JayEwQ; Sat, 4 Jan 2025 22:00:06 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 486863C082EB9; Sat, 4 Jan 2025 22:00:06 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 43/59] Fix overflow checking in offtime Date: Sat, 4 Jan 2025 21:57:18 -0800 Message-ID: <20250105055750.1668721-44-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/offtime.c: Include . (__offtime): New arg CORR, so that overflow cannot happen when the caller subtracts it from OFFSET. All callers changed. Replace loops with straight-line code. Do not set *TP unless successful. Do not rely on undefined behavior when checking for year overflow. Use narrower integers when computing mon and mday. --- include/time.h | 10 ++++---- time/offtime.c | 64 +++++++++++++++++++++++--------------------------- time/tzfile.c | 2 +- time/tzset.c | 4 ++-- 4 files changed, 37 insertions(+), 43 deletions(-) diff --git a/include/time.h b/include/time.h index 186ea657cb..e460037ee2 100644 --- a/include/time.h +++ b/include/time.h @@ -247,12 +247,12 @@ extern int __gettimeofday64 (struct __timeval64 *restrict tv, libc_hidden_proto (__gettimeofday64) #endif -/* Compute the `struct tm' representation of T, - offset OFFSET seconds east of UTC, - and store year, yday, mon, mday, wday, hour, min, sec into *TP. - Return TP if successful, a null pointer otherwise. */ +/* Compute the 'struct tm' representation of T, offset OFFSET seconds + east of UTC and minus a leap second correction CORR. + If successful, store year, yday, mon, mday, wday, hour, min, sec + into *TP and return TP; otherwise return a null pointer. */ extern struct tm *__offtime (__time64_t __timer, - long int __offset, + long int __offset, int __corr, struct tm *__tp) attribute_hidden; extern char *__asctime_r (const struct tm *__tp, char *__buf) diff --git a/time/offtime.c b/time/offtime.c index 89cdcc4b15..9fce47c01d 100644 --- a/time/offtime.c +++ b/time/offtime.c @@ -16,43 +16,28 @@ . */ #include +#include #include #define SECS_PER_HOUR (60 * 60) #define SECS_PER_DAY (SECS_PER_HOUR * 24) -/* Compute the `struct tm' representation of T, - offset OFFSET seconds east of UTC, - and store year, yday, mon, mday, wday, hour, min, sec into *TP. - Return TP if successful, a null pointer otherwise. */ +/* Compute the 'struct tm' representation of T, offset OFFSET seconds + east of UTC and minus a leap second correction CORR. + If successful, store year, yday, mon, mday, wday, hour, min, sec + into *TP and return TP; otherwise return a null pointer. */ struct tm * -__offtime (__time64_t t, long int offset, struct tm *tp) +__offtime (__time64_t t, long int offset, int corr, struct tm *tp) { - __time64_t days, rem, y; - const unsigned short int *ip; + int rem = (t % SECS_PER_DAY + offset % SECS_PER_DAY - corr % SECS_PER_DAY + + 3 * SECS_PER_DAY); + __time64_t days = (t / SECS_PER_DAY + offset / SECS_PER_DAY + - corr / SECS_PER_DAY + rem / SECS_PER_DAY - 3); - days = t / SECS_PER_DAY; - rem = t % SECS_PER_DAY; - rem += offset; - while (rem < 0) - { - rem += SECS_PER_DAY; - --days; - } - while (rem >= SECS_PER_DAY) - { - rem -= SECS_PER_DAY; - ++days; - } - tp->tm_hour = rem / SECS_PER_HOUR; - rem %= SECS_PER_HOUR; - tp->tm_min = rem / 60; - tp->tm_sec = rem % 60; /* January 1, 1970 was a Thursday. */ - tp->tm_wday = (4 + days) % 7; - if (tp->tm_wday < 0) - tp->tm_wday += 7; - y = 1970; + int wday_rem = (4 + days) % 7; + + __time64_t y = 1970; #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0)) #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) @@ -68,19 +53,28 @@ __offtime (__time64_t t, long int offset, struct tm *tp) - LEAPS_THRU_END_OF (y - 1)); y = yg; } - tp->tm_year = y - 1900; - if (tp->tm_year != y - 1900) + int year; + if (ckd_sub (&year, y, 1900)) { /* The year cannot be represented due to overflow. */ __set_errno (EOVERFLOW); return NULL; } + + int day_rem = rem % SECS_PER_DAY; + tp->tm_hour = day_rem / SECS_PER_HOUR; + int hour_rem = day_rem % SECS_PER_HOUR; + tp->tm_sec = hour_rem % 60; + tp->tm_min = hour_rem / 60; + tp->tm_year = year; + tp->tm_wday = wday_rem + (wday_rem < 0 ? 7 : 0); tp->tm_yday = days; - ip = __mon_yday[__isleap(y)]; - for (y = 11; days < (long int) ip[y]; --y) + unsigned short int yday = days; + unsigned short int const *ip = __mon_yday[__isleap (y)]; + int mon; + for (mon = 11; yday < ip[mon]; mon--) continue; - days -= ip[y]; - tp->tm_mon = y; - tp->tm_mday = days + 1; + tp->tm_mon = mon; + tp->tm_mday = yday - ip[mon] + 1; return tp; } diff --git a/time/tzfile.c b/time/tzfile.c index 9c05811e22..01821cc368 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -648,7 +648,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, /* Convert to broken down structure. If this fails do not use the string. */ - if (__glibc_unlikely (__offtime (timer, 0, tp) == NULL)) + if (__glibc_unlikely (__offtime (timer, 0, 0, tp) == NULL)) goto use_last; /* Use the rules from the TZ string to compute the change. */ diff --git a/time/tzset.c b/time/tzset.c index 671394f02d..add0073e0d 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -610,7 +610,7 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) &leap_extra_sec, tp); else { - tp = __offtime (timer, 0, tp); + tp = __offtime (timer, 0, 0, tp); if (tp && use_localtime) __tz_compute (timer, tp); leap_correction = 0; @@ -626,7 +626,7 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) tp->tm_gmtoff = 0L; } - if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp) != NULL) + if (__offtime (timer, tp->tm_gmtoff, leap_correction, tp) != NULL) { /* This assumes leap seconds can occur only when the local time offset from UTC is a multiple of 60 seconds, From patchwork Sun Jan 5 05:57:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104077 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 206473858D28 for ; Sun, 5 Jan 2025 06:31:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 206473858D28 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=CzdXFcip X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 0C6DB3858401 for ; Sun, 5 Jan 2025 06:00:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C6DB3858401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0C6DB3858401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056810; cv=none; b=lKViO7rrKQV/FyNluPobyRqSrDMR53DL5UJJFH0ImYxbuSWnGKZuOywFmAqEFOvRZj5+EzO1FOm50mW20Sxvp+b+sEZd+ls7XpUqjWbEyAjKwpRyADhpdeNfQOTvvbzPR9mHMFrnXH3OTz6Cm2Dmmvw+TcWr3mg73jiHozNsgKs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056810; c=relaxed/simple; bh=yf5WEZ0CUpXbVZT6tRZ6UZz5y94tKQnYbYhu32ZqCxk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=t3ER2CJGcmPcoC2HXHkELcTVtOOOjMtHiWac0UmMTVdpyrjGMRxyFMIni3QZZsnYaVcBH/So/XxdQJbx6QqVrjyUcai/J8DF3NGSkkideZSxFpF1E4z5ZpjRTmaM4hb0b2BpLaO8S9Nfh1plRgg5OjfcEMfnH7rdNoWuChzM87k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0C6DB3858401 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 770373C123861 for ; Sat, 4 Jan 2025 22:00:09 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id NcAzQOw3xy2W; Sat, 4 Jan 2025 22:00:09 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 179C83C123844; Sat, 4 Jan 2025 22:00:09 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 179C83C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056809; bh=rdxoPQn1w4AWa3ivffzwQDnbWoqA6hqvAmGfsz330TA=; h=From:To:Date:Message-ID:MIME-Version; b=CzdXFcipXk0UpUI9Z99PVuI86N0ASYK2+ATCeyHXlFNp7N7cMYuzRmEZiiT3xO47h TIq4sET6voV2eAqb2qFP+13GI2SbLZ86NhOo3jCaVrcJIpUh1lPB1CvIC4glst+Us5 T2ja7TXZtvhoFZ+mY7Bb60BBF4SrZrQcWUmpKN95/ObKVzQpG6LAdHDX/AkJIRjDlM D+0ieDvJab5gtrhtuuj4vQbNJuNb5DftPqAl9WGmaYlR+xUalntqer4gyzc6pTo9kt rw30S8yLfxN4YZbc1gTWMZATD0oNfJoLq/ZI3G0NNzVdvPv9o8lDLayC7/woKhcA+L OXnK88a0guORA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id gel28sGeJ7D1; Sat, 4 Jan 2025 22:00:09 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id F3B403C082EB9; Sat, 4 Jan 2025 22:00:08 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 44/59] Handle TZif time type 0 correctly Date: Sat, 4 Jan 2025 21:57:19 -0800 Message-ID: <20250105055750.1668721-45-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): For time stamps before the first transition, or for all transitions if no TZ string is specified, use time type 0 as specified by Internet RFC 9636 section 3.2. Omit an unnecessary loop to set tzname, daylight, and timezone as that behavior is undocumented, POSIX does not require it, and it does not make sense in general. Instead, just set tzname[isdst] as that is all that makes sense (particularly in light of the time type 0 fix) and that might conceivably help old-fashioned apps. --- time/tzfile.c | 88 ++++++++++----------------------------------------- 1 file changed, 17 insertions(+), 71 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 01821cc368..dd5973b76d 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -600,40 +600,16 @@ __tzfile_compute (__time64_t timer, int use_localtime, if (use_localtime) { - __tzname[0] = NULL; - __tzname[1] = NULL; + unsigned char ti; - if (__glibc_unlikely (timecnt == 0 || timer < transitions[0])) + if (__glibc_unlikely (timecnt == 0 ? tzspec == NULL + : timer < transitions[0])) { - /* TIMER is before any transition (or there are no transitions). - Choose the first non-DST type - (or the first if they're all DST types). */ - i = 0; - while (i < typecnt && types[i].dst) - { - if (__tzname[1] == NULL) - __tzname[1] = __tzstring (&zone_names[types[i].idx]); - - ++i; - } - - if (i == typecnt) - i = 0; - __tzname[0] = __tzstring (&zone_names[types[i].idx]); - if (__tzname[1] == NULL) - { - tzidx j = i; - while (j < typecnt) - if (types[j].dst) - { - __tzname[1] = __tzstring (&zone_names[types[j].idx]); - break; - } - else - ++j; - } + /* TIMER is before the first transition; or there are no + transitions and no TZ string. Use the first type. */ + ti = 0; } - else if (timer >= transitions[timecnt - 1]) + else if (timecnt == 0 || timer >= transitions[timecnt - 1]) { if (__glibc_unlikely (tzspec == NULL)) { @@ -710,49 +686,19 @@ __tzfile_compute (__time64_t timer, int use_localtime, i = hi; found: - /* assert (timer >= transitions[i - 1] - && (i == timecnt || timer < transitions[i])); */ - __tzname[types[type_idxs[i - 1]].dst] - = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]); - - for (tzidx j = i; j < timecnt; j++) - { - struct ttinfo *ttype = &types[type_idxs[j]]; - unsigned char dst = ttype->dst; - - if (__tzname[dst] == NULL) - { - __tzname[dst] = __tzstring (&zone_names[ttype->idx]); - - if (__tzname[1 - dst] != NULL) - break; - } - } - - if (__glibc_unlikely (__tzname[0] == NULL)) - __tzname[0] = __tzname[1]; - - i = type_idxs[i - 1]; + ti = type_idxs[i - 1]; } - struct ttinfo *info = &types[i]; - __daylight = daylight_saved; - __timezone = -rule_stdoff; - - if (__tzname[0] == NULL) - { - /* This should only happen if there are no transition rules. - In this case there should be only one single type. */ - assert (typecnt == 1); - __tzname[0] = __tzstring (zone_names); - } - if (__tzname[1] == NULL) - /* There is no daylight saving time. */ - __tzname[1] = __tzname[0]; - tp->tm_isdst = info->dst; - assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0); - tp->tm_zone = __tzname[tp->tm_isdst]; + struct ttinfo *info = &types[ti]; + unsigned char dst = info->dst; + char *tm_zone = __tzstring (&zone_names[info->idx]); + tp->tm_isdst = dst; tp->tm_gmtoff = info->utoff; + tp->tm_zone = tm_zone; + + /* Set tzname[isdst] too; although not required by POSIX or documented, + this is closer to what proleptic TZ does. */ + __tzname[dst] = tm_zone; } leap: From patchwork Sun Jan 5 05:57:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104058 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5CA533858415 for ; Sun, 5 Jan 2025 06:18:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id A527D3858404 for ; Sun, 5 Jan 2025 06:00:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A527D3858404 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A527D3858404 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056812; cv=none; b=LmgBPM2XrPLllR1Q5G3Obz6EDBxrHmJzbfHO6XM3GBlTHIWEznra3KAxPagPjAkCu27vleOcb1AzFBypdQwJMWIV75N4jyZvhnIaX1/4psRCuW4pQPMSIAmNY4enL54rbZKkGe6nRw2oDtjd/Xed7VBP7Uz8id6V9CckvvkyNrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056812; c=relaxed/simple; bh=kolG9VCY/IDf2qXFOITLTNZXtHmmsfAAUVIKoQrBjeU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NYA8a5H4z6SSSG2XbTlR6tH1iwdpnmhVAqvHpFkmcyR5DsPH8GvcEjtNwlADn5PCMG4fJ2eoM2KhC3gNZXZRWyAdI5MULpnvMsiFZlQ4yPNz1Jlo9z9pcoDFFkyUHp8BbJ89f430zkcvqIt8525FiaUMsN0oAeeqbLXg3I6eAlo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A527D3858404 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=YozidVxv Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 2AE8A3C123843 for ; Sat, 4 Jan 2025 22:00:12 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id yJS-vd7TnqK2; Sat, 4 Jan 2025 22:00:11 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D81D03C123844; Sat, 4 Jan 2025 22:00:11 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu D81D03C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056811; bh=bvjqZx9x5iorzgKg/SAlYRhMa+L9n5m36jM2Wvfp2nU=; h=From:To:Date:Message-ID:MIME-Version; b=YozidVxv0oDGkhNtsL14viFsfVDujEn1Z9HL0STAnok3bV3jACUosFnztNf4CMB7d S/9x6IKaU4JRcHsVzZLMk9MzzU1WR80YWPxRWG04nOwo26D6nUvcivPCblaZj80Q/m AOAXMK7Vu9eQhozIF9dP+bUZrNEBYZRiBMabcDUWNh41tZuS8qWmYfhIYaT3+eduIs S3tzBkXwkmn6XlW+jAFuKLJ79Mn+dfnqQ7ofNrCMVDNMDMMso7wLispYjcCcZjoyrV PGV7d0oq3JFnmN2n5aC56RhZb5cywOn8tyDRRyIlilePIxfN4vg8xr+/yVJ5+Cdx48 uYAaHTdeoUSoQ== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id fp6mdkqigjfg; Sat, 4 Jan 2025 22:00:11 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id BC7BA3C082EB9; Sat, 4 Jan 2025 22:00:11 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 45/59] Remove invalid tzset tests from tst-bz29951.c Date: Sat, 4 Jan 2025 21:57:20 -0800 Message-ID: <20250105055750.1668721-46-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * timezone/tst-bz29951.c (set_timezone): Do not test errno after tzset, as its value is unspecified then. (do_test): Do not test daylight or timezone after tzset, as their values are unspecified with geographical TZ. See Austin Group Defect 1816 . --- timezone/tst-bz29951.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/timezone/tst-bz29951.c b/timezone/tst-bz29951.c index ff0eb6ff1e..ee68c5c143 100644 --- a/timezone/tst-bz29951.c +++ b/timezone/tst-bz29951.c @@ -26,9 +26,7 @@ static void set_timezone (const char *name) { TEST_VERIFY (setenv ("TZ", name, 1) == 0); - errno = 0; tzset (); - TEST_COMPARE (errno, 0); } static int @@ -44,15 +42,9 @@ do_test (void) = Fri Oct 25 02:00:00 2013 EET isdst=0 gmtoff=7200 */ set_timezone ("XT6"); - TEST_VERIFY (daylight != 0); - TEST_COMPARE (timezone, -7200); - /* Check that localtime re-initializes the two variables. */ - daylight = timezone = 17; time_t t = 844034401; struct tm *tm = localtime (&t); - TEST_VERIFY (daylight != 0); - TEST_COMPARE (timezone, -7200); TEST_COMPARE (tm->tm_year, 96); TEST_COMPARE (tm->tm_mon, 8); TEST_COMPARE (tm->tm_mday, 29); From patchwork Sun Jan 5 05:57:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104095 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 33C893858C66 for ; Sun, 5 Jan 2025 06:52:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33C893858C66 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=OY3JPr0B X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 6F13B385840E for ; Sun, 5 Jan 2025 06:00:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F13B385840E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6F13B385840E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056816; cv=none; b=To6uCafs4YyavD2vg0xHkG9VZnE6iixF4QwGFvfCmRNVJTyzO8yHf3hU3ues3uJRC3SuPEYET0XsV9nrt/HMwsh1RDgmS5A4IF4MQk/Du+0/Rah6obLLkuj14jhHnfU7dtfazlH7lBSSyK9tNYB2VxMwgtiCmFbPw6AtPGu5tt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056816; c=relaxed/simple; bh=+HJejJq8mlrJZL8xb4OoD6GP7O9C5UqoYF3GukVrL+M=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jCtfO8d5oQkAaa2q1hIAiOzS+1JasyFtEgUsGL3F1vK9Asd0u6i+InqvOevfAstrnKolWEbKGzwvmuA4qtZ+lVYFYJvXN2z7zlzTsaZ9bEXwXOW+bRQmYwDvrTsUAG9VuYXrfl5tb7vnquewQgTKl1SuWS0pGJE2vdkxtre5QiM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6F13B385840E Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 1C5D93C123845 for ; Sat, 4 Jan 2025 22:00:15 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id PTuIguwB0tgi; Sat, 4 Jan 2025 22:00:14 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 972653C123844; Sat, 4 Jan 2025 22:00:14 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 972653C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056814; bh=X28icvuKlE8Tz0ZRD05/iOd3ZB/yQzzLPRPh6W7f60A=; h=From:To:Date:Message-ID:MIME-Version; b=OY3JPr0B2EJM6tfb15hocvGiXGn0zKxCi5dGgrIk3gLejmfEMqjJjq9lvpvgYqOVu s1q+fOm4w17+T8as2lvXToLWvE4CXqcgL8ASe2kje5ePO6kw+a+3Poc8l0GA12oEJm JyJyw0x9t9M9PMaj2HqevDkeDyR5p/DX97B6xePt4OvBipLNLycQGafy441FBwPMMs uVWx8J93IMkx2IOYh6eVlnpmF2iYUVkjvcYdWhQ/OJv75A0UF4FtXIfuyjePd+3PLU XP7jvfBC56jg3UqZoG1bnrcsHr+Gru5plTfunCDaSthbvmganpI+99MW5rHVeirHAZ 1SeW62kRlUL/A== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id ewGVv2lg6eIX; Sat, 4 Jan 2025 22:00:14 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 801023C082EB9; Sat, 4 Jan 2025 22:00:14 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 46/59] tzset, not localtime, now parses TZif TZ spec Date: Sat, 4 Jan 2025 21:57:21 -0800 Message-ID: <20250105055750.1668721-47-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org It's typically faster to parse a TZif file's TZ spec once after TZ changes instead of parsing it on every call to localtime, as most slim TZif files' last transitions precede the current time. * time/tzfile.c (rule_stdoff, daylight_saved): Remove static vars. All uses removed. (__tzfile_read): Parse the tzspec here. Simplify the guesswork for tzname etc.: if a tzspec exists, use its proleptic values as this is more likely to be what obsolescent code wants; otherwise, simplify three loops into one. (__tzfile_compute): Do not parse tzspec, as tzset now does that. * time/tzset.c (tzset_internal): When TZ hasn't changed, update external vars only if TZ is proleptic. (tzset_internal): Do not attempt to migrate __tzfile_read's settings of tzname etc. into tz_rules as that would mess up later use by __tzfile_compute of a parsed tzspec. --- time/tzfile.c | 99 ++++++++++++++++++++------------------------------- time/tzset.c | 27 +++++--------- 2 files changed, 47 insertions(+), 79 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index dd5973b76d..f86df0c8d5 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -64,15 +64,10 @@ static unsigned char *type_idxs; static tzidx typecnt; static struct ttinfo *types; static char *zone_names; -static long int rule_stdoff; static tzidx leapcnt; static struct leap *leaps; static char *tzspec; -/* Used to restore the daylight variable during time conversion, as if - tzset had been called. */ -static int daylight_saved; - #include #include @@ -516,65 +511,53 @@ __tzfile_read (const char *file) if (__tzstring (&zone_names[types[i].idx]) == NULL) goto ret_free_transitions; - /* Find the standard and daylight time offsets used by the rule file. - We choose the offsets in the types of each flavor that are - transitioned to earliest in time. */ - __tzname[0] = NULL; - __tzname[1] = NULL; - for (i = timecnt; i > 0; i--) - { - struct ttinfo *ttype = &types[type_idxs[i - 1]]; - unsigned char dst = ttype->dst; - - if (__tzname[dst] == NULL) - { - __tzname[dst] = __tzstring (&zone_names[ttype->idx]); + /* POSIX does not say what daylight, timezone and tzname should be; + try to set them to something reasonable. */ - if (__tzname[1 - dst] != NULL) - break; - } - } - if (__tzname[0] == NULL) + if (tzspec != NULL) { - /* This should only happen if there are no transition rules. - In this case there's usually only one single type, unless - e.g. the data file has a truncated time-range. */ - __tzname[0] = __tzstring (zone_names); + /* The usual case with tzspec; just use its values. */ + if (__glibc_unlikely (! __tzset_parse_tz (tzspec))) + goto ret_free_transitions; } - if (__tzname[1] == NULL) - __tzname[1] = __tzname[0]; - - daylight_saved = 0; - if (timecnt == 0) - /* Use the first rule (which should also be the only one). */ - rule_stdoff = types[0].utoff; else { - rule_stdoff = 0; - - /* Search for the last rule with a standard time offset. This - will be used for the global timezone variable. */ - i = timecnt - 1; - do - if (!types[type_idxs[i]].dst) - { - rule_stdoff = types[type_idxs[i]].utoff; + /* The unusual case without tzspec; infer from transitions. + Default to the first type's offset and isdst. */ + __timezone = -types[0].utoff; + __daylight = types[0].dst; + + /* Find the abbr and offset for the last transition to standard time, + and the abbr for the last transition to DST. */ + __tzname[0] = NULL; + __tzname[1] = NULL; + for (i = timecnt; i > 0; i--) + { + struct ttinfo *ttype = &types[type_idxs[i - 1]]; + unsigned char dst = ttype->dst; + + if (__tzname[dst] == NULL) + { + __tzname[dst] = __tzstring (&zone_names[ttype->idx]); + if (!dst) + __timezone = -ttype->utoff; + } + + if (__tzname[1 - dst] != NULL) break; - } - else - daylight_saved = 1; - while (i-- > 0); - - /* Keep searching to see if there is a DST rule. This - information will be used to set the global daylight - variable. */ - while (i-- > 0 && !daylight_saved) - daylight_saved = types[type_idxs[i]].dst; + } + /* With no appropriate transitions, default standard time's + abbreviation to that of the first type, and default DST's + abbreviation to that of standard time. Also, set 'daylight' + if a DST transition was found. */ + if (__tzname[0] == NULL) + __tzname[0] = __tzstring (&zone_names[types[0].idx]); + if (__tzname[1] == NULL) + __tzname[1] = __tzname[0]; + else + __daylight = 1; } - __daylight = daylight_saved; - __timezone = -rule_stdoff; - /* Remember the inode and device number and modification time. */ tzfile_dev = st.st_dev; tzfile_ino = st.st_ino; @@ -618,10 +601,6 @@ __tzfile_compute (__time64_t timer, int use_localtime, goto found; } - /* Parse the proleptic TZ string. If this fails do not use it. */ - if (__glibc_unlikely (! __tzset_parse_tz (tzspec))) - goto use_last; - /* Convert to broken down structure. If this fails do not use the string. */ if (__glibc_unlikely (__offtime (timer, 0, 0, tp) == NULL)) diff --git a/time/tzset.c b/time/tzset.c index add0073e0d..b5bbd05573 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -412,15 +412,18 @@ tzset_internal (bool always) tz = TZDEFAULT; /* A leading colon means "implementation defined syntax". - We ignore the colon and always use the same algorithm: + Ignore the colon and always use the same algorithm: try a data file, and if none exists parse the 1003.1 syntax. */ if (*tz == ':') ++tz; - /* If the value changed has not since the last run, - simply update internal vars. */ + /* If the value has not changed since the last run, + simply update external vars if POSIX requires it. */ if (old_tz != NULL && strcmp (tz, old_tz) == 0) - update_vars (); + { + if (!use_tzfile) + update_vars (); + } else { /* Save the value of 'tz'. */ @@ -429,21 +432,7 @@ tzset_internal (bool always) /* Try to read a data file. */ use_tzfile = __tzfile_read (tz); - if (use_tzfile) - { - /* Save equivalent of 'daylight' for later use by update_vars. - Although the other external variables have unspecified values - and so need not be saved in the usual case, save them anyway, - as POSIX requires this for rare file-backed proleptic - TZ strings like "EST5EDT", and it is more likely to match user - expectations for geographical TZ strings. */ - enum tz_rule_type some_DST = J0; /* anything but NO_DST */ - tz_rules[1].type = NO_DST + __daylight * (some_DST - NO_DST); - tz_rules[0].offset = -__timezone; - tz_rules[0].name = __tzname[0]; - tz_rules[1].name = __tzname[1]; - } - else + if (!use_tzfile) { /* No data file found. Default to UTC without leap seconds if TZDEFAULT is broken. */ From patchwork Sun Jan 5 05:57:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104063 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C2A93385840B for ; Sun, 5 Jan 2025 06:19:05 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 9F9F6385842C for ; Sun, 5 Jan 2025 06:00:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9F9F6385842C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9F9F6385842C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056817; cv=none; b=aJFr9u9LMiFJ6zEjddsZrpQ7fZHQpiBy7qypPVcD+yPE3p17lLp2Iht61MziwoLg6bCIrpYUeD/qDcg7jypQBGTvO/ukR54/6Jwuyu8zcU5LV9GP/d4xZscwfjYTfsLBd2hXJkzuEZJAOjLkAYshTj/bEn52+brwMKmsFzDpies= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056817; c=relaxed/simple; bh=1L3MNvU0Mhue9HLN+nmR7USia6oroscReyJH+5NPDw0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oY+hIqXpA8/ZnyKjxpw4rUXVsHVqwo0hIwvn7Uupb5vDnQrnhk2BuWK7ubNT8eQQ4PQiaUlqyDwG1NfFr/NN1ZfCqSEjgpMpMaQRpEArSMD6doV70I3Rordy2KYzv96uFh/IiPRwPGRM/aIhnkhw6cQ++sXX0u1yhAea6SoJaHA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F9F6385842C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=NcorpVSS Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 228313C123847 for ; Sat, 4 Jan 2025 22:00:17 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id qvuLPJcrROl3; Sat, 4 Jan 2025 22:00:16 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id D45F33C123844; Sat, 4 Jan 2025 22:00:16 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu D45F33C123844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056816; bh=ADc5JthCh5yOh6pHWAuXVU2Q95ufdm46aSbboRrxZP0=; h=From:To:Date:Message-ID:MIME-Version; b=NcorpVSSZkX2G6Wt+uOlQesl1+d9jVAhEGlFDVZjNjLIF/siHD9OuI8pU8/aDKo+M sx/TAKyXaqxIs4mTkeuwLfx/pqMXXQIflR55el3R8zSJHTZVz/SvGZlSBkGxgTFp38 TDmNBfMWz9L3+I22t7YKWjMaI6uLAf7iC3tjgWAmvE+p+CTGCuMXurQ8gicY5lWTku c3SlDxhzIjvhf4ODm6RAkYYkDU9HqgRaE0SJQ3Vv5CKoBpY3kDasyrgGOumXO8cAd8 0m8SGGpSpegVKGwTkFiIVkAqHwbxRaDfcm9Rf9pPiWYXsLoMtySZwdtRz0jQ6URlDT oaKetd5hvotBg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 9iPF3N-1pG2w; Sat, 4 Jan 2025 22:00:16 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id B0F713C082EB9; Sat, 4 Jan 2025 22:00:16 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 47/59] Minor compute_change widening tweak Date: Sat, 4 Jan 2025 21:57:22 -0800 Message-ID: <20250105055750.1668721-48-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (compute_change): Subtract before widening. This can't overflow. --- time/tzset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time/tzset.c b/time/tzset.c index b5bbd05573..f3af960620 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -533,7 +533,7 @@ compute_change (tz_rule *rule, long long int year) /* T is now the Epoch-relative time of 00:00:00 UT on the day we want. Just subtract the UT offset and add the time of day, and we're done. */ - rule->change = t - rule->offset + rule->secs; + rule->change = t - (rule->offset - rule->secs); rule->computed_for = year; } From patchwork Sun Jan 5 05:57:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104081 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EF39F3858D28 for ; Sun, 5 Jan 2025 06:35:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EF39F3858D28 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=HKczpfQT X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id C5F50385841D for ; Sun, 5 Jan 2025 06:00:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C5F50385841D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C5F50385841D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056821; cv=none; b=kCG44JhAbb6E2FM9gLK2oORR7t7gVg6Awp2S9YP9+TLyEKgFVBx93Oo8/HM96xz9Mn0tuVwNSZbpz955/oCzejpN4SznxOhSJpozr+msn7poNOzeiY45lyB32zPcgAynkV6QZF1CfgUYrmoUmpnR7UTbBCGluiWTuLkaca/82bo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056821; c=relaxed/simple; bh=1msb6fQcOSpZ/nWTBfsoo0Ukdeis3HYCS53QdDOh95Q=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HhhnuvmLeSrg3v0VAaPJC9o+nbrFSGDL0qRx8vPCUwzx5h+ORvoBYxUfV9saCa6RyGdQTB3wQ5lU1W0JB1wuuKfyXAK9s0ZoVI5enXWBg82yCEZEIEhiWvLVUusOnneddilxubLEB1lckXNW4isQQrm0s9Bw6kMrYyxBkLoTlCQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5F50385841D Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 4B8173C12385D for ; Sat, 4 Jan 2025 22:00:21 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id Bfl3PSm1T7r0; Sat, 4 Jan 2025 22:00:19 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 5D3173C123848; Sat, 4 Jan 2025 22:00:19 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 5D3173C123848 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056819; bh=1msb6fQcOSpZ/nWTBfsoo0Ukdeis3HYCS53QdDOh95Q=; h=From:To:Date:Message-ID:MIME-Version; b=HKczpfQTC5+sjw43ymx6V4x1dmIS5RpSO10EvgAGVp1xwmWSCty3Kz5IDiIPde/05 O7LdxrIUM62CiovLizM6tAb6f0TaqnvoX2XRIYL9ejcPaYe1qbWv0LFHE7WB2AhJNi 9kJX0JCqAPV+UlpOjshfypYz/QdF648+jGeXuyLFujevpaGaBYqYsnsnMKA8e/d3c3 lJu1OkZIxf3zOIxKpSErRMdHiu9E+e0eLdH9SBRR4PUlmp+uj1khOhXbxIbqgOlOE9 7zGmQvnz7svTl+K2NYCOhsuV+z+IcS6Z6sDO1hGfyD8C6r3dIHDtx0sGdZwFtmFsPL zR5o56U/JJ5tg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id muVDLpDjJPkm; Sat, 4 Jan 2025 22:00:19 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 3C5193C082EB9; Sat, 4 Jan 2025 22:00:19 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 48/59] Minor SECSPERDAY widening tweak Date: Sat, 4 Jan 2025 21:57:23 -0800 Message-ID: <20250105055750.1668721-49-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzset.c (SECSPERDAY): Now an enum, not a macro. There's no need to widen it to __time64_t, and keeping it an enum can help the compiler do cheaper multiplication. --- time/tzset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time/tzset.c b/time/tzset.c index f3af960620..cc64516e37 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -27,7 +27,7 @@ #include -#define SECSPERDAY ((__time64_t) 86400) +enum { SECSPERDAY = 24 * 60 * 60 }; char *__tzname[2] = { (char *) "UTC", (char *) "UTC" }; int __daylight = 0; From patchwork Sun Jan 5 05:57:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104080 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B946E3858C5F for ; Sun, 5 Jan 2025 06:35:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B946E3858C5F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=RANNYJOJ X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 9FBE53858430 for ; Sun, 5 Jan 2025 06:00:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9FBE53858430 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9FBE53858430 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056822; cv=none; b=kS3MJaeyQENsaVio0a/qMiaFvVhik8Y8sRv6tjUOJkFcN7rinCaFBDUf6rFZJXLbWlLKDiF4bggAXt0c4eTElR4kX/GUTWh4ITPylc+if45/dnW9NhnOoNWJwpbrHe+6PQCS0ycBk8V1OcojjF7lQY7UFKsZkopCcxh/ARJwkIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056822; c=relaxed/simple; bh=sdAKs1ivHHp43GEAwHy50JKQNsKjwaDjrEz+h5q61pE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rce+qlf5H5TVAeVadA3rwC3ZezvoTo2LgIZ8mmra8aelPLitWI2bilT8yRX6gvO2cfQ4BsyKTX3uf2yTBzRe/TOXMD859q04/Pe9qYIiGQQFdAN3BbNH2QttKziahHJPmazqO6d8Qil9U2o/WCQJ5bDp5tYxNyTciqV9NNPmTpM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9FBE53858430 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 2569D3C123844 for ; Sat, 4 Jan 2025 22:00:22 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id CECPt-kY1eYg; Sat, 4 Jan 2025 22:00:21 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 53EB53C123848; Sat, 4 Jan 2025 22:00:21 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 53EB53C123848 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056821; bh=ziqkde8ZCVK9RCswWhCB8r7/CrJTDcbNZncYeLMPj+E=; h=From:To:Date:Message-ID:MIME-Version; b=RANNYJOJVmjuTBTmYVnXO7vJqRMzrfUycw5SRg3Cbojiyk7b/gvc8xVFgL8AuLyQx 2QZ+o+9SiPVl9rSBHOUQ9IK4ZAC6jI24Kw14C1hFusXDWr3FbK80n8HKxTngwaRViY RpxR1sOzWfAEtIp4DBQ4Btalfb8UZSuaxZiuIQCK9H8jKHPC6+zflC2EDB93RIErHt sZy5QQxDtk4MTKPP2I70+xB5O9E2hHWdaiaUW+b/HSQlJ7a9mRNEQWN7fS9A6wxlmg 2Vjktg5GekQNGFh/NFspAdaa5jUI3yCsjqENB9XS49V5B/tkFj3nZs3YUcnlmd2LCZ PQdcTA3E+DAUg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 4NF3ydCh87NM; Sat, 4 Jan 2025 22:00:21 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 37AFB3C082EB9; Sat, 4 Jan 2025 22:00:21 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 49/59] Remove leap-second goto from __tzfile_compute Date: Sat, 4 Jan 2025 21:57:24 -0800 Message-ID: <20250105055750.1668721-50-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): Do leap second computations first, to avoid the need for a goto and a label. --- time/tzfile.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index f86df0c8d5..e491df7d9f 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -581,6 +581,26 @@ __tzfile_compute (__time64_t timer, int use_localtime, { tzidx i; + /* Find the last leap second correction transition time before TIMER. */ + int corr = 0; + bool hit = false; + for (i = leapcnt; 0 < i; ) + { + i--; + if (leaps[i].occur <= timer) + { + /* Apply its correction. */ + corr = leaps[i].corr; + + /* A hit if exactly at a positive leap second. */ + hit = (timer == leaps[i].occur + && (i == 0 ? 0 : leaps[i - 1].corr) < leaps[i].corr); + break; + } + } + *leap_correct = corr; + *leap_hit = hit; + if (use_localtime) { unsigned char ti; @@ -609,7 +629,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, /* Use the rules from the TZ string to compute the change. */ __tz_compute (timer, tp); - goto leap; + return; } else { @@ -679,24 +699,6 @@ __tzfile_compute (__time64_t timer, int use_localtime, this is closer to what proleptic TZ does. */ __tzname[dst] = tm_zone; } - - leap: - *leap_correct = 0; - *leap_hit = false; - - /* Find the last leap second correction transition time before TIMER. */ - i = leapcnt; - do - if (i-- == 0) - return; - while (timer < leaps[i].occur); - - /* Apply its correction. */ - *leap_correct = leaps[i].corr; - - if (timer == leaps[i].occur /* Exactly at the transition time. */ - && (leaps[i].corr > (i == 0 ? 0 : leaps[i - 1].corr))) - *leap_hit = true; } weak_alias (transitions, __libc_tzfile_freemem_ptr) From patchwork Sun Jan 5 05:57:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104083 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DDB823858C54 for ; Sun, 5 Jan 2025 06:37:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DDB823858C54 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=m7uIThQt X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 29AEF385841F for ; Sun, 5 Jan 2025 06:00:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 29AEF385841F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 29AEF385841F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056826; cv=none; b=AEpeYysXJzFBASryfgeklLwT7BRBr0ODnEJ9PEH2+RRdbTBquxiYGcEc0QbKg8mLMpEiYgmFWrZ1c4k/d79mt1Rm3rBjxssPRz+UlDOo1A/oE0P5f6zDQWP9sRa8+KHXagcLZ+acVNdpxM3WSffG6ivQ61/J+EZZz7p9gikLpTg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056826; c=relaxed/simple; bh=htNjsnRR6BBAZJ5tFNIxu0WO8LPXqzCp7X58qAqAa7U=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=IukPgQp2B1EyrnbWAvWGI8ApVn3fP/m6R3ki1FL8k+b3LU2ipLAPSTzVPLg7vLXESRMznUViXh0wgVrslbtYJ0hAEcGUt9o0xsFgwLjiOtsLWa2JS4TTe0Ip5jqzEb6cdZNt0+kaPHn3k+Q3FE7JzoamC10loQSLBalw0jZY5l0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29AEF385841F Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id A3B4B3C123847 for ; Sat, 4 Jan 2025 22:00:25 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id ngtpJJ6lJhr6; Sat, 4 Jan 2025 22:00:24 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 056C23C123843; Sat, 4 Jan 2025 22:00:24 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 056C23C123843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056824; bh=pMLpxWQzIDUrj7JvIw6wfQBFFDSDZVRgp4Gy92rvvgI=; h=From:To:Date:Message-ID:MIME-Version; b=m7uIThQtXshopEgDCK6WGD11H7i+DTic5iahI+89uIfSolVUfKfmrPHTzK8RFWT0H G7qTVmG9BtvqnMqevW/oIaYmxG5LGMNUOxW3DEoUb+cRNNeRZYG/K9KygMtGBsbkiy dVLQeyu54hxpXzSU6s5e/OHQm57w7t62kaaa1upLiiY/1IEbHNsN/l2kXvxjElKu6C 59GFCEhdP1VIoI7IdENPHbO42DhgL/pyuxuGpqx68twjoTB1ohpFA9oJcifuPACZ/J e5ThftPJxeaRaEKqBE3rHjf7VMSKzXqtJZFq/noRHjoWofE7mDw8P4aRX96ztp2tio MBpmp6gBIwi1A== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 9mwHJhYxubyq; Sat, 4 Jan 2025 22:00:23 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id DE91B3C082EB9; Sat, 4 Jan 2025 22:00:23 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 50/59] Remove use_last label from __tzfile_compute Date: Sat, 4 Jan 2025 21:57:25 -0800 Message-ID: <20250105055750.1668721-51-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): Redo last-transition check so that there is no need for the use_last label or goto. --- time/tzfile.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index e491df7d9f..8b5faad424 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -614,18 +614,16 @@ __tzfile_compute (__time64_t timer, int use_localtime, } else if (timecnt == 0 || timer >= transitions[timecnt - 1]) { - if (__glibc_unlikely (tzspec == NULL)) + /* TIMER is after the last transition. Do not use the TZ + string if it is absent or if we cannot convert to the + broken down structure. */ + if (__glibc_unlikely (tzspec == NULL) + || __glibc_unlikely (__offtime (timer, 0, 0, tp) == NULL)) { - use_last: i = timecnt; goto found; } - /* Convert to broken down structure. If this fails do not - use the string. */ - if (__glibc_unlikely (__offtime (timer, 0, 0, tp) == NULL)) - goto use_last; - /* Use the rules from the TZ string to compute the change. */ __tz_compute (timer, tp); From patchwork Sun Jan 5 05:57:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104086 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 94ABA3858C5F for ; Sun, 5 Jan 2025 06:41:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 94ABA3858C5F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=FQulxx1R X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id CF62B3858432 for ; Sun, 5 Jan 2025 06:00:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CF62B3858432 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CF62B3858432 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056827; cv=none; b=SdV63nPN4TmxFHFAZawDdN5mWujQXbifAQAKEVLWU/neSe6eznLFd8Yzq4Au3igmEGS6NCmBJysv/EQH+k6c53AKoflp5PGl1Jto+1B0bGbEp7K5kWkFUSBWTXP6tskzaheQf4EyUVKmf134kkGj+BKdpPyx5BiBN8ZqA7qSuc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056827; c=relaxed/simple; bh=C2jM4EeZIEXpOdOl/z5RpYKxrs961EW4t54MBN6iUJQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=O8JnfeDqrFj3rsUhqR9j2qskb8tzvrZxmIo57OlA0A2WtKbsB4C2zuBY6eiMG9IFpXTVaWeUVuado6wLurI0aqBMvoejsX4qxfkx6er7UaKbaXow4Gg+6AGgVGWgZ7AUjRCTwvH8lwLNaGl/gdDhNoo2FOKO060uG7YkzGWxYkg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CF62B3858432 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 553DB3C123843 for ; Sat, 4 Jan 2025 22:00:27 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id 4qydKndzJI5z; Sat, 4 Jan 2025 22:00:27 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 15E543C082EB9; Sat, 4 Jan 2025 22:00:27 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 15E543C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056827; bh=1Vaw2LNaBwm8+wVtfmZhY/KaRIFFXzm8MEtXPoQh+aw=; h=From:To:Date:Message-ID:MIME-Version; b=FQulxx1RUWq4zmvBN6cjSjMkPam0bDZm9w3qFk0i+QJKMAGmNzg4adzJxG/V5/N+Z EJQnBDscf9oxjEYkayWAcMzsb58ecHnxkiL9pncp3q4ZE9p4spMPECmLbUHm3QTocX NLn5PmiWQZ1A62Tpvna7DOikyqaRDxkQJd+avBi8UzteYebJ9Tly/zAc1bp21QpF2y JSUNrebr8Dhrm0Wri9E0IaP18ebt80H1jCdTEU/OwettbLWRnJYSp/Y4DdZgreutn7 hbD+FAu7ACOkl0KppBwx82jeM9hY1iTt/xzvA0fijhc54WpVfpYh+b+9Fz3VMir29t 4l5iqcsfYK3bA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id zsnRPju-v4Hj; Sat, 4 Jan 2025 22:00:27 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id EED8D3C082EB5; Sat, 4 Jan 2025 22:00:26 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 51/59] =?utf-8?b?UmVmYWN0b3Ig4oCYaWbigJkgaW4gX190emZpbGVf?= =?utf-8?b?Y29tcHV0ZQ==?= Date: Sat, 4 Jan 2025 21:57:26 -0800 Message-ID: <20250105055750.1668721-52-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): Negate the sense of an ‘if’ to simplify future changes. --- time/tzfile.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 8b5faad424..37b194df16 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -614,20 +614,18 @@ __tzfile_compute (__time64_t timer, int use_localtime, } else if (timecnt == 0 || timer >= transitions[timecnt - 1]) { - /* TIMER is after the last transition. Do not use the TZ - string if it is absent or if we cannot convert to the - broken down structure. */ - if (__glibc_unlikely (tzspec == NULL) - || __glibc_unlikely (__offtime (timer, 0, 0, tp) == NULL)) + /* TIMER is after the last transition. Use the TZ string if + it is present and we can convert to the broken down structure. */ + if (__glibc_likely (tzspec != NULL) + && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL)) { - i = timecnt; - goto found; + /* Use the rules from the TZ string to compute the change. */ + __tz_compute (timer, tp); + return; } - /* Use the rules from the TZ string to compute the change. */ - __tz_compute (timer, tp); - - return; + i = timecnt; + goto found; } else { From patchwork Sun Jan 5 05:57:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104084 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 22D6B3858C51 for ; Sun, 5 Jan 2025 06:38:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22D6B3858C51 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=XOUPUy/e X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 4371F385843F for ; Sun, 5 Jan 2025 06:00:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4371F385843F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4371F385843F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056831; cv=none; b=kMWQ4jnq4h2WptbFTJWWHXtaZHBKKxzCw5nOs9BsFwL2JOeT77P4Q0T+xmGctYLpKAwZCc8JdAhKIur6l8RQv3FLKLkMAD3nxuyE1ElEYcNsVo0obRHw+KjM5WLhERrL1E84+PKky5fLVL0cdug5b8zIlv0OegD+ePsmJ/UG+Fw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056831; c=relaxed/simple; bh=ade+6IH6jO53kD4e8qfX/mMbtuwIc2vVsd37ceGzS2s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=dDdL+FpQtSy7xhIRAPCZg2+aIQP9arcVTtBM0EyTfm8ehL3BKXXQKt0a4Ya5djzJjIOSDtCBjpLJUOrmz+Oand2go0MzItzCaqzch+WlqhL4A1hpVuqvU2XXYqp37EGgUPfKSaDfe8w5sF6jvZr5/XIRBzCJ6OzYPIMwW6gWj50= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4371F385843F Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id BCD5F3C123844 for ; Sat, 4 Jan 2025 22:00:30 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id fMlKkenh6_BT; Sat, 4 Jan 2025 22:00:30 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 5504E3C082EB9; Sat, 4 Jan 2025 22:00:30 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 5504E3C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056830; bh=Ke8JNgUqbH+Fndz5dulTxrWCu4bHenZ/TL2rWJ2DdHw=; h=From:To:Date:Message-ID:MIME-Version; b=XOUPUy/e527oavQVjUIy9WlYMrFBeVnGCVBVxgjz2QLGpCs8+An5akz227BVFRJLD 2OoaDc2vU5Fn5IBX57XTJyW9cp5yz6HoGdtfyRDvthx7EO1sKhoEQk1U/XbqxzwJUS SVssvXEe7PHCx3GB4P6weGDodHaCXxO1PtkaKcy4XxDZXAWlrrGSYbagEMEy1jndCI 7DcKdDvX5tN2yEv7VBXSWpG781LKEQpbXGCFteys1C/ZB8rLJ/AOvMb8p7ce/PkNNn RM2h2zu0kg4enh8e7hhn61R3pGDoRuv2utU2k//nEfd1eeanGiWwCcXRqfvS+o0BQr t4zbwAeUC4mog== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id d-8eZyDDt3SY; Sat, 4 Jan 2025 22:00:30 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 3EEC03C082EB5; Sat, 4 Jan 2025 22:00:30 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 52/59] =?utf-8?b?UmVmYWN0b3IgbG9jYWx0aW1lIOKAmGlm4oCZIGlu?= =?utf-8?b?IF9fdHpmaWxlX2NvbXB1dGU=?= Date: Sat, 4 Jan 2025 21:57:27 -0800 Message-ID: <20250105055750.1668721-53-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): Negate the sense of another ‘if’ to simplify future changes. Almost all of this is indenting changes. --- time/tzfile.c | 158 +++++++++++++++++++++++++------------------------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 37b194df16..783518cd6d 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -601,100 +601,100 @@ __tzfile_compute (__time64_t timer, int use_localtime, *leap_correct = corr; *leap_hit = hit; - if (use_localtime) - { - unsigned char ti; + if (!use_localtime) + return; + + unsigned char ti; - if (__glibc_unlikely (timecnt == 0 ? tzspec == NULL - : timer < transitions[0])) + if (__glibc_unlikely (timecnt == 0 ? tzspec == NULL + : timer < transitions[0])) + { + /* TIMER is before the first transition; or there are no + transitions and no TZ string. Use the first type. */ + ti = 0; + } + else if (timecnt == 0 || timer >= transitions[timecnt - 1]) + { + /* TIMER is after the last transition. Use the TZ string if + it is present and we can convert to the broken down structure. */ + if (__glibc_likely (tzspec != NULL) + && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL)) { - /* TIMER is before the first transition; or there are no - transitions and no TZ string. Use the first type. */ - ti = 0; + /* Use the rules from the TZ string to compute the change. */ + __tz_compute (timer, tp); + return; } - else if (timecnt == 0 || timer >= transitions[timecnt - 1]) - { - /* TIMER is after the last transition. Use the TZ string if - it is present and we can convert to the broken down structure. */ - if (__glibc_likely (tzspec != NULL) - && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL)) - { - /* Use the rules from the TZ string to compute the change. */ - __tz_compute (timer, tp); - return; - } - i = timecnt; - goto found; - } - else + i = timecnt; + goto found; + } + else + { + /* Find the first transition after TIMER, and + then pick the type of the transition before it. */ + tzidx lo = 0; + tzidx hi = timecnt - 1; + /* Assume that DST is changing twice a year and guess + initial search spot from it. Half of a gregorian year + has on average 365.2425 * 86400 / 2 = 15778476 seconds. + Although i's value can be wrong if overflow occurs, + this is harmless because it is just a guess. */ + __time64_t tdiff; + ckd_sub (&tdiff, transitions[timecnt - 1], timer); + ckd_add (&i, tdiff / 15778476, 0); + if (i < timecnt) { - /* Find the first transition after TIMER, and - then pick the type of the transition before it. */ - tzidx lo = 0; - tzidx hi = timecnt - 1; - /* Assume that DST is changing twice a year and guess - initial search spot from it. Half of a gregorian year - has on average 365.2425 * 86400 / 2 = 15778476 seconds. - Although i's value can be wrong if overflow occurs, - this is harmless because it is just a guess. */ - __time64_t tdiff; - ckd_sub (&tdiff, transitions[timecnt - 1], timer); - ckd_add (&i, tdiff / 15778476, 0); - if (i < timecnt) + i = timecnt - 1 - i; + if (timer < transitions[i]) { - i = timecnt - 1 - i; - if (timer < transitions[i]) - { - if (i < 10 || timer >= transitions[i - 10]) - { - /* Linear search. */ - while (timer < transitions[i - 1]) - --i; - goto found; - } - hi = i - 10; - } - else + if (i < 10 || timer >= transitions[i - 10]) { - if (timecnt - i <= 10 || timer < transitions[i + 10]) - { - /* Linear search. */ - while (timer >= transitions[i]) - ++i; - goto found; - } - lo = i + 10; + /* Linear search. */ + while (timer < transitions[i - 1]) + --i; + goto found; } + hi = i - 10; } - - /* Binary search. */ - /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ - while (lo + 1 < hi) + else { - i = (lo >> 1) + (hi >> 1) + (lo & hi & 1); - if (timer < transitions[i]) - hi = i; - else - lo = i; + if (timecnt - i <= 10 || timer < transitions[i + 10]) + { + /* Linear search. */ + while (timer >= transitions[i]) + ++i; + goto found; + } + lo = i + 10; } - i = hi; - - found: - ti = type_idxs[i - 1]; } - struct ttinfo *info = &types[ti]; - unsigned char dst = info->dst; - char *tm_zone = __tzstring (&zone_names[info->idx]); - tp->tm_isdst = dst; - tp->tm_gmtoff = info->utoff; - tp->tm_zone = tm_zone; + /* Binary search. */ + /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ + while (lo + 1 < hi) + { + i = (lo >> 1) + (hi >> 1) + (lo & hi & 1); + if (timer < transitions[i]) + hi = i; + else + lo = i; + } + i = hi; - /* Set tzname[isdst] too; although not required by POSIX or documented, - this is closer to what proleptic TZ does. */ - __tzname[dst] = tm_zone; + found: + ti = type_idxs[i - 1]; } + + struct ttinfo *info = &types[ti]; + unsigned char dst = info->dst; + char *tm_zone = __tzstring (&zone_names[info->idx]); + tp->tm_isdst = dst; + tp->tm_gmtoff = info->utoff; + tp->tm_zone = tm_zone; + + /* Set tzname[isdst] too; although not required by POSIX or documented, + this is closer to what proleptic TZ does. */ + __tzname[dst] = tm_zone; } weak_alias (transitions, __libc_tzfile_freemem_ptr) From patchwork Sun Jan 5 05:57:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104087 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C44943858C78 for ; Sun, 5 Jan 2025 06:41:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C44943858C78 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=UGXxbJxP X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 66C2D3858D39 for ; Sun, 5 Jan 2025 06:00:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66C2D3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 66C2D3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056838; cv=none; b=Jj6cryHCN38XsKBeG8YkBQeP8coX90VwaKtDJ4arAbEcFdJ2CHKxagZwPJc1Jitra6jOeHF8Aa3p39thEuCPhAylOd3azqMEJRdlMnPQlpQ6Of23xHcqs0Du1izc7kk+Z4hPGo1ZZ3kD1r5MoPKB4vVDst1NmBp1dWZoPyXSjf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056838; c=relaxed/simple; bh=UWS26T/Bk1mN7yXKYe5bycsnf+obsZJVUWfnBC3l5lA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=GmzsboU+LlZ3DP2X7IUuy0CljJRAIfAzU23A0Wbu9tdOhFo80VriHnJd5Q/c1iGwWL0b44dxXqw8IEl+V9W7/QrM18BInQcWauf+Hj1C2IEIakSLHsyEV9kFnydNukgB56hhbxTKKgPU8Z5tFBoeV5YVw+6YlEUQmorQ1pGBoDQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 66C2D3858D39 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id E09723C123845 for ; Sat, 4 Jan 2025 22:00:32 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id me7dbnErCnAM; Sat, 4 Jan 2025 22:00:32 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 8C10E3C082EB9; Sat, 4 Jan 2025 22:00:32 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 8C10E3C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056832; bh=Sll9ylHjmnplAg0o1iKR4ixwiCCS1CH1gVHRxJd/ZLk=; h=From:To:Date:Message-ID:MIME-Version; b=UGXxbJxPWK8Qo3q5Id/2ewvcqqK8DceErFma98TPPRnu72rBU6aplb1XYZWmzljzu DbijTO2UytykKSoQ4XFp28aPOOqgkZFdsEzlsS+8g/q5wFoZ9zKD+f+7lpkVtEcilx 0OhYacQWLfZpx1dN85C3IGvC+1eB+KelAgRwqaxHZGzJVQbAu+ZU2hsQR6XlqkJks3 CE6br/PGF06a1SsQatMkvewuIrCZrTqe7gdL8ulb6cryJqeqYCHpKZTK4MhOgE21bI W1Otc0D1r4TYReMWWixDYW/QIAZ55isBEAImzlyON6q5b1YaoXEvBaxUhCLW273nRF FOr7u/wgV8pcA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id B5j5lcHmG7S6; Sat, 4 Jan 2025 22:00:32 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 70EA33C082EB5; Sat, 4 Jan 2025 22:00:32 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 53/59] =?utf-8?q?Refactor_post-transition-table_=E2=80=98if?= =?utf-8?q?=E2=80=99?= Date: Sat, 4 Jan 2025 21:57:28 -0800 Message-ID: <20250105055750.1668721-54-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): Reorder code and ‘if’ to remove a goto, for clarity. Again, mostly indenting changes. --- time/tzfile.c | 112 +++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 783518cd6d..cde8d74b6f 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -613,73 +613,75 @@ __tzfile_compute (__time64_t timer, int use_localtime, transitions and no TZ string. Use the first type. */ ti = 0; } - else if (timecnt == 0 || timer >= transitions[timecnt - 1]) + else { - /* TIMER is after the last transition. Use the TZ string if - it is present and we can convert to the broken down structure. */ - if (__glibc_likely (tzspec != NULL) - && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL)) + if (timecnt == 0 || timer >= transitions[timecnt - 1]) { - /* Use the rules from the TZ string to compute the change. */ - __tz_compute (timer, tp); - return; - } + /* TIMER is after the last transition. Use the TZ string if + it is present and we can convert to the broken down structure. */ + if (__glibc_likely (tzspec != NULL) + && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL)) + { + /* Use the rules from the TZ string to compute the change. */ + __tz_compute (timer, tp); + return; + } - i = timecnt; - goto found; - } - else - { - /* Find the first transition after TIMER, and - then pick the type of the transition before it. */ - tzidx lo = 0; - tzidx hi = timecnt - 1; - /* Assume that DST is changing twice a year and guess - initial search spot from it. Half of a gregorian year - has on average 365.2425 * 86400 / 2 = 15778476 seconds. - Although i's value can be wrong if overflow occurs, - this is harmless because it is just a guess. */ - __time64_t tdiff; - ckd_sub (&tdiff, transitions[timecnt - 1], timer); - ckd_add (&i, tdiff / 15778476, 0); - if (i < timecnt) + i = timecnt; + } + else { - i = timecnt - 1 - i; - if (timer < transitions[i]) + /* Find the first transition after TIMER, and + then pick the type of the transition before it. */ + tzidx lo = 0; + tzidx hi = timecnt - 1; + /* Assume that DST is changing twice a year and guess + initial search spot from it. Half of a gregorian year + has on average 365.2425 * 86400 / 2 = 15778476 seconds. + Although i's value can be wrong if overflow occurs, + this is harmless because it is just a guess. */ + __time64_t tdiff; + ckd_sub (&tdiff, transitions[timecnt - 1], timer); + ckd_add (&i, tdiff / 15778476, 0); + if (i < timecnt) { - if (i < 10 || timer >= transitions[i - 10]) + i = timecnt - 1 - i; + if (timer < transitions[i]) { - /* Linear search. */ - while (timer < transitions[i - 1]) - --i; - goto found; + if (i < 10 || timer >= transitions[i - 10]) + { + /* Linear search. */ + while (timer < transitions[i - 1]) + --i; + goto found; + } + hi = i - 10; } - hi = i - 10; - } - else - { - if (timecnt - i <= 10 || timer < transitions[i + 10]) + else { - /* Linear search. */ - while (timer >= transitions[i]) - ++i; - goto found; + if (timecnt - i <= 10 || timer < transitions[i + 10]) + { + /* Linear search. */ + while (timer >= transitions[i]) + ++i; + goto found; + } + lo = i + 10; } - lo = i + 10; } - } - /* Binary search. */ - /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ - while (lo + 1 < hi) - { - i = (lo >> 1) + (hi >> 1) + (lo & hi & 1); - if (timer < transitions[i]) - hi = i; - else - lo = i; + /* Binary search. */ + /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ + while (lo + 1 < hi) + { + i = (lo >> 1) + (hi >> 1) + (lo & hi & 1); + if (timer < transitions[i]) + hi = i; + else + lo = i; + } + i = hi; } - i = hi; found: ti = type_idxs[i - 1]; From patchwork Sun Jan 5 05:57:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104065 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 643B63857713 for ; Sun, 5 Jan 2025 06:19:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id AFE1F3858429 for ; Sun, 5 Jan 2025 06:00:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AFE1F3858429 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AFE1F3858429 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056835; cv=none; b=gEBSRoA5K9UZ4dXPqtS/2GMNgQGJ3n5/bMuHfNmvFamH57IW0plYB9EBy/x+ezZKXwZtfgR8dc6pj1F9+lQRKkADdLortDnQJ3TT7UUkp1lmHcDPAFtCf3IqYL9pB9QHMxZeHmbjBLBoVC7L84rX1t4tTw1UrU/I0fKD2cyXKU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056835; c=relaxed/simple; bh=ineo5JBXl805/O1ggz2r9aS6RtbNIKfML91MIL1ScSI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nGGcK+ldB56uUPzprW9vAf6HXGpowun453i80DsI5qLQYAMX93kXifA+GNt8cSmJyLBYz0An/9K0DR0hLuN4SwDJutVMw/uT22wXLRr8kxSSzrxIHM4mndQKRXig1YW3zUeSBT3mUhSxUVaX8ux/fOJZJU36uRxk0bepZf+pzgk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AFE1F3858429 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=Zkqsn3pJ Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 353CA3C123843 for ; Sat, 4 Jan 2025 22:00:35 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id Cuj-ZR0qR928; Sat, 4 Jan 2025 22:00:34 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id CA6AC3C082EB9; Sat, 4 Jan 2025 22:00:34 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu CA6AC3C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056834; bh=9a9joSjfR0byLtNeIREefPsFf80nGV2fcPMZsS3fssY=; h=From:To:Date:Message-ID:MIME-Version; b=Zkqsn3pJhrHUIILY0aMPUjf3G08Z+eBXBbzxnnvWDnJ0B8X0gMk2fKmbGBFCaKAdZ WhVmng2/rKGpvC7rliH+acQeg9Zx7Tn5YEq+mDneofN0VURehisujX4p5KvSnHvkcP ZkqFCflUQ+ZUyxcl8tel0cEC4eRqJ1HCBkNfDyggAqOpQiQ35EJ/zW9NBfHzLcBjQ2 YEmnGpJKPofzLKKmo9w+s1gY6xuTc0K2qKUKIvNXX073mHaLuG4Cs3W3aC3EuuqDdw 4DZxsthJTsR9ZLHQ1x++B5fc4whHeyXMGrkPcHIwOLVad3Ywo+7ziYx1JHQ+yglWOO dFGu8s/zmvFYg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id zfO-YS58YiG7; Sat, 4 Jan 2025 22:00:34 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id B42843C082EB5; Sat, 4 Jan 2025 22:00:34 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 54/59] Refactor __tzfile_compute to avoid two gotos Date: Sat, 4 Jan 2025 21:57:29 -0800 Message-ID: <20250105055750.1668721-55-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (first_transition_after): New function. (__tzfile_compute): Use it to avoid the need for two gotos. --- time/tzfile.c | 129 +++++++++++++++++++++++++++----------------------- 1 file changed, 69 insertions(+), 60 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index cde8d74b6f..0ee70fd356 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -574,6 +574,70 @@ __tzfile_read (const char *file) return false; } +/* Find index of first transition after TIMER. + If there are no transitions after TIMER, return the number of transitions. + TIMER must not be less than the first transition (if any). */ + +static tzidx +first_transition_after (__time64_t timer) +{ + if (timecnt == 0 || timer >= transitions[timecnt - 1]) + return timecnt; + + tzidx lo = 0; + tzidx hi = timecnt - 1; + + /* Find the last transition on or before TIMER. + Assume that DST is changing twice a year and guess + initial search spot from it. Half of a gregorian year + has on average 365.2425 * 86400 / 2 = 15778476 seconds. + Although i's value can be wrong if overflow occurs, + this is harmless because it is just a guess. */ + + __time64_t tdiff; + tzidx i; + ckd_sub (&tdiff, transitions[timecnt - 1], timer); + ckd_add (&i, tdiff / 15778476, 0); + if (i < timecnt) + { + i = timecnt - 1 - i; + if (timer < transitions[i]) + { + if (i < 10 || timer >= transitions[i - 10]) + { + /* Linear search. */ + while (timer < transitions[i - 1]) + --i; + return i; + } + hi = i - 10; + } + else + { + if (timecnt - i <= 10 || timer < transitions[i + 10]) + { + /* Linear search. */ + while (timer >= transitions[i]) + ++i; + return i; + } + lo = i + 10; + } + } + + /* Binary search. */ + /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ + while (lo + 1 < hi) + { + i = (lo >> 1) + (hi >> 1) + (lo & hi & 1); + if (timer < transitions[i]) + hi = i; + else + lo = i; + } + return hi; +} + void __tzfile_compute (__time64_t timer, int use_localtime, int *leap_correct, bool *leap_hit, @@ -615,10 +679,12 @@ __tzfile_compute (__time64_t timer, int use_localtime, } else { - if (timecnt == 0 || timer >= transitions[timecnt - 1]) + i = first_transition_after (timer); + if (i == timecnt) { - /* TIMER is after the last transition. Use the TZ string if - it is present and we can convert to the broken down structure. */ + /* TIMER is after the last transition, or there are no transitions + and a TZ string is present. Use the TZ string if it is present + and if we can convert to the broken down structure. */ if (__glibc_likely (tzspec != NULL) && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL)) { @@ -626,64 +692,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, __tz_compute (timer, tp); return; } - - i = timecnt; - } - else - { - /* Find the first transition after TIMER, and - then pick the type of the transition before it. */ - tzidx lo = 0; - tzidx hi = timecnt - 1; - /* Assume that DST is changing twice a year and guess - initial search spot from it. Half of a gregorian year - has on average 365.2425 * 86400 / 2 = 15778476 seconds. - Although i's value can be wrong if overflow occurs, - this is harmless because it is just a guess. */ - __time64_t tdiff; - ckd_sub (&tdiff, transitions[timecnt - 1], timer); - ckd_add (&i, tdiff / 15778476, 0); - if (i < timecnt) - { - i = timecnt - 1 - i; - if (timer < transitions[i]) - { - if (i < 10 || timer >= transitions[i - 10]) - { - /* Linear search. */ - while (timer < transitions[i - 1]) - --i; - goto found; - } - hi = i - 10; - } - else - { - if (timecnt - i <= 10 || timer < transitions[i + 10]) - { - /* Linear search. */ - while (timer >= transitions[i]) - ++i; - goto found; - } - lo = i + 10; - } - } - - /* Binary search. */ - /* assert (timer >= transitions[lo] && timer < transitions[hi]); */ - while (lo + 1 < hi) - { - i = (lo >> 1) + (hi >> 1) + (lo & hi & 1); - if (timer < transitions[i]) - hi = i; - else - lo = i; - } - i = hi; } - - found: ti = type_idxs[i - 1]; } From patchwork Sun Jan 5 05:57:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104069 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 707593858C78 for ; Sun, 5 Jan 2025 06:22:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 707593858C78 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=d9dCfVm8 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id EB74F3858288 for ; Sun, 5 Jan 2025 06:00:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB74F3858288 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EB74F3858288 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056838; cv=none; b=PmHDUsXja2TuSgwBO2cTv1+Cpmyr4N2ORf1DKrnwYpF5jCNJrfOmwni1TjdtfLGnfZjM2hcjKobh71CVqZY5qQdQWKeBwfLQpPcOaSUABEH1yajKfmcfgOy9HMj/q7JJ/TlH89bIhzip5DBizN4K90jZuJBnlIgsx/bzoGvEAv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056838; c=relaxed/simple; bh=5kNEy2JbDoKXoRQ+0aWejo9Z7qxLTmgo+JxKiDeW5XE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PrCrx45SdY6BlzZlQ7m2Q+Pf7K9JHAAAbTgcD7nYWrW3EPPAjRA8UxtuS5J9ZTCfPhJIpi3Gx7Xve0+6/ipTZDQRZremGDcvoh7N2+cTj746o6qhTbGntCv2pYW1vflSNBol1MBgse4zcUsVjNVMEtxDe2MEpFz8pfr72yt+paM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB74F3858288 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 714893C123844 for ; Sat, 4 Jan 2025 22:00:37 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id hO3Baeu9rpsg; Sat, 4 Jan 2025 22:00:37 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 35D1B3C082EB9; Sat, 4 Jan 2025 22:00:37 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 35D1B3C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056837; bh=SLkO5GW0G9tX7FbuXDnqknC3UK8vky4isX2CV53E3vs=; h=From:To:Date:Message-ID:MIME-Version; b=d9dCfVm8l9a/7gXkk1n+9eB6n8LKi2O++Cf2KCcDq9UbCxO4o/nMpvJdz4M/JBKsj sVyKjlbK3+XW/BhlMceGGFeIMHf13q0oFMlRcfBJMkMI4qt8D8m+xtiZ95hTByVs7g wIF0hK9PjFYk9b2o1Rrcj7JPadJcO8LuXaiAy5CVkBmrR0yX1HACZn3hK2QeRWXzku aQVjuD0qKOyI+721rQBQTzLBwPdgY14IKlDa5iig09N0z8boFxoHi8PnY8M7US6166 sifpFUyXLpxiOV3UY1FMqd3XWZr4ntOVdKJiVSrFuQ/8NCkARPIWnCFEL8DYt/oetD /SZg90SknMWRA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id JDzkA_0YCqAr; Sat, 4 Jan 2025 22:00:37 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 1DA833C082EB5; Sat, 4 Jan 2025 22:00:37 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 55/59] Refactor __tzfile_compute index local Date: Sat, 4 Jan 2025 21:57:30 -0800 Message-ID: <20250105055750.1668721-56-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * time/tzfile.c (__tzfile_compute): Avoid portmanteau index local. --- time/tzfile.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/time/tzfile.c b/time/tzfile.c index 0ee70fd356..bed62d877f 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -643,12 +643,10 @@ __tzfile_compute (__time64_t timer, int use_localtime, int *leap_correct, bool *leap_hit, struct tm *tp) { - tzidx i; - /* Find the last leap second correction transition time before TIMER. */ int corr = 0; bool hit = false; - for (i = leapcnt; 0 < i; ) + for (tzidx i = leapcnt; 0 < i; ) { i--; if (leaps[i].occur <= timer) @@ -679,7 +677,7 @@ __tzfile_compute (__time64_t timer, int use_localtime, } else { - i = first_transition_after (timer); + tzidx i = first_transition_after (timer); if (i == timecnt) { /* TIMER is after the last transition, or there are no transitions From patchwork Sun Jan 5 05:57:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104093 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2D1973858C66 for ; Sun, 5 Jan 2025 06:49:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D1973858C66 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=TeReZjy2 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 216143858416 for ; Sun, 5 Jan 2025 06:00:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 216143858416 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 216143858416 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056840; cv=none; b=AgwMjIqrW1g/bMe1O3l8Rvy/cihALpII+IhFNIayY9QUDyfm3NNch0HUKBRo60YavKUQ6/xjzCLyh66bBMWtXbLNfwLHaapAcvl0TqJ5eTYGpqKAhf0pWWEOhSIicE5mT5cix7K9CnVjjB/6Q155Dvl8kHOVrWweV1fBX+jmZXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056840; c=relaxed/simple; bh=vdvJ5Q9WoqVjBKMZgIz5aeQHu95W3bZx6r3bJ3aNot8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lbEGtxo9Ie9aZ8uJpYCRYEMZevohyCyv8jtXiajuUs7MfppRm9NkMAJ3NEarVSvUeAwDyQ2h84NK5Ji5TuiTahQY+/akC0QtpmwlfDgSfELihgrbZQiaV+pj+n1MSes0pmH9ECAd6liL/JroULTLzXumUyOvKPejWiGrisuGgWY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 216143858416 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 9AF603C123847 for ; Sat, 4 Jan 2025 22:00:39 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id M4wg7rUcsPe1; Sat, 4 Jan 2025 22:00:39 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 61E6E3C082EB9; Sat, 4 Jan 2025 22:00:39 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 61E6E3C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056839; bh=0p/iDKxG0V27S+KzbjhJjLfuFioy4VJnuT1pkH3ZaGE=; h=From:To:Date:Message-ID:MIME-Version; b=TeReZjy2wNoJu28HCD1Q5XI3wByr5n7vowI9+Vqcvb6pZ1gWSshmx6nzeccs6jSsO oLZgljZlh6vGLr7s+ipCg6ZeZhO1GVUFS2LJawy3nWLMDD1g51XcHNvMkcMIPEBmt1 TiSU0cLUumAMCn7py8WiOTdOc4ctFOl9pbsnpvwQZDjJNTsqgbexC29Kxma2SyfupZ NgD9f/j9DHy/hiuDv1q3i3W/NJL+LdCJTtjuXGIP5pvIcDv4TFcp7R1e0MChTequFP 5evUGVN5O/1vc1+Vv/HD1nNwm0160kVmH+8Liy3lHwdxFiFHOL8I5l5rMsG/YT3cf4 o0sy52RSgZ60g== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id Gzpc3mHdF3BP; Sat, 4 Jan 2025 22:00:39 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 41B883C082EB5; Sat, 4 Jan 2025 22:00:39 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 56/59] mktime: prefer bool to int Date: Sat, 4 Jan 2025 21:57:31 -0800 Message-ID: <20250105055750.1668721-57-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * lib/mktime.c (__mktime_internal): Use bool for a boolean local. --- time/mktime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/time/mktime.c b/time/mktime.c index 0981cd1d1d..fca0b62d76 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -359,8 +359,8 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) /* Ignore any tm_isdst request for timegm. */ int isdst = local ? tp->tm_isdst : 0; - /* 1 if the previous probe was DST. */ - int dst2 = 0; + /* True if the previous probe was DST. */ + bool dst2 = false; /* Ensure that mon is in range, and set year accordingly. */ int mon_remainder = mon % 12; From patchwork Sun Jan 5 05:57:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104089 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C816C3858D35 for ; Sun, 5 Jan 2025 06:44:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C816C3858D35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=DWhoLpTd X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 446ED3858424 for ; Sun, 5 Jan 2025 06:00:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 446ED3858424 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 446ED3858424 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056842; cv=none; b=dYhqIspN3GvltE3Cp2AXi8/9uALIDzwotbSTXnEjFg7uKXodW6AMiSpx44He6IyTZ0raPLRIk3ZkQfqoSF6bFK1R4TK4afLZBwRqAOeswZTu/do9kJEq06eAQA+1fuDkyE4+D15En2SgT7CSFZTPOGDn8p2gBcnXk5YttayhBcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056842; c=relaxed/simple; bh=X/g+tCRz2HzyOCv40icBhIhBF5ddERvoQtfKHG6CKGE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=i5fq2ZncwpjIEUu+BxdNq+otMMI2Dzqr/H8dPq3fVpBqXZqAJlCysW838QGKbunKxMNTUycXgCumZ/b4Xeaap0HujD9nHfSKS/GynzazIiZdPsbJEPrK8OpBhv2W0Vjd0fJcgC+wu0aVYwEskzC39ca0QFJh/QN9GWaE3tZ0/+Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 446ED3858424 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id BCE463C123848 for ; Sat, 4 Jan 2025 22:00:41 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id exy1a-v_tAMm; Sat, 4 Jan 2025 22:00:41 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 7E4743C082EB9; Sat, 4 Jan 2025 22:00:41 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 7E4743C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056841; bh=E1oZuCN3WHO0V/mIMiAhBqpJolBOo1lVbQzegAJ3ewk=; h=From:To:Date:Message-ID:MIME-Version; b=DWhoLpTdsxz9YLcVD+FJvhHVzmjuYBm22WoEYLd/uAnHLisPHGqjA0IzUc9yWdAAD ++ORypgB2jkLbpegfcDiwmctdOJIpqK1vBtGLCGSNr17cbHGEvOPdVyr8+GPS7cXT7 My7wJRCmvHsYzUjIeATc9JHwSIPB6A6IUYbDET5wrx2YQ5xkrB/ycYEjT0ZWy68mvj R+eKlNX5Eh8MjKAYAQjbUXIMbUZtKAjLFF4gXM69rCMnT4qWvqDrFs7NhapevV8zeG YNxmvzKmRFBIaL/0UxGGTp8saDMlfuRQs1SE3mz3MsFwqK/WhEnVowWsJ6qDrTqztQ Jt+T2ylopyNHg== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id VbqdMJ85be1s; Sat, 4 Jan 2025 22:00:41 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 677CF3C082EB5; Sat, 4 Jan 2025 22:00:41 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 57/59] mktime: port __mktime64 locking to Gnulib Date: Sat, 4 Jan 2025 21:57:32 -0800 Message-ID: <20250105055750.1668721-58-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * lib/mktime.c (__mktime64) [!_LIBC && NEED_MKTIME_WORKING]: Don't assume glibc. This affects only Gnulib. --- time/mktime.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/time/mktime.c b/time/mktime.c index fca0b62d76..c95805c37c 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -101,8 +101,8 @@ my_tzset (void) tzset (); # endif } -# undef __tzset -# define __tzset() my_tzset () +# undef tzset +# define tzset() my_tzset () #endif #if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL @@ -554,18 +554,19 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) __time64_t __mktime64 (struct tm *tp) { -# if defined _LIBC || NEED_MKTIME_WORKING __libc_lock_lock (__tzset_lock); __tzset_unlocked (); + +# if defined _LIBC || NEED_MKTIME_WORKING static mktime_offset_t localtime_offset; __time64_t result = __mktime_internal (tp, true, &localtime_offset); - __libc_lock_unlock (__tzset_lock); - return result; # else - __tzset (); # undef mktime - return mktime (tp); + __time64_t result = mktime (tp); # endif + + __libc_lock_unlock (__tzset_lock); + return result; } #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */ From patchwork Sun Jan 5 05:57:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104091 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B14BD3858CDA for ; Sun, 5 Jan 2025 06:47:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B14BD3858CDA Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=FVbyawVq X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 77AEE3858427 for ; Sun, 5 Jan 2025 06:00:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 77AEE3858427 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 77AEE3858427 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056845; cv=none; b=UGIXz8ga3YcQBeoINnTctJTb+9cxqewuk18FAaqvTq8Am2p8Kt/zrVQd4iU4wEBCpxR7O8VyjRz4w7c/h/2kgnvoUzlT2D5SNYFdQjzI2QpKrLG1kLoKcZzVCuufBHdbb0OvbG5b+jgks+myEDSkUmUfRXXVLvgbQySr5veNkn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056845; c=relaxed/simple; bh=eRqYYqtK0Hv+V+KcgALR9NEqCTYsIWSIPXLAwlwZ7t0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EatFZwGo4xQ/1StndF1G0dMgpfb4C6/D8AMK714+dbpUM5dTvKzGK5Jelb8hT+HYU4U8LBhdgld0YgvMsUPzuOy1upawNhFfOXexkBEIbr2BMHd4POLTG2coTjCt4QYLPTqx/mLbZMIey5rbcKS46UxHBB5fHx9/iWG9D/I/xAc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 77AEE3858427 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id F1D103C12385D for ; Sat, 4 Jan 2025 22:00:43 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id ykUW81EIq7le; Sat, 4 Jan 2025 22:00:43 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 941843C082EB9; Sat, 4 Jan 2025 22:00:43 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 941843C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056843; bh=ctUJlFngO06gKmeg9e2gKVm/O1CdjCPHly34iV5Tzs4=; h=From:To:Date:Message-ID:MIME-Version; b=FVbyawVq0tpc7fxlfpy0NqP1Ba1Id/2f4N569Mapdg32fnIqdtJC02o+ojJHgtk6j yWnRSM7RRjOO9pHkoj/LKAWTXyrXdiqTDZ+C+mAIcOJKPoXwTMjugyEvEMSo0J/DQ8 /ceA/tJQAg+RS0bG4Agciotge6XXjBshyCKjuGk76JLyzeHb4pYlgXBgkhA5A5jLh8 n4rbF6gw33lTLY0zmVnSkXzqxZBti+mnpHWLhwISHvsERSg+hYBAS7qxIlvrfa4paT 9R7oF4kbWq0WwN8Qh6abKFS+p/yiGd1iAjZbzRihiZEvtN/bJ+HOncoExO6g/Ox3aV u1HBKugbETxyQ== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id R_5r9iXP9F55; Sat, 4 Jan 2025 22:00:43 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 788E93C082EB5; Sat, 4 Jan 2025 22:00:43 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 58/59] mktime: improve tm_isdst heuristic Date: Sat, 4 Jan 2025 21:57:33 -0800 Message-ID: <20250105055750.1668721-59-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org * lib/mktime.c (__mktime_internal): When tm_isdst disagrees with what was requested, search at most a year (+ stride) from the requested time for a matching tm_isdst, and ignore the request if the search fails. This is more likely to match user expectations in timezones like Asia/Kolkata. Problem reported by Florian Weimer in: https://sourceware.org/pipermail/libc-alpha/2025-January/163342.html --- time/mktime.c | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/time/mktime.c b/time/mktime.c index c95805c37c..4218fca69b 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -355,9 +355,7 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) int mday = tp->tm_mday; int mon = tp->tm_mon; int year_requested = tp->tm_year; - - /* Ignore any tm_isdst request for timegm. */ - int isdst = local ? tp->tm_isdst : 0; + int isdst = tp->tm_isdst; /* True if the previous probe was DST. */ bool dst2 = false; @@ -449,13 +447,10 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) Heuristic: probe the adjacent timestamps in both directions, looking for the desired isdst. If none is found within a - reasonable duration bound, assume a one-hour DST difference. + reasonable duration bound, ignore the disagreement. This should work for all real time zone histories in the tz database. */ - /* +1 if we wanted standard time but got DST, -1 if the reverse. */ - int dst_difference = (isdst == 0) - (tm.tm_isdst == 0); - /* Distance between probes when looking for a DST boundary. In tzdata2003a, the shortest period of DST is 601200 seconds (e.g., America/Recife starting 2000-10-08 01:00), and the @@ -465,21 +460,17 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) periods when probing. */ int stride = 601200; - /* In TZDB 2021e, the longest period of DST (or of non-DST), in - which the DST (or adjacent DST) difference is not one hour, - is 457243209 seconds: e.g., America/Cambridge_Bay with leap - seconds, starting 1965-10-31 00:00 in a switch from - double-daylight time (-05) to standard time (-07), and - continuing to 1980-04-27 02:00 in a switch from standard time - (-07) to daylight time (-06). */ - int duration_max = 457243209; - - /* Search in both directions, so the maximum distance is half - the duration; add the stride to avoid off-by-1 problems. */ - int delta_bound = duration_max / 2 + stride; + /* Do not probe too far away from the requested time, + by striding until at least a year has passed, but then giving up. + This helps avoid unexpected results in (for example) Asia/Kolkata, + for which today's users expect to see no DST even though it + did observe DST long ago. */ + int year_seconds_bound = 366 * 24 * 60 * 60 + 1; + int delta_bound = year_seconds_bound + stride; int delta, direction; + /* Search in both directions, closest first. */ for (delta = stride; delta < delta_bound; delta += stride) for (direction = -1; direction <= 1; direction += 2) { @@ -509,13 +500,8 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) } } - /* No unusual DST offset was found nearby. Assume one-hour DST. */ - t += 60 * 60 * dst_difference; - if (mktime_min <= t && t <= mktime_max && __tz_convert (t, local, &tm)) - goto offset_found; - - __set_errno (EOVERFLOW); - return -1; + /* No probe with the requested tm_isdst was found nearby. + Ignore the requested tm_isdst. */ } offset_found: From patchwork Sun Jan 5 05:57:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Eggert X-Patchwork-Id: 104096 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 05C573858CDA for ; Sun, 5 Jan 2025 06:55:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 05C573858CDA Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.a=rsa-sha256 header.s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C header.b=groGusNO X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id 6F88E3858280 for ; Sun, 5 Jan 2025 06:00:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F88E3858280 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6F88E3858280 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056848; cv=none; b=nQUYDljnVCUqiv06HFBWEdUWzOVIgy13C4gaxBZv2jKMOOytI3M33irzvpGNwlhz0ei3IxJG/1U/VmeqvZKZpJpJ+n1J4l9+FF+cX/a5Y9HeI2eI+IrDDV0ieh4lST3grkIQgsyDmMqv5UjWB8C+x2nliaIpZ848byHv2Jm4K50= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736056848; c=relaxed/simple; bh=QZMzFBfqe04BEkS5TfL+s5elFjAin5UkLoXpLPYXe5I=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=BDrtWEg+6NN9jnxsq/XvJnJ+ENvMvX3Xv6hUgYPG/8Sj7tewmg0jqk1tz4VfCRziPpjsSF1j8sr94TmqM4hrMUATby1vlFGYzP9WqECpIPHugu/VLntLj/2vP5Tb8VyDub9JKiqMeNCnJPfNc3zN1iDTYdYn7Chq//dsjXDSUn4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6F88E3858280 Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id E8ADF3C082EB5; Sat, 4 Jan 2025 22:00:47 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10032) with ESMTP id frWJ7-Tt4ErV; Sat, 4 Jan 2025 22:00:47 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 85D553C082EB9; Sat, 4 Jan 2025 22:00:47 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 85D553C082EB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1736056847; bh=qDcWI1uhFXRZ5kiXsifrpCoef0L+rkBAIg/9SVPbNtE=; h=From:To:Date:Message-ID:MIME-Version; b=groGusNOmgWrzEem6/c+/sC0eXpizRNG+mIV0Hlpu1WSgEj9/+e5aqZmyOXdOcWS3 Zm0o8ectykRRbiMmpGUo11DiLuFGzZ6DuErcK4d5j8zCUptDOx4NmyG07txhwaKCYq SLJxU5AcGZilYnrecJBz99Ayl6fkc/n40z4CvudX0VD4Vjh0AI+kfCGxcM3QFQ7uiB /RSuhYIYUrZWJDoRfP8jY5KaGVLCxnbIcRYL864QJZrllm3MO0XE0FPpx48d39qZsc 84IQ0F8KTqxnfDrsaYrg0u5rAf5HsJSgDZQtuJOzAL4ZeFJwtbmGNlZMaCrqQ4pW8A YiY9m7pkaEOjA== X-Virus-Scanned: amavis at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id oU8uXp7tLqU7; Sat, 4 Jan 2025 22:00:47 -0800 (PST) Received: from wing.home (unknown [47.154.28.214]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 6DF933C082EB5; Sat, 4 Jan 2025 22:00:47 -0800 (PST) From: Paul Eggert To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH 59/59] time: test DST adjustment for DST-less zones Date: Sat, 4 Jan 2025 21:57:34 -0800 Message-ID: <20250105055750.1668721-60-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.45.2 In-Reply-To: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> References: <01207110-bd60-46ae-9c08-fb39c2011067@cs.ucla.edu> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org From: Florian Weimer For zones such as UTC, it does not really make sense to perform the forced adjustment. The timezone/testdata/IST file is the compiled Asia/Kolkata zone from tz 2024a. --- time/Makefile | 1 + time/tst-mktime-dst-adjust.c | 156 +++++++++++++++++++++++++++++++++++ timezone/testdata/IST | Bin 0 -> 285 bytes 3 files changed, 157 insertions(+) create mode 100644 time/tst-mktime-dst-adjust.c create mode 100644 timezone/testdata/IST diff --git a/time/Makefile b/time/Makefile index 3e010d2c15..029322ebd7 100644 --- a/time/Makefile +++ b/time/Makefile @@ -66,6 +66,7 @@ tests := \ tst-gmtime \ tst-itimer \ tst-mktime \ + tst-mktime-dst-adjust \ tst-mktime2 \ tst-mktime3 \ tst-mktime4 \ diff --git a/time/tst-mktime-dst-adjust.c b/time/tst-mktime-dst-adjust.c new file mode 100644 index 0000000000..416ef2abcd --- /dev/null +++ b/time/tst-mktime-dst-adjust.c @@ -0,0 +1,156 @@ +/* Test mktime DST adjustment special cases. + Copyright (C) 2024 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 + . */ + +#include +#include +#include + +static int +do_test (void) +{ + TEST_COMPARE (setenv ("TZ", "UTC", 1), 0); + + { + struct tm t = + { + .tm_year = 124, + .tm_mon = 9, + .tm_mday = 1, + .tm_hour = 9, + .tm_min = 20, + .tm_sec = 53, + .tm_isdst = 1, /* Not actually true. */ + }; + TEST_COMPARE (mktime (&t), 1727774453); + } + + /* IST used DST at one point, but no longer does. */ + { + char *path = realpath ("../timezone/testdata/IST", NULL); + TEST_VERIFY (path != NULL); + TEST_COMPARE (setenv ("TZ", path, 1), 0); + free (path); + } + + { + struct tm t = + { + .tm_year = 124, + .tm_mon = 9, + .tm_mday = 1, + .tm_hour = 9, + .tm_min = 20, + .tm_sec = 53, + .tm_isdst = 0, /* Correct value. */ + }; + TEST_COMPARE (mktime (&t), 1727774453 - (int) (5.5 * 3600)); + TEST_COMPARE (t.tm_gmtoff, (int) (5.5 * 3600)); + TEST_COMPARE (t.tm_isdst, 0); + } + + /* This value is incorrect, but the heuristic ignores historic + DST changes. */ + { + struct tm t = + { + .tm_year = 124, + .tm_mon = 9, + .tm_mday = 1, + .tm_hour = 9, + .tm_min = 20, + .tm_sec = 53, + .tm_isdst = 1, /* Incorrect value. */ + }; + TEST_COMPARE (mktime (&t), 1727774453 - (int) (5.5 * 3600)); + TEST_COMPARE (t.tm_gmtoff, (int) (5.5 * 3600)); + TEST_COMPARE (t.tm_isdst, 0); + } + + /* Test using correct DST. */ + { + struct tm t = + { + .tm_year = 42, + .tm_mon = 9, + .tm_mday = 1, + .tm_hour = 9, + .tm_min = 20, + .tm_sec = 53, + .tm_isdst = 1, /* Correct value, DST was in effect. */ + }; + TEST_COMPARE (mktime (&t), -860015347); + TEST_COMPARE (t.tm_gmtoff, (int) (6.5 * 3600)); + TEST_COMPARE (t.tm_isdst, 1); + } + + /* Mismatch: DST incorrectly claimed not in effect. */ + + { + struct tm t = + { + .tm_year = 42, + .tm_mon = 9, + .tm_mday = 1, + .tm_hour = 9, + .tm_min = 20, + .tm_sec = 53, + .tm_isdst = 0, /* Incorrect value. */ + }; + TEST_COMPARE (mktime (&t), -860015347 + 3600); /* One hour added. */ + TEST_COMPARE (t.tm_gmtoff, (int) (6.5 * 3600)); + TEST_COMPARE (t.tm_isdst, 1); + } + + /* Test using correct standard time. */ + { + struct tm t = + { + .tm_year = 42, + .tm_mon = 7, + .tm_mday = 1, + .tm_hour = 9, + .tm_min = 20, + .tm_sec = 53, + .tm_isdst = 0, /* Correct value, standard time in effect. */ + }; + TEST_COMPARE (mktime (&t), -865282147); + TEST_COMPARE (t.tm_gmtoff, (int) (5.5 * 3600)); + TEST_COMPARE (t.tm_isdst, 0); + } + + /* Test using standard time with mismatch. */ + { + struct tm t = + { + .tm_year = 42, + .tm_mon = 7, + .tm_mday = 1, + .tm_hour = 9, + .tm_min = 20, + .tm_sec = 53, + .tm_isdst = 1, /* Incorrect value. */ + }; + TEST_COMPARE (mktime (&t), -865282147 - 3600); /* One hour subtracted. */ + TEST_COMPARE (t.tm_gmtoff, (int) (5.5 * 3600)); + TEST_COMPARE (t.tm_isdst, 0); + } + + return 0; +} + +#include diff --git a/timezone/testdata/IST b/timezone/testdata/IST new file mode 100644 index 0000000000000000000000000000000000000000..0014046d29a38e9b8006f746fea794d7f71eb479 GIT binary patch literal 285 zcmWHE%1kq2zzf)bvMfL>)Bq&f=kD2c>UNLD8P-CHGgFOLTq+To!N|l6gbWNpH-HKl zyxl;meIpn+7#N~67^~5w?UK*{(az6b z8-Qq#8$dM39UvOy7BCHT4~T}kiG`Vk8Rn><3m``Uod$Fi&}lqirwM?Z=7HfnE}%