public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114385] New: -Wrestrict false positive creating std::string from iterators
@ 2024-03-18 20:25 wsf at fultondesigns dot co.uk
  2024-03-18 20:39 ` [Bug tree-optimization/114385] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: wsf at fultondesigns dot co.uk @ 2024-03-18 20:25 UTC (permalink / raw)
  To: gcc-bugs

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

            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 <string>

template< class String_type >
    String_type substitute_esc_chars( typename String_type::const_iterator
begin, 
                                   typename String_type::const_iterator end )
    {

        if( end - begin < 2 ) return String_type( begin, end );
//        if( end - begin < 2 ) return String_type(1, *begin); // workaround 1
//        if( end - begin == 0 || end - begin == 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[] = {"hi", "a"};
    for (const std::string& s : ss)
        substitute_esc_chars<std::string>(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 ‘static std::char_traits<char>::char_type*
std::char_traits<char>::copy(char_type*, const char_type*, std::size_t)’,
    inlined from ‘static 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++/13/bits/basic_string.h:420:21,
    inlined from ‘static void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_S_copy_chars(_CharT*, const _CharT*, const _CharT*) [with _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/usr/include/c++/13/bits/basic_string.h:474:16,
    inlined from ‘static void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_S_copy_chars(_CharT*, const_iterator, const_iterator) [with _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/usr/include/c++/13/bits/basic_string.h:463:22,
    inlined from ‘void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag)
[with _FwdIterator = __gnu_cxx::__normal_iterator<const char*,
std::__cxx11::basic_string<char> >; _CharT = char; _Traits =
std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/usr/include/c++/13/bits/basic_string.tcc:243:21,
    inlined from ‘std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with
_InputIterator = __gnu_cxx::__normal_iterator<const char*,
std::__cxx11::basic_string<char> >; <template-parameter-2-2> = void; _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/usr/include/c++/13/bits/basic_string.h:753:16,
    inlined from ‘String_type substitute_esc_chars(typename
String_type::const_iterator, typename String_type::const_iterator) [with
String_type = std::__cxx11::basic_string<char>]’ at runme.cxx:8:62:
/usr/include/c++/13/bits/char_traits.h:445:56: warning: ‘void*
__builtin_memcpy(void*, const void*, long unsigned int)’ accessing
9223372036854775808 or more bytes at offsets 0 and 0 may overlap up to
9223372036854775809 bytes at offset -1 [-Wrestrict]
  445 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2,
__n));
      |                                       
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~


$ g++-13 -v
Using built-in specs.
COLLECT_GCC=g++-13
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
13.1.0-8ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-13/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2,rust --prefix=/usr
--with-gcc-major-version-only --program-suffix=-13
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --disable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.1.0 (Ubuntu 13.1.0-8ubuntu1~18.04)

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

* [Bug tree-optimization/114385] -Wrestrict false positive creating std::string from iterators
  2024-03-18 20:25 [Bug c++/114385] New: -Wrestrict false positive creating std::string from iterators wsf at fultondesigns dot co.uk
@ 2024-03-18 20:39 ` pinskia at gcc dot gnu.org
  2024-03-18 20:40 ` pinskia at gcc dot gnu.org
  2024-03-18 20:45 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-18 20:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Another workaround is to add:

        if (end-begin < 0) __builtin_unreachable();


I notice that this is not the same as:
[[assume(end-begin >= 0)]];

but that seems related to another bug report dealing with how we don't do
IPA_SRA over assume statements yet.

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

* [Bug tree-optimization/114385] -Wrestrict false positive creating std::string from iterators
  2024-03-18 20:25 [Bug c++/114385] New: -Wrestrict false positive creating std::string from iterators wsf at fultondesigns dot co.uk
  2024-03-18 20:39 ` [Bug tree-optimization/114385] " pinskia at gcc dot gnu.org
@ 2024-03-18 20:40 ` pinskia at gcc dot gnu.org
  2024-03-18 20:45 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-18 20:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This also works:
if (end < begin) __builtin_unreachable();

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

* [Bug tree-optimization/114385] -Wrestrict false positive creating std::string from iterators
  2024-03-18 20:25 [Bug c++/114385] New: -Wrestrict false positive creating std::string from iterators wsf at fultondesigns dot co.uk
  2024-03-18 20:39 ` [Bug tree-optimization/114385] " pinskia at gcc dot gnu.org
  2024-03-18 20:40 ` pinskia at gcc dot gnu.org
@ 2024-03-18 20:45 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-18 20:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think this might be due to changes to std::copy which tries to skip memcpy if
it is only one element.

My bet is maybe std::copy could add `if (end < begin) __builtin_unreachable();`
in it and the code would be better optimized but then some warnings might not
show up.

Basically if I read this code correctly, the preconditions for this function is
 both begin and end are proper iterators and that `end >= begin` is holds true.

What is happening is GCC gets `end - begin < 2` but then does not know if that
could be negative and all things break lose.

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

end of thread, other threads:[~2024-03-18 20:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-18 20:25 [Bug c++/114385] New: -Wrestrict false positive creating std::string from iterators wsf at fultondesigns dot co.uk
2024-03-18 20:39 ` [Bug tree-optimization/114385] " pinskia at gcc dot gnu.org
2024-03-18 20:40 ` pinskia at gcc dot gnu.org
2024-03-18 20:45 ` pinskia 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).