(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
From 2d4e7cd1d476a065d824e11045c8dbc049d5f0a0 Mon Sep 17 00:00:00 2001 From: MacroFake <falke.ma...@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