GNOME Bugzilla – Bug 140722
strict aliasing fixes
Last modified: 2010-05-19 21:15:30 UTC
Attached patch fixes most warning of latest gcc (strict aliasing rules, incompatible pointer assignment, undefined function). Still remaining warnings: unused variables, potentially undefined value.
Created attachment 26916 [details] [review] gtk+-strict-aliasing.patch
Changes like this with just adding void */gpointer all over the place to hide the warnings aren't acceptable. In most cases, these warnings are highly dubious, and the right approach may be to file bug reports against GCC and turn off the warning with the appropriate GCC flags.
I will discuss it with gcc developers to get proper solution. It seems, that code, where gcc reports warning does not exactly correspond with miscompiled code. Related reports: gail: bug 140604, pango: bug 140495, glib: bug 140482, libwnck: 140175.
Additional info from Andreas Schwab: The interface is ill-defined. You can't expect to be able to stuff a void* into every pointer. The only way to fix that portably is to pass the address of a real void* variable and copy the value in the caller.
I've gone through a few of these, and yes, some of them need to be solved as described (make temp gpointer variable, then copy the address of the differently typed variable into it - then pass the temp gpointer variable to the function). So far, I've tried to resolve a good proportion of the warnings that are coming from gcc when it attempts to compile gtk+ (more to come): * Bug 140346: libmoduletestplugin_a.c gives type-punned warning when compiling glibc-2.2.3 * Bug 141010: When compiling queryloaders.c gcc generates warning that dereferencing type-punned pointer breaks strict-aliasing * Bug 141011: Unused variable i in gtk_cell_view_cell_layout_clear of gtkcellview.c * Bug 141020: gtkmain.c: multiple warnings about deferencing type-punned pointer breaks strict aliasing rules * Bug 141024: gtkselection.c: gtk_selection_convert - warning about deferencing type-punned pointer breaking strict aliasing rules * Bug 141027: gtktipsquery.c: gtk_tips_query_event - warning about deferencing type-punned pointer breaks strict aliasing rules * Bug 141030: gtkwidget.c: gtk_widget_reparent_subwindows - warning about deferencing type-punned pointer breaking strict aliasing rules
Er... bug 141011 has nothing to do with this issue. It's another warning I fixed.
Now also: * Bug 141066: queryimmodules.c: query_module - warning: dereferencing type-punned pointer will break strict-aliasing rules * Bug 141068: demos/gtk-demo/changedisplay.c: find_toplevel_at_pointer - warning: dereferencing type-punned pointer will break strict-aliasing rules * Bug 141069: Bug 141068: demos/gtk-demo/textview.c: easter_egg_callback - warning: dereferencing type-punned pointer will break strict-aliasing rules * Bug 141072: tests/testgtk.c: many warnings fixed
Please note, that my upper mentioned patch removes warnings, but some places are not correct. Bad strict aliasing demo: long *v; long l; v = malloc(sizeof(long)); *(long *)v = 0; *(short *)v = -1; /* No warning, but does not work. */ printf("p: %ld\n", *v); /* 0 */ printf("p: %ld\n", *v); /* 65535 */ *(long *)(&l) = 0; *(short *)(&l) = -1; /* Here you get warning, but code works. */ printf("l: %ld\n", l); /* 65535 */ printf("l: %ld\n", l); /* 65535 */
Um... I assume that the printfs here demonstrate what happens after you do the different casts... in other words the first *(long *)v = 0; goes with the first printf, and the *(short *)v = -1 goes with the second printf. Just clarifying...
Mass changing gtk+ bugs with target milestone of 2.4.2 to target 2.4.4, as Matthias said he was trying to do himself on IRC and was asking for help with. If you see this message, it means I was successful at fixing the borken-ness in bugzilla :) Sorry for the spam; just query on this message and delete all emails you get with this message, since there will probably be a lot.
Yes, cross-posting is evil. Nevertheless I'll do (http://bugzilla.gnome.org/show_bug.cgi?id=335341#c6). Why not add this to <glib/gmacros.h>: #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) # define G_GNUC_MAY_ALIAS __attribute__((may_alias)) #else # define G_GNUC_MAY_ALIAS #endif After that enhancement wherever aliasing is needed this could be written: static GtkWidget *G_GNUC_MAY_ALIAS dialog = NULL; to make this code work without warning: g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer*) &dialog);
Ah, I didn't know about may_alias. That sounds like a good idea to me.
Is this any clearer now? Do these warnings still exist?
*** Bug 423903 has been marked as a duplicate of this bug. ***
No, it is not cleaner: queryloaders.c: In function ‘query_module’: queryloaders.c:164: warning: dereferencing type-punned pointer will break strict-aliasing rules queryloaders.c:165: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkevents-x11.c: In function 'IA__gdk_x11_screen_get_window_manager_name': gdkevents-x11.c:2707: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkevents-x11.c: In function 'IA__gdk_x11_screen_supports_net_wm_hint': gdkevents-x11.c:2807: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkgeometry-x11.c: In function 'queue_item_free': gdkgeometry-x11.c:1018: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkgeometry-x11.c: In function 'gdk_window_queue': gdkgeometry-x11.c:1095: warning: dereferencing type-punned pointer will break strict-aliasing rules gdkwindow-x11.c: In function 'set_text_property': gdkwindow-x11.c:2802: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:99: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:100: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:101: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:102: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:103: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:104: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:105: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:106: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:107: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:108: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:109: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:110: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:111: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:112: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:113: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:114: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:115: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginebeagle.c:116: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginetracker.c:57: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginetracker.c:58: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginetracker.c:59: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginetracker.c:60: warning: dereferencing type-punned pointer will break strict-aliasing rules gtksearchenginetracker.c:61: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkbuilderparser.c: In function 'parse_custom': gtkbuilderparser.c:684: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkfilesystem.c: In function 'gtk_file_system_module_load': gtkfilesystem.c:1279: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkfilesystem.c:1281: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkfilesystem.c:1283: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkgamma.c: In function 'button_clicked_callback': gtkgamma.c:410: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c: In function 'gtk_icon_view_accessible_set_scroll_adjustments': gtkiconview.c:8802: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c:8812: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c:8824: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c:8834: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c: In function 'gtk_icon_view_accessible_notify_gtk': gtkiconview.c:9069: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c:9079: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c: In function 'gtk_icon_view_accessible_initialize': gtkiconview.c:9106: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c:9115: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c:9133: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c: In function 'gtk_icon_view_accessible_destroyed': gtkiconview.c:9165: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkiconview.c:9175: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkimmodule.c: In function 'gtk_im_module_load': gtkimmodule.c:107: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkimmodule.c:109: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkimmodule.c:111: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkimmodule.c:113: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkmain.c: In function 'rewrite_event_for_grabs': gtkmain.c:1365: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkmain.c: In function 'IA__gtk_get_event_widget': gtkmain.c:2205: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkmodules.c: In function 'load_module': gtkmodules.c:283: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkmodules.c:300: warning: dereferencing type-punned pointer will break strict-aliasing rules gtknotebook.c: In function 'gtk_notebook_set_focus_child': gtknotebook.c:3926: warning: dereferencing type-punned pointer will break strict-aliasing rules gtknotebook.c:3929: warning: dereferencing type-punned pointer will break strict-aliasing rules gtknotebook.c: In function 'gtk_notebook_real_remove': gtknotebook.c:4368: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c: In function 'gtk_paned_set_saved_focus': gtkpaned.c:1348: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c:1354: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c: In function 'gtk_paned_set_first_paned': gtkpaned.c:1362: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c:1368: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c: In function 'gtk_paned_set_last_child1_focus': gtkpaned.c:1376: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c:1382: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c: In function 'gtk_paned_set_last_child2_focus': gtkpaned.c:1390: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkpaned.c:1396: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkselection.c: In function 'IA__gtk_selection_convert': gtkselection.c:1067: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkthemes.c: In function 'gtk_theme_engine_load': gtkthemes.c:90: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkthemes.c:92: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkthemes.c:94: warning: dereferencing type-punned pointer will break strict-aliasing rules gtktipsquery.c: In function 'gtk_tips_query_event': gtktipsquery.c:476: warning: dereferencing type-punned pointer will break strict-aliasing rules gtktooltip.c: In function 'find_widget_under_pointer': gtktooltip.c:570: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkwidget.c: In function 'gtk_widget_reparent_subwindows': gtkwidget.c:4765: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkwidget.c:4793: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkwindow.c: In function 'get_screen_icon_info': gtkwindow.c:2860: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkwindow.c:2866: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkwindow.c: In function 'get_pixmap_and_mask': gtkwindow.c:2981: warning: dereferencing type-punned pointer will break strict-aliasing rules gtkwindow.c:2984: warning: dereferencing type-punned pointer will break strict-aliasing rules queryimmodules.c: In function ‘query_module’: queryimmodules.c:116: warning: dereferencing type-punned pointer will break strict-aliasing rules queryimmodules.c:117: warning: dereferencing type-punned pointer will break strict-aliasing rules queryimmodules.c:118: warning: dereferencing type-punned pointer will break strict-aliasing rules queryimmodules.c:119: warning: dereferencing type-punned pointer will break strict-aliasing rules changedisplay.c: Infind_toplevel_at_pointer’: changedisplay.c:84: warning: dereferencing type-punned pointer will break strict-aliasing rules textview.c: In function ‘easter_egg_callback’: textview.c:590: warning: dereferencing type-punned pointer will break strict-aliasing rules However everything might work just now for many processors even with these warnings, there is no guarantee, that the code will be compiled correctly without -fno-strict-aliasing.
Created attachment 95722 [details] [review] gtk+-strict-aliasing.patch Proposed fix. It is against gtk+-2.11.5, but applies cleanly on 2.12.0 as well.
This problem has been fixed in our software repository, at least in the current stable version (2.20) and in the previous one (2.18). Thank you for your bug report.