GNOME Bugzilla – Bug 141080
Patch to remove multiple warnings in gdk/x11/gdkdnd-x11.c
Last modified: 2005-08-15 01:47:26 UTC
This is a tricky one. I was getting the following warnings: gdkdnd-x11.c: In function `motif_read_target_table': gdkdnd-x11.c:920: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkdnd-x11.c: In function `motif_check_dest': gdkdnd-x11.c:1270: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkdnd-x11.c: In function `motif_read_initiator_info': gdkdnd-x11.c:1464: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkdnd-x11.c: In function `xdnd_check_dest': gdkdnd-x11.c:2384: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkdnd-x11.c:2404: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkdnd-x11.c: In function `xdnd_read_actions': gdkdnd-x11.c:2451: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkdnd-x11.c: In function `xdnd_enter_filter': What I've found is the following: MotifTargetTableHeader *header = NULL; . . . XGetWindowProperty (display_x11->xdisplay, display_x11->motif_drag_window, motif_drag_targets_atom, 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE, motif_drag_targets_atom, &type, &format, &nitems, &bytes_after, (guchar **)&header); is breaks gcc's C99 strict aliasing rules. To get around this, you need to convert a guchar** to a MotifTargetTableHeader*. The only way I can see that is valid is to create a guchar**, use this in parameter 12 of XGetWindowProperty, cast this to a temporary gpointer, and then cast the temporary gpointer to an XGetWindowProperty* variable. So the previous fragment will look like: MotifTargetTableHeader *header = NULL; . . . guchar **pheader; gpointer temp_header; . . . XGetWindowProperty (display_x11->xdisplay, display_x11->motif_drag_window, motif_drag_targets_atom, 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE, motif_drag_targets_atom, &type, &format, &nitems, &bytes_after, pheader); temp_header = (gpointer) &pheader; header = (MotifTargetTableHeader*) &temp_header; Very much a hack job, I feel (in fact I say as much in a comment in the diff). However, it does remove the strict-aliasing warnings in gcc.
Created attachment 27079 [details] [review] Proposed patch Incidently, this patch also removed some other warnings I was getting as I compiled the code.
There is some real doubt whether we want to cater to GCC's annoying paranoia/lack-of-understanding here at all, but if you did want to fix up this warning, the correct way to do it is to simply: guchar* data; XGetWindowProperty (..., &data, ...) header = (MotifTargetTableTable *)data;
I got annoyed when working something else and fixed this in CVS a while ago. Tue Nov 16 19:26:30 2004 Owen Taylor <otaylor@redhat.com> * gdk/x11/gdkdnd-x11.c (_gdk_drag_get_protocol_for_display): Squash strict aliasing warnings.