build: Work around SIZE_MAX bug in OSX headers
PATCH v2 1/1 build: Work around SIZE_MAX bug in OSX headers
C99 requires SIZE_MAX to be declared with the same type as the integral promotion of size_t, but OSX mistakenly defines it as an 'unsigned long long' expression even though size_t is only 'unsigned long'. Rather than futzing around with whether size_t is 32- or 64-bits wide (which would be needed if we cared about using SIZE_T in a #if expression), let the compiler get the right type for us by virtue of integer promotion.
See also https://patchwork.ozlabs.org/patch/542327/ for an instance where the wrong type trips us up if we don't fix it for good in osdep.h.
Some versions of glibc make a similar mistake with SSIZE_MAX; the goal is that the approach of this patch could be copied to work around that problem if it ever becomes important to us.
---
v1 was here:
https://lists.gnu.org/archive/html/qemu-devel/2016-07/msg02520.html
The topic recently came up again, and I noticed this patch sitting on one of my older branches, so I've taken another shot at it.
https://lists.gnu.org/archive/html/qemu-devel/2016-10/msg00950.html
v2: rewrite into a configure check (not sure if directly adding a -D to QEMU_CFLAGS is the best, so advice welcome)
I lack easy access to a Mac box, so this is untested as to whether it actually solves the issue...
--- include/qemu/osdep.h | 8 ++++++++ configure | 13 +++++++++++++ 2 files changed, 21 insertions(+)
include/qemu/osdep.h +8 -0
@@ -141,6 +141,14 @@ extern int daemon(int, int);# error Unknown pointer size#endif +/* Mac OSX has a <stdint.h> bug that incorrectly defines SIZE_MAX with +* the wrong type. Our replacement isn't usable in preprocessor +* expressions, but it is sufficient for our needs. */ +#ifdef SIZE_MAX_BROKEN +#undef SIZE_MAX +#define SIZE_MAX ((sizeof(char)) * -1) +#endif +#ifndef MIN #define MIN (a, b) (((a) < (b)) ? (a) : (b))#endif
configure +13 -0
@@ -1725,6 +1725,19 @@ if test "$cocoa" = "yes"; then sdl=no fi +# Some versions of Mac OS X incorrectly define SIZE_MAX +cat > $TMPC << EOF +#include <stdint.h> +#include <stdio.h> +int main(int argc, char *argv[]) { + return printf("%zu", SIZE_MAX); +} +EOF + +if ! compile_object -Werror ; then + QEMU_CFLAGS="-DSIZE_MAX_BROKEN $QEMU_CFLAGS" +fi + ########################################## # L2TPV3 probe
Signature · 1 line
2.7.4
Quoted 22 lines · Eric Blake
C99 requires
SIZE_MAXto be declared with the same type as the integral promotion ofsize_t, but OSX mistakenly defines it as an 'unsigned long long' expression even thoughsize_tis only 'unsigned long'. Rather than futzing around with whethersize_tis 32- or 64-bits wide (which would be needed if we cared about usingSIZE_Tin a #if expression), let the compiler get the right type for us by virtue of integer promotion.See also https://patchwork.ozlabs.org/patch/542327/ for an instance where the wrong type trips us up if we don't fix it for good in
osdep.h.Some versions of glibc make a similar mistake with
SSIZE_MAX; the goal is that the approach of this patch could be copied to work around that problem if it ever becomes important to us.Signed-off-by Eric Blake <[email protected]>---
v1 was here:
https://lists.gnu.org/archive/html/qemu-devel/2016-07/msg02520.html
Quoted 6 lines
The topic recently came up again, and I noticed this patch sitting on one of my older branches, so I've taken another shot at it.
https://lists.gnu.org/archive/html/qemu-devel/2016-10/msg00950.htmlv2: rewrite into a configure check (not sure if directly adding a -D to
QEMU_CFLAGSis the best, so advice welcome)
Writing into config-host.mak would be preferable I think.
Quoted 2 lines
I lack easy access to a Mac box, so this is untested as to whether it actually solves the issue...
I ran a test with one of the cast workarounds locally removed, and this patch does allow the %zu version to build OK on OSX.
Quoted 10 lines
--- include/qemu/osdep.h | 8 ++++++++ configure | 13 +++++++++++++ 2 files changed, 21 insertions(+)include/qemu/osdep.h +0
-0
include/qemu/osdep.h extern int daemon(int, int); · line 146
Quoted 6 lines
include/qemu/osdep.h +3
-0@@ -141,6 +141,14 @@ extern int daemon(int, int);# error Unknown pointer size#endif +/* Mac OSX has a <stdint.h> bug that incorrectly defines SIZE_MAX with +* the wrong type. Our replacement isn't usable in preprocessor +* expressions, but it is sufficient for our needs. */
I guess if we get bitten by this it'll be obvious (break the OSX build) and we can fix it then.
Quoted 18 lines
include/qemu/osdep.h +5
-0+#ifdef SIZE_MAX_BROKEN +#undef SIZE_MAX +#define SIZE_MAX ((sizeof(char)) * -1) +#endif +#ifndef MIN #define MIN (a, b) (((a) < (b)) ? (a) : (b))#endif configure +2
-0@@ -1725,6 +1725,19 @@ if test "$cocoa" = "yes"; then sdl=no fi +# Some versions of Mac OS X incorrectly define SIZE_MAX +cat > $TMPC << EOF
configure if test "$cocoa" = "yes"; then · line 1739
Quoted 10 lines
configure +10
-0+#include <stdint.h> +#include <stdio.h> +int main(int argc, char *argv[]) { + return printf("%zu", SIZE_MAX); +} +EOF + +if ! compile_object -Werror ; then + QEMU_CFLAGS="-DSIZE_MAX_BROKEN $QEMU_CFLAGS" +fi
thanks
-- PMM
Quoted 5 lines · Peter Maydell
Quoted 2 lines
v2: rewrite into a configure check (not sure if directly adding a -D to
QEMU_CFLAGSis the best, so advice welcome)Writing into config-host.mak would be preferable I think.
Okay, attempted in v4.
Quoted 5 lines
Quoted 2 lines
I lack easy access to a Mac box, so this is untested as to whether it actually solves the issue...
I ran a test with one of the cast workarounds locally removed, and this patch does allow the %zu version to build OK on OSX.
Thanks. But v3 leaves off Tested-by, because I still need to make sure my configure magic worked correctly :)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 9 lines · Eric Blake
Quoted 5 lines · Peter Maydell
Quoted 2 lines
v2: rewrite into a configure check (not sure if directly adding a -D to
QEMU_CFLAGSis the best, so advice welcome)Writing into config-host.mak would be preferable I think.
Okay, attempted in v4.
I can't type today. I just sent v3, not v4.
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
I lack easy access to a Mac box, so this is untested as to whether it actually solves the issue...
I ran a test with one of the cast workarounds locally removed, and this patch does allow the %zu version to build OK on OSX.
Thanks. But v3 leaves off Tested-by, because I still need to make sure my configure magic worked correctly :)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org