From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 50CEB385008B; Mon, 12 Dec 2022 14:01:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50CEB385008B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670853671; bh=LZ8FuUtZGL6fzh2vToecEFvDoKlBB5z1WrKN4oVM53E=; h=From:To:Subject:Date:From; b=gK1ypos4jRgXTB5Xt5VnyLceR7D2Gy951KzRPnpcUR1yKdH0/Ly2tiMQ3irMCpXLk 7w9zvRHAD/RVgG0VHNjeGrPaEQWYk32ncJXwDs7kE516790VaJDaBzWyZ+zqb2R60W dxV1Asvfj+Eg7rFwrzPNmTJDHNoUxkDguu5yb4c0= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-4615] libstdc++: Add a test checking for chrono::duration overflows X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 6c0f9584013dbedeacacb041d40ce9005b902df0 X-Git-Newrev: dc94eaab008165698a1161c757db96fc701103a2 Message-Id: <20221212140111.50CEB385008B@sourceware.org> Date: Mon, 12 Dec 2022 14:01:11 +0000 (GMT) List-Id: https://gcc.gnu.org/g:dc94eaab008165698a1161c757db96fc701103a2 commit r13-4615-gdc94eaab008165698a1161c757db96fc701103a2 Author: Jonathan Wakely Date: Mon Dec 12 12:51:49 2022 +0000 libstdc++: Add a test checking for chrono::duration overflows 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. Diff: --- .../20_util/duration/arithmetic/overflow_c++20.cc | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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(); +}