public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marco Falke <falke.marco@gmail.com>
To: Marco Falke <falke.marco@gmail.com>,
	libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: Re: [PATCH] libstdc++: Make __from_chars_alnum_to_val conversion explicit
Date: Mon, 18 Jul 2022 12:10:31 +0200	[thread overview]
Message-ID: <CAK51vgDbNmRUtXVBsun=d3uaUMzpiY_FzP5NOQDsjA2_SdmLqQ@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 923 bytes --]

(in reply to https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598412.html,
adding libstdc++ to CC, with the same patch attached again)

To clarify, this is not a fix for a user-facing issue of gcc or a fix
for UB. It is just a minor UX improvement for developers that use the
clang integer sanitizer to detect implicit int conversions.

To reproduce:

$ cat 1.cpp
#include <charconv>

int main() {
  const auto a{"-1"};
  unsigned b{};
  std::from_chars(a, a + 2, b);
}
$ clang++ -fsanitize=integer -std=c++17 1.cpp -o exe && ./exe
/usr/bin/../lib64/gcc/x86_64-s
use-linux/12/../../../../include/c++/12/charconv:439:9:
runtime error: implicit conversion from type 'int' of value -3
(32-bit, signed) to type 'unsigned char' changed the value to 253
(8-bit, unsigned)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
/usr/bin/../lib64/gcc/x86_64-suse-linux/12/../../../../include/c++/12/charconv:439:9

Best,
Marco

[-- Attachment #2: 0001-libstdc-Make-__from_chars_alnum_to_val-conversion-ex.patch --]
[-- Type: text/x-patch, Size: 1244 bytes --]

From 2d4e7cd1d476a065d824e11045c8dbc049d5f0a0 Mon Sep 17 00:00:00 2001
From: MacroFake <falke.marco@gmail.com>
Date: Thu, 14 Jul 2022 15:26:12 +0200
Subject: [PATCH] libstdc++: Make __from_chars_alnum_to_val conversion explicit

The optimizations from commit a54137c88061c7495728fc6b8dfd0474e812b2cb
introduced a clang integer sanitizer error.

Fix this with an explicit static_cast, similar to the fix in commit
074436cf8cdd2a9ce75cadd36deb8301f00e55b9.

libstdc++-v3/ChangeLog:

        * include/std/charconv (__from_chars_alnum_to_val): Replace
          implicit conversions from int to unsigned char with explicit
          casts.
---
 libstdc++-v3/include/std/charconv | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/charconv b/libstdc++-v3/include/std/charconv
index 218813e4797..bdf23e4a5ad 100644
--- a/libstdc++-v3/include/std/charconv
+++ b/libstdc++-v3/include/std/charconv
@@ -436,7 +436,7 @@ namespace __detail
     __from_chars_alnum_to_val(unsigned char __c)
     {
       if _GLIBCXX17_CONSTEXPR (_DecOnly)
-	return __c - '0';
+       return static_cast<unsigned char>(__c - '0');
       else
 	{
 	  // This initializer is deliberately made dependent in order to work
-- 
2.35.3


             reply	other threads:[~2022-07-18 10:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-18 10:10 Marco Falke [this message]
2022-07-19 10:06 ` Jonathan Wakely
2022-07-19 10:13   ` Marco Falke
2022-07-19 10:15     ` Jonathan Wakely

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='CAK51vgDbNmRUtXVBsun=d3uaUMzpiY_FzP5NOQDsjA2_SdmLqQ@mail.gmail.com' \
    --to=falke.marco@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@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).