Bug 114571 - -Wzero-as-null-pointer-constant does not complain about NULL
Summary: -Wzero-as-null-pointer-constant does not complain about NULL
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2024-04-03 09:26 UTC by Pierre Ossman
Modified: 2024-10-10 09:20 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Test case (407 bytes, text/x-csrc)
2024-04-03 09:26 UTC, Pierre Ossman
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pierre Ossman 2024-04-03 09:26:16 UTC
Created attachment 57857 [details]
Test case

We are looking at bringing up the TigerVNC project to a more modern C++ style, and one thing was using nullptr instead of NULL. We were very glad when we found -Wzero-as-null-pointer-constant to help out with this.

Unfortunately, it doesn't seem to do much for NULL in modern¹ gcc. It spots usage of "0", but not any "NULL". clang has no trouble finding both.

I've attached a test case with some comments on the cases we've seen.

¹ gcc 5 spots some, but not all NULL
Comment 1 Pierre Ossman 2024-04-03 09:29:38 UTC
Hmm.. I found bug 77513, and r9-873. So I guess this is intentional?

This makes the warning somewhat pointless. We want to make sure developers standardise on nullptr, both for style and since the behaviour of NULL is compiler dependent (if I'm understanding the C++ standard correctly).

It's also annoying if there is not a consensus between clang and gcc here.
Comment 2 Pierre Ossman 2024-04-03 11:29:07 UTC
Found another case that neither gcc 5, gcc 13, nor clang complain about for some odd reason:

>  assert(thing == NULL);

All three complain about:

>  assert(thing == 0);

Not sure what's going on here.
Comment 3 Pierre Ossman 2024-04-03 14:19:53 UTC
And another odd case; gcc 5 complains about this:

> const char *a;
> a = NULL;

but not:

> const char *a = NULL;

gcc 13 complains about neither, and clang about both.
Comment 4 Alejandro Colomar 2024-10-10 09:00:50 UTC
If you want a diagnostic for that,
you should add (or ask for) -Wnull-as-null-pointer-constant
NULL is not zero.

I want to get warned for 0 but not for NULL.
Comment 5 Pierre Ossman 2024-10-10 09:10:16 UTC
That may be, but -Wzero-as-null-pointer-constant is documented as facilitating conversion to "nullptr", which means it definitely should be complaining about NULL, since that might not be the same thing.

Compatibility with clang would also make everyone's lives easier.
Comment 6 Alejandro Colomar 2024-10-10 09:20:17 UTC
(In reply to Pierre Ossman from comment #5)
> That may be, but -Wzero-as-null-pointer-constant is documented as
> facilitating conversion to "nullptr",

That documentation should be fixed to remove that sentence.

> which means it definitely should be
> complaining about NULL, since that might not be the same thing.

You could move that sentence to a hypothetical -Wnull-as-null-pointer-constant.

Otherwise, how should I transition from 0 to NULL?  The name for the diagnostic is already taken.

> Compatibility with clang would also make everyone's lives easier.

Clang made a bad decision.  They should probably fix it.
Comment 7 Alejandro Colomar 2024-10-10 09:20:50 UTC
See also: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117059>