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 r12-10228] libstdc++: Fix <charconv> uses of signed types with <bit> functions
Date: Mon, 18 Mar 2024 14:03:57 +0000 (GMT)	[thread overview]
Message-ID: <20240318140357.83E093858C5F@sourceware.org> (raw)

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

commit r12-10228-gac0cf0786bb3da60f412afe800fd385686b9517d
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 14 10:56:57 2023 +0000

    libstdc++: Fix <charconv> uses of signed types with <bit> functions
    
    In <charconv> we pass the int __base parameter to our internal versions
    of <bit> functions, __bit_width and __countr_zero. Those functions are
    only defined for unsigned types, so we need to convert the base to
    unsigned. The base must be in the range [2,36] so we can mask off the
    low bits and then convert that to unsigned, so that we don't need to
    care about negative values becoming large unsigned values.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/charconv (__from_chars_pow2_base): Convert base to
            unsigned for call to __countr_zero.
            (__from_chars_alnum): Likewise for call to __bit_width.
    
    (cherry picked from commit 1c15303375f7089ff985b085ab877b11ebfbc4b7)

Diff:
---
 libstdc++-v3/include/std/charconv | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/include/std/charconv b/libstdc++-v3/include/std/charconv
index b4d37435fa0..365a22dab58 100644
--- a/libstdc++-v3/include/std/charconv
+++ b/libstdc++-v3/include/std/charconv
@@ -458,7 +458,7 @@ namespace __detail
 
       // __glibcxx_assert((__base & (__base - 1)) == 0);
       // __glibcxx_assert(_DecOnly ? __base <= 8 : __base <= 32);
-      const int __log2_base = __countr_zero(__base);
+      const int __log2_base = __countr_zero(unsigned(__base & 0x3f));
 
       const ptrdiff_t __len = __last - __first;
       ptrdiff_t __i = 0;
@@ -510,9 +510,9 @@ namespace __detail
     __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
 		       int __base)
     {
-      // __glibcxx_assert(!_DecOnly || __base <= 10);
+      // __glibcxx_assert(_DecOnly ? __base <= 10 : __base <= 36);
 
-      const int __bits_per_digit = __bit_width(__base);
+      const int __bits_per_digit = __bit_width(unsigned(__base & 0x3f));
       int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
       for (; __first != __last; ++__first)
 	{

                 reply	other threads:[~2024-03-18 14:03 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=20240318140357.83E093858C5F@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).