-Wstringop-overflow warning in std::string
Martin Sebor
msebor@gmail.com
Thu Feb 27 21:43:00 GMT 2020
On 2/27/20 2:06 PM, Jonathan Wakely wrote:
> Compiling libstd++-v3/testsuite/21_strings/basic_string/cons/char/1.cc
> with
>
> Â -O2 -D_GLIBCXX_USE_CXX11_ABI=0 -D_GLIBCXX_ASSERTIONS
>
> I see the following warning:
>
> In file included from
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/string:40,
>
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â from
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/stdexcept:39,
>
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â from
> /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/1.cc:23:
>
> 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::basic_string<_CharT, _Traits,
> _Alloc>::_M_copy(_CharT*, const _CharT*, std::basic_string<_CharT,
> _Traits, _Alloc>::size_type) [with _CharT = char; _Traits =
> std::char_traits<char>; _Alloc = std::allocator<char>]' at
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:3429,
>
> Â Â Â inlined from 'static void std::basic_string<_CharT, _Traits,
> _Alloc>::_M_copy(_CharT*, const _CharT*, std::basic_string<_CharT,
> _Traits, _Alloc>::size_type) [with _CharT = char; _Traits =
> std::char_traits<char>; _Alloc = std::allocator<char>]' at
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:3424,
>
> Â Â Â inlined from 'static void std::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
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:3476,
>
> Â Â Â inlined from 'static _CharT* std::basic_string<_CharT, _Traits,
> _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&,
> std::forward_iterator_tag) [with _FwdIterator = const char*; _CharT =
> char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
> at
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc:582,
>
> Â Â Â inlined from 'static _CharT* std::basic_string<_CharT, _Traits,
> _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&,
> std::forward_iterator_tag) [with _FwdIterator = const char*; _CharT =
> char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
> at
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc:565,
>
> Â Â Â inlined from 'static _CharT* std::basic_string<_CharT, _Traits,
> _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&,
> std::__false_type) [with _InIterator = const char*; _CharT = char;
> _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:5121,
>
> Â Â Â inlined from 'static _CharT* std::basic_string<_CharT, _Traits,
> _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with
> _InIterator = const char*; _CharT = char; _Traits =
> std::char_traits<char>; _Alloc = std::allocator<char>]' at
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:5142,
>
> Â Â Â inlined from 'std::basic_string<_CharT, _Traits,
> _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits,
> _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits =
> std::char_traits<char>; _Alloc = std::allocator<char>]' at
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc:659,
>
> Â Â Â inlined from 'void test01()' at
> /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/1.cc:86:
>
> /home/jwakely/src/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:365:
> warning: 'void* __builtin_memcpy(void*, const void*, long unsigned int)'
> specified bound 18446744073709551615 exceeds maximum object size
> 9223372036854775807 [-Wstringop-overflow=]
>
> This warning is right, the length passed to the string constructor (on
> line 86) is SIZE_MAX, which is clearly a bug. But in practice the
> memcpy will never be reached because we check the length and throw an
> exception (which is what the testcase is checking for).
>
> Should I just add -Wno-stringop-overflow to the testcase, or would it
> be worth doing something like this to inform the compiler?
I'm not sure. Since the ctor is undefined when passed a size in
excess of the argument size the warning might be useful even if
the library handles it. But then again, since the library handles
it (at least in a subset of cases) it's not necessarily undefined.
For what it's worth, when I wrote the library we grappled with
this question and ended up deciding that providing well-defined
semantics for undefined behavior wasn't as helpful as it seemed
at first unless we could do it reliably.
Martin
>
> --- a/libstdc++-v3/include/bits/basic_string.h
> +++ b/libstdc++-v3/include/bits/basic_string.h
> @@ -3423,6 +3423,8 @@ _GLIBCXX_END_NAMESPACE_CXX11
> Â Â Â Â Â Â static void
> Â Â Â Â Â Â _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
> _GLIBCXX_NOEXCEPT
> Â Â Â Â Â Â {
> +Â Â Â Â Â Â if (__n > _Rep::_S_max_size)
> +Â Â Â Â Â Â Â Â __builtin_unreachable();
> Â Â Â Â Â Â Â if (__n == 1)
> Â Â Â Â Â Â Â Â Â traits_type::assign(*__d, *__s);
> Â Â Â Â Â Â Â else
>
>
>
More information about the Libstdc++
mailing list