google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.51k stars 10.09k forks source link

[Bug]: GCC-12: Compiler warning with C++20 mode in testing::internal::CanonicalizeForStdLibVersioning #4570

Closed Nekto89 closed 2 months ago

Nekto89 commented 3 months ago

Describe the issue

I've increased standard in my code from C++17 to C++20 and got new warning coming from gtest. I would like to not have this warning in future from public headers.

In file included from /usr/include/c++/12/ios:40,
                 from /usr/include/c++/12/ostream:38,
                 from /usr/include/c++/12/bits/unique_ptr.h:41,
                 from /usr/include/c++/12/memory:76,
                 from /usr/src/googletest/include/gtest/gtest.h:55,
                 from /usr/src/googletest/test/gtest_list_output_unittest_.cc:41:
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 /usr/include/c++/12/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 /usr/include/c++/12/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 /usr/include/c++/12/bits/basic_string.h:2171:19,
    inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::replace(size_type, size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /usr/include/c++/12/bits/basic_string.h:2196:22,
    inlined from 'std::string testing::internal::CanonicalizeForStdLibVersioning(std::string)' at /usr/src/googletest/include/gtest/internal/gtest-type-util.h:83:14:
/usr/include/c++/12/bits/char_traits.h:431: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]
  431 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
      |

Steps to reproduce the problem

prerequisites: Debian Bookworm with gcc 12.2. For example, through docker: docker run --rm -v %CD%:/usr/src -it debian:bookworm-20240612 /bin/bash apt-get update apt-get install g++ cmake mkdir build && cd build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=20 -Dgtest_build_tests=ON /usr/src cmake --build . --config Release -j

What version of GoogleTest are you using?

latest 34ad51b3dc4f922d8ab622491dd44fc2c39afee9

What operating system and version are you using?

Debian Bookworm 12.5

What compiler and version are you using?

g++ (Debian 12.2.0-14) 12.2.0

What build system are you using?

cmake version 3.25.1

Additional context

As far as I know GCC 12 has lots of false positives for this warning but it's easy to do workaround in this case. For example, like this https://github.com/Nekto89/googletest/commit/751760ad543f205e1d76797bcc5c7e2ca0c30cce

derekmauro commented 2 months ago

These false positives are super annoying. Thanks for providing a reasonable suggested fix.