[gjs/wip/ptomato/warnings: 7/15] build: Test for %Id as alternate integer output
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/warnings: 7/15] build: Test for %Id as alternate integer output
- Date: Tue, 18 Oct 2016 20:09:10 +0000 (UTC)
commit 05d05758e51790a26d76932b43728fa298112dae
Author: Philip Chimento <philip chimento gmail com>
Date: Sun Oct 2 12:24:32 2016 -0700
build: Test for %Id as alternate integer output
%Id is a glibc extension. The "I" character is used elsewhere in other
extensions too. For example, %I is a Microsoft extension for printing
size_t. Although undocumented on Darwin, that platform's libc emulates
the Microsoft extension.
If %I is interpreted as size_t, then the code is likely to crash on
platforms where int and size_t are different widths, such as 64-bit OSX.
This adds a configure-time check for %Id. When it's not supported, then
the provided sample program will error out when compiled with -Werror. If
not supported then we simply print %d since I don't believe the
alternative representation flag has any other equivalent on other
platforms.
configure.ac | 13 +++++++++++++
libgjs-private/gjs-util.cpp | 4 ++++
2 files changed, 17 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index be70e53..0cb457f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -27,6 +27,7 @@ AM_SILENT_RULES([yes])
PKG_PROG_PKG_CONFIG
PKG_INSTALLDIR
+AC_LANG([C++])
AC_PROG_CXX
AX_CXX_COMPILE_STDCXX_11
@@ -135,6 +136,18 @@ LIBS=$LIBS_no_readline
AC_CHECK_FUNCS([backtrace])
+AC_MSG_CHECKING([whether printf() accepts '%Id' for alternative integer output])
+CXXFLAGS_save=$CXXFLAGS
+CXXFLAGS="-Werror -Wformat -pedantic-errors"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]],
+ [[printf("%Id", (int)0);]])], [
+ AC_MSG_RESULT([yes])
+ AC_DEFINE([HAVE_PRINTF_ALTERNATIVE_INT], [1],
+ [Define to 1 if printf() accepts '%Id' for alternative integer output])
+ ],
+ [AC_MSG_RESULT([no])])
+CXXFLAGS=$CXXFLAGS_save
+
AC_ARG_ENABLE(installed_tests,
[AS_HELP_STRING([--enable-installed-tests],
[Install test programs @<:@default: no@:>@])])
diff --git a/libgjs-private/gjs-util.cpp b/libgjs-private/gjs-util.cpp
index 961654f..f0e941f 100644
--- a/libgjs-private/gjs-util.cpp
+++ b/libgjs-private/gjs-util.cpp
@@ -31,7 +31,11 @@
char *
gjs_format_int_alternative_output(int n)
{
+#ifdef HAVE_PRINTF_ALTERNATIVE_INT
return g_strdup_printf("%Id", n);
+#else
+ return g_strdup_printf("%d", n);
+#endif
}
GType
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]