From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 6846D3858401 for ; Fri, 15 Oct 2021 22:03:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6846D3858401 Received: by mail-wr1-x429.google.com with SMTP id t2so28726326wrb.8 for ; Fri, 15 Oct 2021 15:03:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=IBd88oDI8dEHLXfo7rpov0JJL9DY5R4kUIVwZxXFqWo=; b=dvq4M5c9GDJTxy3EfbuhrYuf8lxoemSSYdP8xdQessJxHJGUguk9/tL6hDCwGZvruH LPuob2LMTx3i+t68FleNJSs0MJqScoHOMIOPfOVcjDYK9c3+YABju4ERPHmLlskGbwsd Nh3AUXIlmxhipLD3irge7YlORyTDP7XGFIpKZA0XI71uU9AuJko2l1f7bmtlnK4L8SAY Ag6VrAit/2NB3875L3jLKDHL+ouv500IWr6neiJmp9vBj2pPD2ZWn3G7yGd59v8VYsBp D1t9dPYZHKrwYXyvZkigbs1if2GbXWpry/yNBRHGVOFuGfbS981NNA85RYi+eEG9bg+m mKpw== X-Gm-Message-State: AOAM5322KHFGsJ9h8ICgo0HzhAwN6su4kl+niJCEh9LoQR1rxP+QVeH8 FDFkFVnCQAoYh8s8NQYTxcg= X-Google-Smtp-Source: ABdhPJwxQxnFGzOaiQedGbLnzFLCIVYsjHpbYll57iY3qMfb8ZTzZ1ZoMN3Te0OUKFOhHmDso8JEGA== X-Received: by 2002:adf:bc14:: with SMTP id s20mr17402307wrg.8.1634335415572; Fri, 15 Oct 2021 15:03:35 -0700 (PDT) Received: from [10.168.10.11] ([170.253.36.171]) by smtp.gmail.com with ESMTPSA id l20sm16497406wmq.42.2021.10.15.15.03.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Oct 2021 15:03:35 -0700 (PDT) Subject: Re: [PATCH v2 2/2] timegm.3: Remove recommendation against use of timegm() To: Paul Eggert Cc: libc-alpha@sourceware.org, mtk.manpages@gmail.com, linux-man@vger.kernel.org References: <20211011111237.9414-2-alx.manpages@gmail.com> <15d7b96d-13d0-86c1-48f3-24a637ab8e30@cs.ucla.edu> From: "Alejandro Colomar (man-pages)" Message-ID: Date: Sat, 16 Oct 2021 00:03:34 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <15d7b96d-13d0-86c1-48f3-24a637ab8e30@cs.ucla.edu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Oct 2021 22:03:37 -0000 Hi Paul, On 10/11/21 5:40 PM, Paul Eggert wrote: > On 10/11/21 4:12 AM, Alejandro Colomar wrote: > >> time_t portable_timegm(struct tm *tm) >> { >>     tm->tm_isdst = 0; >>     /* >>      * If another thread modifies the timezone during the >>      * execution of the line below, it will produce undefined >>      * behavior. >>      */ >>     return mktime(tm) - timezone; >> } > > This doesn't work for multiple reasons: > it's not thread-safe, Actually, since timegm(3) is implemented in terms of mktime(3), as far as I could read from glibc code, the problem will be the same, I think. I don't understand why it wasn't the other way around; maybe it was more complex internally... But timegm(3) shouldn't need to depend on environment variables. > mktime might set timezone even in a single-threaded app, Yes, I should have called tzset() before the return line. > and the subtraction might overflow. Yup, casting to int64_t needed. BTW, I had a look at mktime source code, and it uses long, which might be 32 bits, and then there's a lot of checking for overflow. Wouldn't it be simpler to just implement mktime(3) with int64_t internally? Then, only at the return, cast it implicitly to whatever time_t means, but int64_t would simplify the code very much, I think. Thanks, Alex -- Alejandro Colomar Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/ http://www.alejandro-colomar.es/