Bug 106044 - Spurious warning: -Wrestrict for adding std::strings (__builtin_memcpy)
Summary: Spurious warning: -Wrestrict for adding std::strings (__builtin_memcpy)
Status: RESOLVED DUPLICATE of bug 105651
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wrestrict
  Show dependency treegraph
 
Reported: 2022-06-21 12:24 UTC by Chris Uzdavinis
Modified: 2022-06-23 15:25 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
preprocessed code (97.23 KB, application/x-gzip)
2022-06-21 12:24 UTC, Chris Uzdavinis
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Uzdavinis 2022-06-21 12:24:56 UTC
Created attachment 53182 [details]
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)
Comment 1 Chris Uzdavinis 2022-06-21 12:27:49 UTC
only in c++20, too.
Comment 2 Chris Uzdavinis 2022-06-21 14:51:23 UTC
Even marginally simpler:

#include <string>
auto f() {
    return "_" + std::string(" ");
}


Originally I marked it as tree-optimization since it's only with -O3, but it could be an issue in the standard library itself. I didn't consider that initially.
Comment 3 Andrew Pinski 2022-06-21 17:33:01 UTC
Dup of bug 105651.

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