From: peter0x44 <peter0x44@disroot.org>
To: gcc@gcc.gnu.org
Subject: Differences between clang and gcc handling of int[static n] function arguments
Date: Sun, 21 May 2023 23:40:14 +0000 [thread overview]
Message-ID: <2e8a8750bb4b44a535a9df669183f6fa@disroot.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 2992 bytes --]
Hi,
So, recently I learned about the c99 feature to get NULL pointer checks
for array function arguments.
I have really never seen this feature used in an actual codebase. It's
definitely something I wanted on a few occasions.
To be clear, I'm talking about specifically:
void foo(int array[static 1]);
I checked what warnings this produces - gcc by default produces, none,
but with -Wall it produces for this code:
int foo(int array[static 1]){ return array[0]; }
int main(void)
{
#define NULL (void*)0
foo(NULL);
}
bruh.c: In function 'main':
bruh.c:8:9: warning: argument 1 to 'int[static 1]' is null where
non-null expected [-Wnonnull]
8 | foo(NULL);
| ^~~~~~~~~
bruh.c:3:5: note: in a call to function 'foo'
3 | int foo(int array[static 1]){ return array[0]; }
| ^~~
I think this warning is acceptable, but has some scope for improvement.
I checked what clang did instead, and it seemed nicer, for sure.
bruh.c:8:2: warning: null passed to a callee that requires a non-null
argument [-Wnonnull]
foo(NULL);
^ ~~~~
bruh.c:3:13: note: callee declares array parameter as static here
int foo(int array[static 1]){ return array[0]; }
^ ~~~~~~~~~~
It's pointing me exactly to the parameter with the static directly, so
there is no ambiguity
Also, this is a warning enabled by default, no need to pass -Wall.
Is there a reason gcc doesn't enable this by default? To me, it seems
like a warning that's desirable always.
You are explicitly agreeing to never call these functions with NULL, any
code doing that is surely broken.
There's no way this gives a false positive, ever.
I'm definitely adding this warning to -Werror on all of my future
projects, now that I know about it.
One last thing worth mentioning, is that GCC makes a nicer warning than
clang when this is done through __attribute__((nonnull))
bruh.c: In function 'main':
bruh.c:8:9: warning: argument 1 null where non-null expected [-Wnonnull]
8 | foo(NULL);
| ^~~
bruh.c:3:5: note: in a call to function 'foo' declared 'nonnull'
3 | int foo(int array[1]){ return array[0]; }
| ^~~
It points out specifically that that it is done through the attribute.
I think it would be nice if the attribute could be underlined also,
though.
Clang produces:
bruh.c:8:10: warning: null passed to a callee that requires a non-null
argument [-Wnonnull]
foo(NULL);
~~~~^
with no mention that the warning is specifically because of the
attribute.
I tried looking on the bug tracker and I could find nothing elaborating
on this. Maybe I'm not looking hard enough.
I would be happy to open a PR to improve this warning, if there isn't
one already.
It seems it might even be trivial enough for me to investigate and
tackle myself, in some spare time.
I see very little code using either of these features, so it's
definitely not a high priority task regardless.
next reply other threads:[~2023-05-21 23:40 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-21 23:40 peter0x44 [this message]
2023-05-23 17:47 ` Jonathan Wakely
2023-05-24 9:06 ` Florian Weimer
2023-05-24 9:26 ` Jonathan Wakely
2023-05-24 9:42 ` Florian Weimer
2023-05-24 9:51 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2e8a8750bb4b44a535a9df669183f6fa@disroot.org \
--to=peter0x44@disroot.org \
--cc=gcc@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).