public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/54924] Warn for std::string constructor with wrong size
Date: Wed, 15 Dec 2021 17:58:25 +0000	[thread overview]
Message-ID: <bug-54924-4-z2pK8LGvmo@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-54924-4@http.gcc.gnu.org/bugzilla/>

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #15 from Martin Sebor <msebor at gcc dot gnu.org> ---
GCC 11 and 12 finally diagnose this problem even without -Wsystem-headers,
albeit inconsistently.  At -O1 GCC 11 issues -Wstringop-overread:

In file included from
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/string:40,
                 from pr54924.C:2:
In static member function ‘static std::char_traits<char>::char_type*
std::char_traits<char>::copy(std::char_traits<char>::char_type*, const
char_type*, std::size_t)’,
    inlined from ‘static void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_S_copy(_CharT*, const _CharT*, std::__cxx11::basic_string<_CharT,
_Traits, _Alloc>::size_type) [with _CharT = char; _Traits =
std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:359: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
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:406:16,
    inlined from ‘void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag)
[with _FwdIterator = const char*; _CharT = char; _Traits =
std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc:225:25,
    inlined from ‘void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_M_construct_aux(_InIterator, _InIterator, std::__false_type) [with
_InIterator = const char*; _CharT = char; _Traits = std::char_traits<char>;
_Alloc = std::allocator<char>]’ at
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:255:23,
    inlined from ‘void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_M_construct(_InIterator, _InIterator) [with _InIterator = const
char*; _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]’ at
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:274:20,
    inlined from ‘std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT,
_Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits =
std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:521:21,
    inlined from ‘void __static_initialization_and_destruction_0(int, int)’ at
pr54924.C:4:24,
    inlined from ‘(static initializers for pr54924.C)’ at pr54924.C:4:25:
/build/gcc-11-branch/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:409:56:
warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’
reading 5 bytes from a region of size 4 [-Wstringop-overread]
  409 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2,
__n));
      |                                       
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

As a result of g:b8f2efaed02e8b03d215d74e42d3707761772f64 GCC 12 doesn't issue
-Wstringop-overread at any level but at -O2 it does issue -Warray-bounds:

In file included from
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/string:53,
                 from pr54924.C:2:
In constructor ‘std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const _CharT*, size_type, const _Alloc&) [with _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’,
    inlined from ‘void __static_initialization_and_destruction_0(int, int)’ at
pr54924.C:4:24,
    inlined from ‘(static initializers for pr54924.C)’ at pr54924.C:4:25:
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:620:21:
warning: array subscript 5 is outside array bounds of ‘const char [4]’
[-Warray-bounds]
  620 |         _M_construct(__s, __s + __n, std::forward_iterator_tag());
      |         ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm not happy with how this has turned out but I'm also not sure how to improve
things, so I'll resolve this as fixed.

  parent reply	other threads:[~2021-12-15 17:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-14 17:06 [Bug c++/54924] New: " david at doublewise dot net
2012-10-14 17:44 ` [Bug libstdc++/54924] " redi at gcc dot gnu.org
2012-10-14 18:40 ` david at doublewise dot net
2021-12-15 17:58 ` msebor at gcc dot gnu.org [this message]
2021-12-15 21:30 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-54924-4-z2pK8LGvmo@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).