public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/54924] New: Warn for std::string constructor with wrong size
@ 2012-10-14 17:06 david at doublewise dot net
  2012-10-14 17:44 ` [Bug libstdc++/54924] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: david at doublewise dot net @ 2012-10-14 17:06 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 54924
           Summary: Warn for std::string constructor with wrong size
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: david@doublewise.net


The constructor for std::string that takes an array of char and a size assumes
that the array of char you pass in is at least as large as the size you
specify. In other words, std::string str('0', 100) is undefined behavior. As I
show in this example, the real issue can be much more subtle if escape
characters are involved:

http://stackoverflow.com/questions/164168/how-do-you-construct-a-stdstring-with-an-embedded-null/12884464#12884464

It would be nice if gcc warned when the size specified in the constructor
exceeds the size of the array passed as the first argument.


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

* [Bug libstdc++/54924] Warn for std::string constructor with wrong size
  2012-10-14 17:06 [Bug c++/54924] New: Warn for std::string constructor with wrong size david at doublewise dot net
@ 2012-10-14 17:44 ` redi at gcc dot gnu.org
  2012-10-14 18:40 ` david at doublewise dot net
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-10-14 17:44 UTC (permalink / raw)
  To: gcc-bugs


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |diagnostic
   Last reconfirmed|                            |2012-10-14
          Component|c++                         |libstdc++
     Ever Confirmed|0                           |1
           Severity|normal                      |enhancement

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-10-14 17:44:17 UTC ---
(In reply to comment #0)
> In other words, std::string str('0', 100) is undefined behavior.

I assume you mean std::string str("0", 100)

We might be able to use
http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html


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

* [Bug libstdc++/54924] Warn for std::string constructor with wrong size
  2012-10-14 17:06 [Bug c++/54924] New: Warn for std::string constructor with wrong size 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
  2021-12-15 21:30 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: david at doublewise dot net @ 2012-10-14 18:40 UTC (permalink / raw)
  To: gcc-bugs


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

David Stone <david at doublewise dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |david at doublewise dot net

--- Comment #2 from David Stone <david at doublewise dot net> 2012-10-14 18:40:33 UTC ---
Yeah, sorry, I meant the (char const *, size_t) overload, not the (size_t,
char) overload.


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

* [Bug libstdc++/54924] Warn for std::string constructor with wrong size
  2012-10-14 17:06 [Bug c++/54924] New: Warn for std::string constructor with wrong size 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
  2021-12-15 21:30 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-12-15 17:58 UTC (permalink / raw)
  To: gcc-bugs

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.

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

* [Bug libstdc++/54924] Warn for std::string constructor with wrong size
  2012-10-14 17:06 [Bug c++/54924] New: Warn for std::string constructor with wrong size david at doublewise dot net
                   ` (2 preceding siblings ...)
  2021-12-15 17:58 ` msebor at gcc dot gnu.org
@ 2021-12-15 21:30 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-15 21:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #16 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #14)
> Created attachment 43350 [details]
> Patch to use __builtin_object_size in std::string
> 
> So it isn't lost, here's a prototype I was working on last year (which only
> helps if you use -Wsystem-headers).

Huh, I forgot about this patch and then reinvented that wheel:
https://gcc.gnu.org/pipermail/gcc-patches/2021-October/581376.html

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

end of thread, other threads:[~2021-12-15 21:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-14 17:06 [Bug c++/54924] New: Warn for std::string constructor with wrong size 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
2021-12-15 21:30 ` redi 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).