From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1F2883858C98; Mon, 18 Mar 2024 20:25:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F2883858C98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710793506; bh=+tOL386xK178sNOEDx2T6ME6YGHN79nN2p8mxlRcJwM=; h=From:To:Subject:Date:From; b=wroFZWJGqENZMHifWKxzA2xQtdEprRYam08nuMd1kOYDs6jVsMCKhhbCCVD/PHe8U QTVSPDdd4UlaqdHgaIsjlcPECrNj62eNqer4JpkVKBoU+QXePg9lDypdqtr4yWzsFR Z6xG+F3zmN3AYml8OiPSMKsUyput07vGdTSI9J5U= From: "wsf at fultondesigns dot co.uk" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/114385] New: -Wrestrict false positive creating std::string from iterators Date: Mon, 18 Mar 2024 20:25:05 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: wsf at fultondesigns dot co.uk X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114385 Bug ID: 114385 Summary: -Wrestrict false positive creating std::string from iterators Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: wsf at fultondesigns dot co.uk Target Milestone: --- Observed in gcc-13.1.0, 13.2.0, gcc-14 (gcc-trunk-20240318). Minimal testcase: #include template< class String_type > String_type substitute_esc_chars( typename String_type::const_iterator begin,=20 typename String_type::const_iterator end= ) { if( end - begin < 2 ) return String_type( begin, end ); // if( end - begin < 2 ) return String_type(1, *begin); // workaroun= d 1 // if( end - begin =3D=3D 0 || end - begin =3D=3D 1 ) return String_= type( begin, end ); // workaround 2 String_type result; result.resize( end - begin ); std::copy(begin, end, result.begin()); return result; } int main() { std::string ss[] =3D {"hi", "a"}; for (const std::string& s : ss) substitute_esc_chars(s.begin(), s.end()); return 0; } gcc-13.1 version info... $ g++-13 -Wall -O1 -D_GLIBCXX_ASSERTIONS runme.cxx -o runme In file included from /usr/include/c++/13/string:42, from runme.cxx:1: In static member function =E2=80=98static std::char_traits::char_type* std::char_traits::copy(char_type*, const char_type*, std::size_t)=E2= =80=99, inlined from =E2=80=98static void std::__cxx11::basic_string<_CharT, _T= raits, _Alloc>::_S_copy(_CharT*, const _CharT*, size_type) [with _CharT =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]=E2=80= =99 at /usr/include/c++/13/bits/basic_string.h:420:21, inlined from =E2=80=98static void std::__cxx11::basic_string<_CharT, _T= raits, _Alloc>::_S_copy_chars(_CharT*, const _CharT*, const _CharT*) [with _CharT = =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]= =E2=80=99 at /usr/include/c++/13/bits/basic_string.h:474:16, inlined from =E2=80=98static void std::__cxx11::basic_string<_CharT, _T= raits, _Alloc>::_S_copy_chars(_CharT*, const_iterator, const_iterator) [with _Char= T =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]= =E2=80=99 at /usr/include/c++/13/bits/basic_string.h:463:22, inlined from =E2=80=98void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag) [with _FwdIterator =3D __gnu_cxx::__normal_iterator >; _CharT =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]=E2=80=99 at /usr/include/c++/13/bits/basic_string.tcc:243:21, inlined from =E2=80=98std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator =3D __gnu_cxx::__normal_iterator >; =3D void; _Cha= rT =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]= =E2=80=99 at /usr/include/c++/13/bits/basic_string.h:753:16, inlined from =E2=80=98String_type substitute_esc_chars(typename String_type::const_iterator, typename String_type::const_iterator) [with String_type =3D std::__cxx11::basic_string]=E2=80=99 at runme.cxx:8:6= 2: /usr/include/c++/13/bits/char_traits.h:445:56: warning: =E2=80=98void* __builtin_memcpy(void*, const void*, long unsigned int)=E2=80=99 accessing 9223372036854775808 or more bytes at offsets 0 and 0 may overlap up to 9223372036854775809 bytes at offset -1 [-Wrestrict] 445 | return static_cast(__builtin_memcpy(__s1, __s2, __n)); |=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ $ g++-13 -v Using built-in specs. COLLECT_GCC=3Dg++-13 COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion=3D'Ubuntu 13.1.0-8ubuntu1~18.04' --with-bugurl=3Dfile:///usr/share/doc/gcc-13/README.= Bugs --enable-languages=3Dc,ada,c++,go,d,fortran,objc,obj-c++,m2,rust --prefix= =3D/usr --with-gcc-major-version-only --program-suffix=3D-13 --program-prefix=3Dx86_64-linux-gnu- --enable-shared --enable-linker-build-= id --libexecdir=3D/usr/libexec --without-included-gettext --enable-threads=3Dp= osix --libdir=3D/usr/lib --enable-nls --enable-clocale=3Dgnu --enable-libstdcxx-= debug --enable-libstdcxx-time=3Dyes --with-default-libstdcxx-abi=3Dnew --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=3Drelea= se --with-target-system-zlib=3Dauto --enable-objc-gc=3Dauto --enable-multiarch --disable-werror --disable-cet --with-arch-32=3Di686 --with-abi=3Dm64 --with-multilib-list=3Dm32,m64,mx32 --enable-multilib --with-tune=3Dgeneric --enable-checking=3Drelease --build=3Dx86_64-linux-gnu --host=3Dx86_64-linu= x-gnu --target=3Dx86_64-linux-gnu Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.1.0 (Ubuntu 13.1.0-8ubuntu1~18.04)=