Re: std::cout breaks after nullptr

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

 



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?

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)
     ^~



// g++-8 -Wall -Wnonnull -O2 -o nonnull nonnull.cpp
#include <cstddef>

void f(int * ptr) __attribute__((nonnull));

void f(int * ptr)
{
    if(NULL != ptr)
    {
        *ptr = 1;
    }
}

int main()
{
    int * p = NULL;
    f(p);
}


I can easily get it past the g++-8 by coping the param locally - is this suitable though?


// g++-8 -Wall -Wnonnull -O2 -o nonnull nonnull.cpp
#include <cstddef>

void f(int * ptr) __attribute__((nonnull));

void f(int * ptr)
{
    const int * const p = ptr;
    if(NULL != p)
    {
        *ptr = 1;
    }
}

int main()
{
    int * p = NULL;
    f(p);
}




[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