From 6b39b06339ee59559b31f860d4af635b046322df Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 11 Oct 2016 10:46:23 -0500 Subject: [PATCH] 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), just hard-code it with a cast. This is not a strict C99-compliant definition, because it doesn't work in the preprocessor, but if we later need that, the build will break on Mac to inform us to improve our replacement at that time. 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 Reviewed-by: Markus Armbruster Message-id: 1476200784-17210-1-git-send-email-eblake@redhat.com Reviewed-by: John Arbuckle Tested-by: Peter Maydell Signed-off-by: Peter Maydell --- configure | 16 ++++++++++++++++ include/qemu/osdep.h | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/configure b/configure index 5751d8ecaa..dd9e6792bb 100755 --- a/configure +++ b/configure @@ -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 +#include +int main(int argc, char *argv[]) { + return printf("%zu", SIZE_MAX); +} +EOF +have_broken_size_max=no +if ! compile_object -Werror ; then + have_broken_size_max=yes +fi + ########################################## # L2TPV3 probe @@ -5245,6 +5258,9 @@ fi if test "$have_ifaddrs_h" = "yes" ; then echo "HAVE_IFADDRS_H=y" >> $config_host_mak fi +if test "$have_broken_size_max" = "yes" ; then + echo "HAVE_BROKEN_SIZE_MAX=y" >> $config_host_mak +fi # Work around a system header bug with some kernel/XFS header # versions where they both try to define 'struct fsxattr': diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 384bfe245f..0e3c330e6b 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -141,6 +141,14 @@ extern int daemon(int, int); # error Unknown pointer size #endif +/* Mac OSX has a 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. */ +#if defined(HAVE_BROKEN_SIZE_MAX) && HAVE_BROKEN_SIZE_MAX +#undef SIZE_MAX +#define SIZE_MAX ((size_t)-1) +#endif + #ifndef MIN #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif -- GitLab