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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonny Grant from comment #2)
> I was taught to validate parameters at University. Personally I always
> follow defensive programming approaches to avoid crashes.

So stop passing null to these functions then :-)

> So I would check
> parameters on all interface methods and operators. I would rely on the
> compiler to remove any unnecessary duplicate sanity checks.

That doesn't necessarily work when the member functions are separately
compiled, as with most std::string member functions.

> > What would be the point of _GLIBCXX_DEBUG_PEDASSERT when there's already a
> > debug assertion there? Compiling with _GLIBCXX_DEBUG will already abort.
> 
> I don't see a debug assertion for _GLIBCXX_DEBUG_PEDASSERT  could you point
> out the file and line number to me please.

You already quoted it in your comment 0 above, it's right there in assign(const
_CharT*)!

      basic_string&
      assign(const _CharT* __s)
      {
        __glibcxx_requires_string(__s);


> Just compiled with -D_GLIBCXX_DEBUG but I don't get any abort, just the same
> SEGV
> https://godbolt.org/z/rjYG8Yrnh

If you want a PEDASSERT to fire you need to actually request pedantic
assertions.

https://godbolt.org/z/874x18G1G

/opt/compiler-explorer/gcc-trunk-20230227/include/c++/13.0.1/bits/basic_string.h:1645:
constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*)
[with _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]: Assertion '__s != nullptr' failed.

I'm not persuaded to change anything here. The performance of string
assignments is very important and adding an extra branch and throwing an
exception isn't free.

Reply via email to