Re: std::cout breaks after nullptr

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

 



On 2019-03-25 11:58 +0000, Jonny Grant wrote:
> Hello!
> 
> On 25/03/2019 11:36, Xi Ruoyao wrote:
> > On 2019-03-25 12:11 +0100, Marc Glisse wrote:
> > > On Mon, 25 Mar 2019, Jonathan Wakely wrote:
> > > > On Sun, 24 Mar 2019, 13:53 Xi Ruoyao, <xry111@xxxxxxxxxxxxxxxx> wrote:
> > > > > Well, if you want this to be detected at compile time, maybe you can
> > > > > convince
> > > > > Jonathan to add an) to
> > > > > operator<<(basic_ostream&,
> > > > > const char *).  Then this case can be detected with "-O -Wnonnull".
> > > > 
> > > > That seems like a good idea.
> > > 
> > > Note that IIRC the attribute is also used for optimization purposes.
> > > Adding it would remove the test "if (!__s)". One could try and add the
> > > attribute only for user code and not while compiling the library itself,
> > > but that's getting complicated (_GLIBCXX_EXTERN_TEMPLATE, specializations
> > > for types other than char, etc).
> > 
> > Oh really.  I'd been misunderstanding "nonnull" as a pure diagnostic
> > attribute
> > for long time :(.
> 
> -Wnonnull causes warnings if the implementation checks for NULL. (Unless 
> -Wno-nonnull-compare is set - which is probably a bad idea)
> 
> Although, g++-8 doesn't give any warning from this, it only seems to 
> catch directly calling f(NULL);
> That's a little limited?

It seems so.  But for your "cout << p" example -O -Wnonnull works.

> Anyway - this is what the warning looks like in practice if the nonnull 
> attribute is on the function declaration.
> 
> nonnull.cpp: In function ‘void f(int*)’:
> nonnull.cpp:8:5: warning: nonnull argument ‘ptr’ compared to NULL 
> [-Wnonnull-compare]
>       if(NULL != ptr)
>       ^~
> I can easily get it past the g++-8 by coping the param locally - is this 
> suitable though?

No.  One day the optimizer would optimize it away with some code snip and then
we would recevice a bug report (though actually it's not a bug at all since the
standard demands p in "operator<<(basic_ostream& os, char * p)" to be not null.)
-- 
Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[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