[PATCH][_GLIBCXX_DEBUG] Review null string assertions (was: Add basic_string::starts_with/ends_with checks)
François Dumont
frs.dumont@gmail.com
Wed Aug 31 04:38:06 GMT 2022
If I got your point correctly here is this patch again with just the
change on '0' becoming 'nullptr' in assertions.
libstdc++: [_GLIBCXX_DEBUG] Review nullptr assertion diagnostics
Review null string checks to show:
_String != nullptr
rather than:
_String != 0
libstdc++-v3/ChangeLog:
* include/debug/debug.h: Use nullptr rather than '0' in
checks in post-C++11.
* include/debug/string: Likewise.
*
testsuite/21_strings/basic_string/operations/ends_with/char.cc: Use
__gnu_test::string.
*
testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc: Likewise.
*
testsuite/21_strings/basic_string/operations/ends_with/wchar_t.cc: Likewise.
*
testsuite/21_strings/basic_string/operations/starts_with/wchar_t.cc:
Likewise.
*
testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc:
Likewise.
*
testsuite/21_strings/basic_string/operations/starts_with/char.cc: Likewise..
Ok to commit ?
François
On 26/08/22 11:31, Jonathan Wakely wrote:
> On Sun, 14 Aug 2022 at 16:34, François Dumont via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
>> I think we can add those checks.
>>
>> Note that I wonder if it was needed as in basic_string_view I see usages
>> of __attribute__((__nonnull__)). But running the test I saw no impact
>> even after I try to apply this attribute to the starts_with/ends_with
>> methods themselves.
> That should cause warnings, and does when I try it.
>
> As you say, the relevant string_view constructor already has that anyway:
>
> __attribute__((__nonnull__)) constexpr
> basic_string_view(const _CharT* __str) noexcept
>
> And so does string_view::find. The problem is that those only help if
> the compiler inlines the calls to those functions and so can propagate
> the null value all the way down to a function with the attribute.
> Adding the attribute to the relevant starts_with, ends_with and
> contains functions makes the diagnostics more likely to be emitted
> without optimization.
>
>> Also note that several checks like the ones I am adding here are XFAILS
>> when using 'make check' because of the segfault rather than on a proper
>> debug checks. Would you prefer to add dg-require-debug-mode to those ?
>>
>> libstdc++: [_GLIBCXX_DEBUG] Add basic_string::starts_with/ends_with
>> checks
>>
>> Add simple checks on C string parameters which should not be null.
>>
>> Review null string checks to show:
>> _String != nullptr
>>
>> rather than:
>> _String != 0
> I don't really like the extra complexity in the macros, but this does
> seem like a nice improvement for what users see.
>
> We could use __null for C++98, which is a compiler keyword that
> expands to a null pointer constant, but I'm not sure if that would be
> clear to all users or not. Maybe 0 is better.
>
> .
-------------- next part --------------
A non-text attachment was scrubbed...
Name: debug_nullptr_string.patch
Type: text/x-patch
Size: 7977 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20220831/4304347c/attachment.bin>
More information about the Libstdc++
mailing list