public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-4615] libstdc++: Add a test checking for chrono::duration overflows
Date: Mon, 12 Dec 2022 14:01:11 +0000 (GMT)	[thread overview]
Message-ID: <20221212140111.50CEB385008B@sourceware.org> (raw)

https://gcc.gnu.org/g:dc94eaab008165698a1161c757db96fc701103a2

commit r13-4615-gdc94eaab008165698a1161c757db96fc701103a2
Author: Jonathan Wakely <jwakely@redhat.com>
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<int32_t, ratio<86400>>, 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 <chrono>
+#include <cstdint>
+#include <testsuite_hooks.h>
+
+void
+test_overflow()
+{
+  using namespace std::chrono;
+
+  using seconds32_t = duration<std::int_least32_t>;
+  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<years>(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();
+}

                 reply	other threads:[~2022-12-12 14:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221212140111.50CEB385008B@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).