Re: std::string add nullptr attribute

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 9 Feb 2023 at 16:30, Xi Ruoyao wrote:
>
> On Thu, 2023-02-09 at 14:56 +0000, Jonathan Wakely via Gcc-help wrote:
> > > Note, my code isn't like this, it is just an example to suggest
> > > adding the nullptr attribute, as its clearly already rejected at
> > > runtime.
> >
> > I assume you mean the nonnull attribute. That was added in 2020 and
> > then reverted because it broke some things:
>
> I remember I'd once made the same mistake when I suggested to add
> nonnull for ostream::operator<<(const string &) and I was lectured:
> nonnull is not only a diagnostic attribute, it also allows the compiler
> to assume the parameter is never null and rendering std::string(nullptr)
> an undefined behavior.

Yes, I think that's what might have happened with the std::string change.

> Then the example may just silently continue to run, instead of throwing
> an exception.  It would be an ironic example: an attempt to improve
> diagnostic finally made diagnostic more difficult.

Indeed.

Maybe we can add __attribute__((access(read, 1))) instead, which says
that we will read from the pointer, which also implies it must be
non-null.

N.B. in C++23 string(nullptr) produces an error, although
string((const char*)nullptr) doesn't, so in practice it only prevents
the dumbest calls with a literal 'nullptr' token, and not the more
realistic problems where you have a pointer that happens to be null.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux