public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/36299] New: spurious and undocumented warning with -Waddress for a == 0 when a is an array
@ 2008-05-22 9:05 vincent at vinc17 dot org
2008-05-22 20:01 ` [Bug c/36299] " pinskia at gcc dot gnu dot org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: vincent at vinc17 dot org @ 2008-05-22 9:05 UTC (permalink / raw)
To: gcc-bugs
With -Waddress (implied by -Wall), I get the following warning when I use the
test a == 0 where a is an array: the address of 'a' will never be NULL. This
behavior is undocumented and inconsistent (see below). Here's a testcase:
int main (void)
{
char a[1], *b;
b = a;
if (a == 0)
return 1;
else if (a == (void *) 0)
return 2;
else if (b == 0)
return 3;
else if (b == (void *) 0)
return 4;
return 0;
}
gcc warns only for a == 0 (and this is OK to use 0 instead of (void *) 0
because it is a valid form for a null pointer constant).
Moreover this is very similar to code like
if (1) ...
or code given in bug 12963, for which gcc no longer emits warnings: indeed such
kind of correct and useful code is typically used in macros.
--
Summary: spurious and undocumented warning with -Waddress for a
== 0 when a is an array
Product: gcc
Version: 4.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: vincent at vinc17 dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36299
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/36299] spurious and undocumented warning with -Waddress for a == 0 when a is an array
2008-05-22 9:05 [Bug c/36299] New: spurious and undocumented warning with -Waddress for a == 0 when a is an array vincent at vinc17 dot org
@ 2008-05-22 20:01 ` pinskia at gcc dot gnu dot org
2008-08-23 12:55 ` manu at gcc dot gnu dot org
2008-08-23 20:01 ` vincent at vinc17 dot org
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-05-22 20:01 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from pinskia at gcc dot gnu dot org 2008-05-22 20:00 -------
if (a == 0)
return 1;
else if (a == (void *) 0)
Those two should warn about being address being zero. "a" decays to a pointer
type and really &a[0].
-- Pinski
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36299
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/36299] spurious and undocumented warning with -Waddress for a == 0 when a is an array
2008-05-22 9:05 [Bug c/36299] New: spurious and undocumented warning with -Waddress for a == 0 when a is an array vincent at vinc17 dot org
2008-05-22 20:01 ` [Bug c/36299] " pinskia at gcc dot gnu dot org
@ 2008-08-23 12:55 ` manu at gcc dot gnu dot org
2008-08-23 20:01 ` vincent at vinc17 dot org
2 siblings, 0 replies; 4+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-23 12:55 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from manu at gcc dot gnu dot org 2008-08-23 12:53 -------
Andrew, how could we detect that it is a decayed array?
I think we would like to warn for
if (a == (void *) 0)
but not for
if ((void *)a == 0)
or even if possible not for
if (&a[0] == 0)
Vincent,
this warning was added on purpose, because probably someone requested it. I
don't see that it is very different from the documented case of using the
address of a function in a conditional.
You should be able to work-around the macro case by casting the array to (char
*) or perhaps casting to (void *) ? That said, we would like to not warn within
macros for a wide range of warnings but we don't have the infrastructure to do
that yet.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36299
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/36299] spurious and undocumented warning with -Waddress for a == 0 when a is an array
2008-05-22 9:05 [Bug c/36299] New: spurious and undocumented warning with -Waddress for a == 0 when a is an array vincent at vinc17 dot org
2008-05-22 20:01 ` [Bug c/36299] " pinskia at gcc dot gnu dot org
2008-08-23 12:55 ` manu at gcc dot gnu dot org
@ 2008-08-23 20:01 ` vincent at vinc17 dot org
2 siblings, 0 replies; 4+ messages in thread
From: vincent at vinc17 dot org @ 2008-08-23 20:01 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from vincent at vinc17 dot org 2008-08-23 20:00 -------
(In reply to comment #2)
> this warning was added on purpose, because probably someone requested it. I
> don't see that it is very different from the documented case of using the
> address of a function in a conditional.
The documentation should be improved anyway (the word "suspicious" is very
subjective).
> You should be able to work-around the macro case by casting the array to (char
> *) or perhaps casting to (void *) ?
Yes, this makes sense. Perhaps this should be documented.
> That said, we would like to not warn within
> macros for a wide range of warnings but we don't have the infrastructure to do
> that yet.
How about something like __extension__, e.g. __no_warnings__ would disable the
warnings for the following statement or expression? If expression, one could
still use __no_warnings__ with ({ ... }). Keywords for individual warnings or
warning groups would even be better. At the same time, it would be nice to have
some macro defined, declaring that such a keyword is available (that would be
much better than testing the GCC version).
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36299
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2008-08-23 20:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-22 9:05 [Bug c/36299] New: spurious and undocumented warning with -Waddress for a == 0 when a is an array vincent at vinc17 dot org
2008-05-22 20:01 ` [Bug c/36299] " pinskia at gcc dot gnu dot org
2008-08-23 12:55 ` manu at gcc dot gnu dot org
2008-08-23 20:01 ` vincent at vinc17 dot org
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).