public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106512] New: String optimization underflows in std::string::operator+ inlining
@ 2022-08-03  9:29 hi at jdoubleu dot de
  2022-08-03  9:57 ` [Bug c++/106512] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: hi at jdoubleu dot de @ 2022-08-03  9:29 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/106512] String optimization underflows in std::string::operator+ inlining
  2022-08-03  9:29 [Bug c++/106512] New: String optimization underflows in std::string::operator+ inlining hi at jdoubleu dot de
@ 2022-08-03  9:57 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2022-08-03  9:57 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Another dup of PR 105651 or PR 105329

*** This bug has been marked as a duplicate of bug 105651 ***

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-03  9:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-03  9:29 [Bug c++/106512] New: String optimization underflows in std::string::operator+ inlining hi at jdoubleu dot de
2022-08-03  9:57 ` [Bug c++/106512] " redi at gcc dot gnu.org

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).