GNOME Bugzilla – Bug 754755
playbin: Compiler warning with 64bit Windows target MinGW, cast to pointer from integer of different size
Last modified: 2015-09-12 09:48:35 UTC
Created attachment 310945 [details] [review] Fix this problem We get the following error by building with 64bit Windows target MinGW: ---- gstplaybin2.c: In function 'pad_added_cb': gstplaybin2.c:3476:7: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] (gpointer) group_id_probe_handler); ^ cc1: all warnings being treated as errors ---- It shows that gulong to gpointer cast is warned. We can use portable GUINT_TO_POINTER() macro for this propose.
Comment on attachment 310945 [details] [review] Fix this problem No that won't work as guint is not necessarily the same size as gulong. You need a GULONG_TO_POINTER and GPOINTER_TO_ULONG macro for this. And for consistency the GPOINTER_TO_ULONG macro should be used in the other places.
Unfortunately, GULONG_TO_PINTER() isn't provided by GLib. But GUINT_TO_POINTER() works with gulong because GUINT_TO_PINTER() casts to gulong (or guint64) and then casts to gpointer. https://developer.gnome.org/glib/stable/glib-Type-Conversion-Macros.html#GUINT-TO-POINTER:CAPS > #define GUINT_TO_POINTER(u) ((gpointer) (gulong) (u)) GUINT_TO_POINTER() definition is different for 32bit Windows and 64bit Windows: For 32bit Windows: https://git.gnome.org/browse/glib/tree/glib/glibconfig.h.win32.in#n122 > #define GUINT_TO_POINTER(u) ((gpointer) (u)) It directly casts to gpointer. For 64bit Windows: https://git.gnome.org/browse/glib/tree/glib/glibconfig.h.win32.in#n137 > #define GUINT_TO_POINTER(u) ((gpointer) (guint64) (u)) It casts to guint64 and then casts to gpointer.
I think instead of depending on this undocumented behaviour of the macros, we should rather define our own macros that explicitly do things via gulongs.
If we define a macro, the following result may be helpful: ---- typedef unsigned long gulong; typedef void* gpointer; #ifdef _WIN64 # include <stdint.h> typedef unsigned __int64 guint64; # define GST_ULONG_TO_POINTER(x) ((gpointer) (guint64) (x)) #else # define GST_ULONG_TO_POINTER(x) ((gpointer) (x)) #endif int main(void) { gulong x = 0; gpointer y; y = GST_ULONG_TO_POINTER(x); y = (gpointer) x; return 0; } ---- ---- % gcc -Wall -Wno-unused-but-set-variable -o a.exe a.c % i686-w64-mingw32-gcc -Wall -Wno-unused-but-set-variable -o a.exe a.c % x86_64-w64-mingw32-gcc -Wall -Wno-unused-but-set-variable -o a.exe a.c a.c: In function 'main': a.c:19:7: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] y = (gpointer) x; ^ ----
Try casting gpointer to guintptr and then guintptr to gulong. And gulong to guintptr before casting guintptr to gpointer.
---- typedef unsigned long gulong; typedef void* gpointer; #ifdef _WIN64 # include <stdint.h> typedef unsigned __int64 guintptr; #elif defined(_WIN32) typedef unsigned int guintptr; #else typedef unsigned long guintptr; #endif #define GST_ULONG_TO_POINTER(x) ((gpointer) (guintptr) (x)) int main(void) { gulong x = 0; gpointer y; y = GST_ULONG_TO_POINTER(x); return 0; } ---- ---- % gcc -Wall -Wno-unused-but-set-variable -o a a.c % i686-w64-mingw32-gcc -Wall -Wno-unused-but-set-variable -o a.exe a.c % x86_64-w64-mingw32-gcc -Wall -Wno-unused-but-set-variable -o a.exe a.c ----
Looks good, do you want to provide an updated patch for this?
No. Could you fix this?
No, because I don't have access to a Windows machine currently
How about the following? You need to just add GST_ULONG_TO_POINTER() macro and use it. If it doesn't work with MinGW, I'll send a patch. I don't know where is the best place to define the macro.
Just inside gstplaybin2.c, #define a ULONG_TO_POINTER() macro and use it in the file :)
Created attachment 311152 [details] [review] Use guintptr
Looks good but I wonder why it doesn't complain in the code that converts the pointers to ulongs :)
Comment on attachment 311152 [details] [review] Use guintptr Merged and fixed to actually use the new macro. Thanks :)
commit ab64b00b48ab78496245d5bb0eda1d51fcb9e209 Author: Kouhei Sutou <kou@clear-code.com> Date: Fri Sep 11 23:48:05 2015 +0900 playback: fix build error for 64bit Windows build by MinGW Casting to gpointer from gulong generates the following warning with 64bit Windows target MinGW: gstplaybin2.c: In function 'pad_added_cb': gstplaybin2.c:3476:7: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] (gpointer) group_id_probe_handler); ^ cc1: all warnings being treated as errors We should cast to guintptr from gulong before we cast to gpointer. https://bugzilla.gnome.org/show_bug.cgi?id=754755
Oh... Sorry for my stupid patch... I've confirmed that ULONG_TO_POINTER() works on x86_64-w64-mingw32-gcc.