This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/36989] New: Wrong type for (... ? ... : ...)
- From: "terra at gnome dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 31 Jul 2008 15:24:51 -0000
- Subject: [Bug c/36989] New: Wrong type for (... ? ... : ...)
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
With the code below, gcc complains over line 8, but not lines 14 and 17
because it deduces the wrong type for the ?: operator. The type of that
operator's result is dependent on whether something is a null pointer
constant and not. gcc fails to track that properly: it knows that
(const void *)0 is a null pointer constant, but it also thinks that
(void *)(void *)0 and (1 ? (void *)0 : (void *)0) are.
The chapter and verse is C99 6.15.15 #6: "if one operand is a null
pointer constant, the result has the type of the other operand;"
Null pointer constants are defined in C99 6.3.2.3 #3 to be an integer
constant expression with value zero or such an expression case to
type void *.
For the record, Sun's cc gets the invalid cases right, but also
rejects the valid case.
int
main (int argc, char **argv)
{
int *p;
int n;
/* Invalid: dereferences a void * */
*(n ? p : (const void *)0);
/* Valid since it dereferences an int* */
*(n ? p : (void *)0);
/* Invalid: dereferences a void * */
*(n ? p : (void *)(void *)0);
/* Invalid: dereferences a void * */
*(n ? p : (1 ? (void *)0 : (void *)0));
return 0;
}
--
Summary: Wrong type for (... ? ... : ...)
Product: gcc
Version: 4.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: terra at gnome dot org
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36989