From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 96B6F3861880 for ; Tue, 14 Nov 2023 22:48:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 96B6F3861880 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 96B6F3861880 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700002131; cv=none; b=HyZ/rkUKlqfBtXeiMBURbcRrGXkyEMbW6xdsx2tfB+UKcfslO4oiUpMCtiploniIuK7hUH651BhCdU4ap8bsxz687iZAHjAK0x1bgtQ7rgwv5ujwUf3aOhTwX+WNb94oZfgmjlnI45DVhrri74eEsSalHWOpBnPKEbboF/K/N1E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700002131; c=relaxed/simple; bh=8XTpyyeOM1uD8ZJhfoqK8mlnKLFmLc9EUbg5Gg+kyZs=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=BhZDdrgNkOOlVHpf6EmSLWmiNxnIskjZ5Eqvz9Sog/byLKB/lwKhPhMHqMw3PuUML/5fx7xvvvafBflW+HD1Tq+r5teNBpxVGyk29Cz1jIX3n7nkyjB/XWcoFAEx4o/w9yocDk9BYuIwAcGOl0CGPinYGsZZAh922U1FiH6MEkc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700002130; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=hKMtffO4hZmuX6phGsgQIG1wXpH/dUMFUqgZFhU/ivI=; b=BJlQ4sumLHPwwV4GvvUzCActF4f8LJnDPZe7FELp+wVFoG9uHwk1XW7wAQtq+MBbBWtKgx XGgdq/JSkr2dak1LjYBfhr2zGHJYzFDBiL+8Lk+qkpJ9z1CgtTiP8fKXyb0v7vTZSdbYYv xver+6Nu3FXYurE1B6AHmTxho8lcTqI= Received: from mail-yw1-f200.google.com (mail-yw1-f200.google.com [209.85.128.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-jKZL9usFP5qHo3qAcGM4ZA-1; Tue, 14 Nov 2023 17:48:48 -0500 X-MC-Unique: jKZL9usFP5qHo3qAcGM4ZA-1 Received: by mail-yw1-f200.google.com with SMTP id 00721157ae682-5a7aa161b2fso85408717b3.2 for ; Tue, 14 Nov 2023 14:48:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700002128; x=1700606928; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hKMtffO4hZmuX6phGsgQIG1wXpH/dUMFUqgZFhU/ivI=; b=VFWr9G2jucpzZ5i1OGdNEUep7k4AzgwguvdjQ58Scrw0kue6qVToxpu0cBlohKfsw+ wzLRuDslsa3riRTov5Nd2ieXrfDbnnTX5dwau9TZZLJx0JX6Mhy7OFq2zhjH0P3dJjoO UOuYdA9Zb4DYENjzDREz3kugDIsxozONnqoFMmoL0ZDBiZXAduTWg/5UHFhjXrXtHhak u0+7SOp45z5ZvENvNrDgkwBJyf2ZTjiitHDmjf8Opeps9k7YwcMn2lfuu3zNfBHHsf1f 5mKllJtY2DdPYoVppzm4mLei9bVjjI01C200HzUf7JZUm+OBwdPRf9ZCgtcZTR4v5zd5 Vbgg== X-Gm-Message-State: AOJu0YxpmjkyWY9VF3nyIwvoiVartqSgD6rfo5JzMY+N/pa6opbV0Fra fJu4cQtp3duHyaST4wM1QxKMRdcry+gBC38woBUGaQiIN+cuUfompti1aiK02nEfchh0KjX2yJq moxvi21L+G65hYJSkEKnkMzrk84o9tGKq+w== X-Received: by 2002:a05:690c:2a89:b0:5c1:c4b:208 with SMTP id ek9-20020a05690c2a8900b005c10c4b0208mr10257388ywb.24.1700002128363; Tue, 14 Nov 2023 14:48:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IGDl2MwiUvRG7IuO+f2Ldxc4BPLX+9GdCZANEAAUa63/qtK5hPH0R5h1c8TmwZeJCYjfQqvy4InE7eIO4S+poY= X-Received: by 2002:a05:690c:2a89:b0:5c1:c4b:208 with SMTP id ek9-20020a05690c2a8900b005c10c4b0208mr10257377ywb.24.1700002128153; Tue, 14 Nov 2023 14:48:48 -0800 (PST) MIME-Version: 1.0 References: <20231112013352.19885-1-cassio.neri@gmail.com> In-Reply-To: <20231112013352.19885-1-cassio.neri@gmail.com> From: Jonathan Wakely Date: Tue, 14 Nov 2023 22:48:37 +0000 Message-ID: Subject: Re: [PATCH] Fix UB in weekday::weekday(sys_days) and add test. To: Cassio Neri Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Sun, 12 Nov 2023 at 01:34, Cassio Neri wrote: > > The following has undefined behaviour (signed overflow) [1]: > weekday max{sys_days{days{numeric_limits::max()}}}; > > The issue is in this line when __n is very large and __n + 4 overflows: > return weekday(__n >= -4 ? (__n + 4) % 7 : (__n + 5) % 7 + 6); > > In addition to fixing this bug, the new implementation makes the compiler emit > shorter and branchless code for x86-64 and ARM [2]. > > [1] https://godbolt.org/z/1s5bv7KfT > [2] https://godbolt.org/z/zKsabzrhs > > libstdc++-v3/ChangeLog: > > * include/std/chrono: Fix weekday::_S_from_days > * testsuite/std/time/weekday/1.cc: Add test for overflow. > --- > > Good for trunk? Pushed to trunk, thanks. I think this one is worth backporting too. > > libstdc++-v3/include/std/chrono | 11 +++++++++-- > libstdc++-v3/testsuite/std/time/weekday/1.cc | 9 +++++++++ > 2 files changed, 18 insertions(+), 2 deletions(-) > > diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono > index 10e868e5a03..c00dd133173 100644 > --- a/libstdc++-v3/include/std/chrono > +++ b/libstdc++-v3/include/std/chrono > @@ -930,8 +930,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > static constexpr weekday > _S_from_days(const days& __d) > { > - auto __n = __d.count(); > - return weekday(__n >= -4 ? (__n + 4) % 7 : (__n + 5) % 7 + 6); > + using _Rep = days::rep; > + using _URep = make_unsigned_t<_Rep>; > + const auto __n = __d.count(); > + const auto __m = static_cast<_URep>(__n); > + > + // 1970-01-01 (__n = 0, __m = 0 ) -> Thursday (4) > + // 1969-31-12 (__n = -1, __m = _URep(-1)) -> Wednesday (3) > + const auto __offset = __n >= 0 ? _URep(4) : 3 - _URep(-1) % 7 - 7; > + return weekday((__m + __offset) % 7); > } > > public: > diff --git a/libstdc++-v3/testsuite/std/time/weekday/1.cc b/libstdc++-v3/testsuite/std/time/weekday/1.cc > index 00278c8b01c..e89fca47d4b 100644 > --- a/libstdc++-v3/testsuite/std/time/weekday/1.cc > +++ b/libstdc++-v3/testsuite/std/time/weekday/1.cc > @@ -20,6 +20,7 @@ > // Class template day [time.cal.weekday] > > #include > +#include > > constexpr void > constexpr_weekday() > @@ -37,6 +38,14 @@ constexpr_weekday() > static_assert(weekday{3}[2].weekday() == weekday{3}); > static_assert(weekday{3}[last].weekday() == weekday{3}); > > + // Test for UB (overflow). > + { > + using rep = days::rep; > + using std::numeric_limits; > + constexpr weekday max{sys_days{days{numeric_limits::max()}}}; > + constexpr weekday min{sys_days{days{numeric_limits::min()}}}; > + } > + > static_assert(weekday{sys_days{1900y/January/1}} == Monday); > static_assert(weekday{sys_days{1970y/January/1}} == Thursday); > static_assert(weekday{sys_days{2020y/August/21}} == Friday); > -- > 2.41.0 >