[Bug tree-optimization/106044] New: Spurious warning: -Wrestrict for adding std::strings (__builtin_memcpy)

cuzdav at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Jun 21 12:24:56 GMT 2022


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

            Bug ID: 106044
           Summary: Spurious warning: -Wrestrict for adding std::strings
                    (__builtin_memcpy)
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cuzdav at gmail dot com
  Target Milestone: ---

Created attachment 53182
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53182&action=edit
preprocessed code

At optimization O3, beginning in g++ 12.1 (and still present in trunk) with
-Wrestrict enabled I see the following warning:

warning: 'void* __builtin_memcpy(void*, const void*, long unsigned int)'
accessing 9223372036854775810 or more bytes at offsets [2, 9223372036854775807]
and 1 may overlap up to 9223372036854775813 bytes at offset -3 [-Wrestrict]

Here is the code:

///////////////////////////////////////
#include <string>

std::string f(std::size_t n) {
    return "_" + std::to_string(n);
}
///////////////////////////////////////

On Compiler explorer: https://godbolt.org/z/KMWehnscM

Command line:

g++12 -O3 --std=c++20 -Wrestrict source.cpp

Output:
In file included from
/opt/compiler-explorer/gcc-trunk-20220621/include/c++/13.0.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-trunk-20220621/include/c++/13.0.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-trunk-20220621/include/c++/13.0.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-trunk-20220621/include/c++/13.0.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-trunk-20220621/include/c++/13.0.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-trunk-20220621/include/c++/13.0.0/bits/basic_string.h:3594:36,
    inlined from 'std::string f(std::size_t)' at <source>:4:34:
/opt/compiler-explorer/gcc-trunk-20220621/include/c++/13.0.0/bits/char_traits.h:434:56:
warning: 'void* __builtin_memcpy(void*, const void*, long unsigned int)'
accessing 9223372036854775810 or more bytes at offsets [2, 9223372036854775807]
and 1 may overlap up to 9223372036854775813 bytes at offset -3 [-Wrestrict]
  434 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2,
__n));
      |                                       
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
Compiler returned: 0

//////////////////////////////////

local machine g++ output (where I first encountered it)

[cuzdav@docker tmp]$ /opt/imc/gcc-12.1.0/bin/g++ -v 
Using built-in specs.
COLLECT_GCC=/opt/imc/gcc-12.1.0/bin/g++
COLLECT_LTO_WRAPPER=/opt/imc/gcc-12.1.0/libexec/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-12.1.0/configure --prefix=/opt/imc/gcc-12.1.0
--enable-languages=c,c++,fortran,lto --disable-multilib
--with-build-time-tools=/build/INSTALLDIR//opt/imc/gcc-12.1.0/bin
--enable-libstdcxx-time=rt
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.1.0 (GCC)


More information about the Gcc-bugs mailing list