[committed] libstdc++: Disable over-zealous warnings about std::string copies [PR103332]

Martin Sebor msebor@gmail.com
Fri Dec 10 01:49:11 GMT 2021


On 12/9/21 5:38 PM, Martin Sebor wrote:
> On 12/9/21 4:24 PM, Jonathan Wakely via Gcc-patches wrote:
>> These warnings are triggered by perfectly valid code using std::string.
>> They're particularly bad when --enable-fully-dynamic-string is used,
>> because even std::string().begin() will give a warning.
>>
>> Use pragmas to stop the troublesome warnings for copies done by
>> std::char_traits.
> 
> I'm still experimenting with some of the approaches we discussed
> last week, but based on my findings so far this was going to be
> my suggestion at lest for now, until or unless the problem turns
> out to affect more code than just std::string.

Just minutes after I wrote this I tried following the clue
in the note printed for the test case from PR 103534 with
an enhancement I'm experimenting with:

/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:426:56: 
warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ 
specified size between 18446744073709551600 and 18446744073709551615 
exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
   426 |         return static_cast<char_type*>(__builtin_memcpy(__s1, 
__s2, __n));
       | 
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:426:56: 
note: when 
‘(<anonymous>.std::__cxx11::basic_string<char>::_M_string_length > 
18446744073709551599)’

and adding an assert to string::size():

       constexpr
       size_type
       size() const noexcept
       {
         if (_M_string_length >= -1LU >> 1)
           __builtin_unreachable ();
         return _M_string_length;
       }

That gets rid of the false positive in this PR.  I realize
the others happen for other reasons but this approach at least
suggests that there might be other ways to suppress them than
the #pragma.  Unlike it, the alternative approaches should also
improve codegen.

> 
> That said, I noticed a typo in the patch:
> 
>>
>> libstdc++-v3/ChangeLog:
>>
>>     PR libstdc++/103332
>>     PR libstdc++/102958
>>     PR libstdc++/103483
>>     * include/bits/char_traits.h: Suppress stringop and array-bounds
>>     warnings.
>> ---
>>   libstdc++-v3/include/bits/char_traits.h | 7 +++++++
>>   1 file changed, 7 insertions(+)
>>
>> diff --git a/libstdc++-v3/include/bits/char_traits.h 
>> b/libstdc++-v3/include/bits/char_traits.h
>> index da3e0ffffaa..3f7befcf8b2 100644
>> --- a/libstdc++-v3/include/bits/char_traits.h
>> +++ b/libstdc++-v3/include/bits/char_traits.h
>> @@ -54,6 +54,11 @@ namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
>>   {
>>   _GLIBCXX_BEGIN_NAMESPACE_VERSION
>> +#pragma GCC diagnostic push
>> +#pragma GCC diagnostic ignored "-Wstringop-overflow"
>> +#pragma GCC diagnostic ignored "-Wstringop-overread"
>> +#pragma GCC diagnostic ignored "-Warray-bounds"
> 
> (Just for reference, as I mentioned in my private mail, at -O1
> the same code also triggers -Wfree-nonheap-object.)
> 
>> +
>>     /**
>>      *  @brief  Mapping from character type to associated types.
>>      *
>> @@ -990,6 +995,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>>     } // namespace __detail
>>   #endif // C++20
>> +#pragma GCC diagnostic push
> 
> This should be pop.
> 
> Martin
> 
>> +
>>   _GLIBCXX_END_NAMESPACE_VERSION
>>   } // namespace
>>
> 



More information about the Libstdc++ mailing list