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 D61553846999 for ; Mon, 12 Dec 2022 14:01:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D61553846999 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670853709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SfQ7kwJc9Q0s6Xd6WaqBRcsa7qUhNgyJIBFxyvpqyGI=; b=KYJkmDBz8K23dpWlQ1EU9LP1dy4f3XbqBw0dzNATskpc1iCcQs93g+Gj+66BnGLvm05Xxo tJpjjN2e+VYstpLuHvMsEwuV2rEcfKFSsXgetyihxf8LZaQEDGjnjKsQ0JS60MdWxJD6eP iz58cMdB5kI89wQF6mIQ01C6dkGyVJw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-498-t74zuqKjM-CbyKuReZwNrQ-1; Mon, 12 Dec 2022 09:01:48 -0500 X-MC-Unique: t74zuqKjM-CbyKuReZwNrQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B11A1805AC5; Mon, 12 Dec 2022 14:01:47 +0000 (UTC) Received: from localhost (unknown [10.33.36.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7962614152F4; Mon, 12 Dec 2022 14:01:47 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add a test checking for chrono::duration overflows Date: Mon, 12 Dec 2022 14:01:46 +0000 Message-Id: <20221212140146.716976-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 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_H2,SPF_HELO_NONE,SPF_NONE,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 List-Id: Tested x86_64-linux. Pushed to trunk. -- >8-- This test fails if chrono::days::rep or chrono::years::rep is a 32-bit type, because a large days or years value silently overflows a 32-bit integer when converted to seconds. It would be conforming to implement chrono::days as chrono::duration>, but would make this overflow case more likely. Similarly for chrono::years, chrono::months and chrono::weeks. This test is here to remind us not to make that change lightly. libstdc++-v3/ChangeLog: * testsuite/20_util/duration/arithmetic/overflow_c++20.cc: New test. --- .../duration/arithmetic/overflow_c++20.cc | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 libstdc++-v3/testsuite/20_util/duration/arithmetic/overflow_c++20.cc diff --git a/libstdc++-v3/testsuite/20_util/duration/arithmetic/overflow_c++20.cc b/libstdc++-v3/testsuite/20_util/duration/arithmetic/overflow_c++20.cc new file mode 100644 index 00000000000..42fa7ea55fb --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/duration/arithmetic/overflow_c++20.cc @@ -0,0 +1,29 @@ +// { dg-options "-std=gnu++20" } +// { dg-do run { target c++20 } } + +#include +#include +#include + +void +test_overflow() +{ + using namespace std::chrono; + + using seconds32_t = duration; + seconds32_t t = 14h + 25min + 55s; + auto snow = sys_days(1854y/December/11); + auto snow_t = snow + t; + // Fails if days::rep is 32-bit: + VERIFY( snow_t.time_since_epoch() < seconds::zero() ); + auto y = floor(snow); + auto y_t = y + t; + // Fails if years::rep is 32-bit: + VERIFY( y_t.time_since_epoch() < seconds::zero() ); + VERIFY( y_t < snow_t ); +} + +int main() +{ + test_overflow(); +} -- 2.38.1