https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100064
Bug ID: 100064
Summary: False positive with -Wincompatible-pointer-types
Product: gcc
Version: 10.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: T6NRR4KFLJO5IUOPHFOXNFTPO at cybercat dot cc
Target Milestone: ---
Created attachment 50582
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50582&action=edit
An example source code triggering this
GCC should not produce a warning with -Wincompatible-pointer-types enabled when
someone attempts to pass (or assign) a pointer from a struct that contains
(directly or indirectly) as its first element the expected type.
This method is used commonly in a multitude of software, such as glib
<https://developer.gnome.org/gobject/stable/chapter-gobject.html>, doom
<https://doomwiki.org/wiki/Thinker>, and basically pretty much any code that
tries to implement some form of inheritance in C. All this software currently
has to use a cast or use void* in order to avoid this warning, throwing away
type safety in the process.
Consider a file aaa.c (also attached):
struct a {
int x;
};
struct b {
struct a parent;
};
int f(struct a *a) { return a->x; }
int main(){
struct b b = {{0}};
f(&b);
}
Compiling with gcc aaa.c produces:
aaa.c: In function ‘main’:
aaa.c:13:5: warning: passing argument 1 of ‘f’ from incompatible pointer type
[-Wincompatible-pointer-types]
13 | f(&b);
| ^~
| |
| struct b *
aaa.c:9:17: note: expected ‘struct a *’ but argument is of type ‘struct b *’
9 | int f(struct a *a) { return a->x; }
| ~~~~~~~~~~^