public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/109772] Memory layout optimization of std::chrono::hh_mm_ss is wrong
Date: Mon, 08 May 2023 15:59:24 +0000	[thread overview]
Message-ID: <bug-109772-4-t0vQ0R4WUw@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-109772-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109772

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> std::chrono::hh_mm_ss hms(std::chrono::duration<int, std::ratio<1, 1024>>{1511});

The bug affects this example because __fits<uint_least32_t> is true, because
duration_values<int>::max() <= duration_values<uint_least32_t>::max() is true.
So the library decides it can use uint_least32_t to store the subseconds part.
However, the value we need to store is duration<int64_t, ratio<1,
10'000'000'000>>(4755859375) not duration<int, ratio<1, 1024>(1511 % 1024) and
so we truncate 9990234375 to 32 bits.

The bug doesn't affect uses of hh_mm_ss with the standard duration aliases in
<chrono>, e.g. hh_mm_ss<nanoseconds>, because the standard chrono aliases with
higher precision than one second all use 64-bit rep types, so
__fits<uint_least32_t> is false. And in practice the number of users of
hh_mm_ss with durations with unusual periods and 32-bit rep types is probably
tiny. And hh_mm_ss should not be used for storage, only as a short-lived stack
variable ... so I think we can probably just fix this for 13.2 even though
that's theoretically an ABI break (the C++20 ABI isn't final yet, but we
usually try to keep it stable between minor releases like 13.1 and 13.2). We
can consider adding an abi_tag attribute to hh_mm_ss for 13.2 (but not on
trunk).

  parent reply	other threads:[~2023-05-08 15:59 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-08 14:41 [Bug libstdc++/109772] New: " redi at gcc dot gnu.org
2023-05-08 14:41 ` [Bug libstdc++/109772] " redi at gcc dot gnu.org
2023-05-08 15:18 ` redi at gcc dot gnu.org
2023-05-08 15:59 ` redi at gcc dot gnu.org [this message]
2023-05-08 16:32 ` redi at gcc dot gnu.org
2023-05-10 22:47 ` [Bug libstdc++/109772] [13/14 Regression] " redi at gcc dot gnu.org
2023-05-11 20:19 ` cvs-commit at gcc dot gnu.org
2023-07-27  9:26 ` [Bug libstdc++/109772] [13 " rguenth at gcc dot gnu.org
2024-05-13 11:34 ` rguenth at gcc dot gnu.org
2024-05-21  9:15 ` jakub at gcc dot gnu.org

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=bug-109772-4-t0vQ0R4WUw@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).