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

            Bug ID: 106512
           Summary: String optimization underflows in
                    std::string::operator+ inlining
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hi at jdoubleu dot de
  Target Milestone: ---

Live example: https://godbolt.org/z/zMqG8W7WE

Given the following code:
```cpp
#include <string>

std::string GetHello()
{
    return std::string{"ello"};
}

int main()
{
    ("H" + GetHello());
}
```

Fails to compile with
1. gcc version 12.1 and newer,
2. linking against gnu++20 and higher
3. all warnings enabled,
4. warnings set to produce an error,
5. -O3 is turned on


I get the following error:
```
In file included from
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/string:40,
                 from <source>:1:
In static member function 'static constexpr std::char_traits<char>::char_type*
std::char_traits<char>::copy(char_type*, const char_type*, std::size_t)',
    inlined from 'static constexpr void std::__cxx11::basic_string<_CharT,
_Traits, _Alloc>::_S_copy(_CharT*, const _CharT*, size_type) [with _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:423:21,
    inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits,
_Allocator>& std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_M_replace(size_type, size_type, const _CharT*, size_type) [with
_CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.tcc:532:22,
    inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>& std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::replace(size_type, size_type, const _CharT*, size_type) [with _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:2171:19,
    inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::insert(size_type,
const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:1928:22,
    inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits,
_Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT,
_Traits, _Allocator>&&) [with _CharT = char; _Traits = char_traits<char>;
_Alloc = allocator<char>]' at
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:3541:36,
    inlined from 'int main()' at <source>:10:10:
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/char_traits.h:431:56:
error: 'void* __builtin_memcpy(void*, const void*, long unsigned int)'
accessing 9223372036854775810 or more bytes at offsets [18,
9223372036854775807] and 17 may overlap up to 9223372036854775813 bytes at
offset -3 [-Werror=restrict]
  431 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2,
__n));
      |                                       
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
```

I'm not sure what the issue is here exactly. From the error message, it looks
some underflow (of `long long`) when trying to inline the
std::string::operator+?

It doesn't seem like a bug in libstdc++, since it compiles with gcc11.

Furthermore, if you just change the `"H" + ...` in the example to `"He" + ...`
it suddenly works.

The symptoms of this one look similar:
https://gcc.gnu.org/bugzilla//show_bug.cgi?id=85651

Reply via email to