>From 5c91e08cb2c4b3cc8779ed0cd89387eb38ea3690 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 23 Jan 2014 15:45:36 +0100
Subject: [PATCH 1/2] Add support for the %z modifier in error messages and
 other usages of printf.

The %z modifier allows to print size_t/Size variables without platform
dependent modifiers like UINT64_FORMAT and is included in C99 and
posix. As it's not present in all supported platforms add a configure
check which tests whether it's supported and actually working to some
degree and fall back to our existing snprintf.c fallback which now
supports the modifier if not.
---
 config/c-library.m4 | 38 ++++++++++++++++++++++++++++++++++++++
 configure           | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure.in        |  8 ++++++++
 src/port/snprintf.c | 26 ++++++++++++++++++++++++++
 4 files changed, 124 insertions(+)

diff --git a/config/c-library.m4 b/config/c-library.m4
index 1e3997b..171d39c 100644
--- a/config/c-library.m4
+++ b/config/c-library.m4
@@ -303,6 +303,44 @@ int main()
 AC_MSG_RESULT([$pgac_cv_printf_arg_control])
 ])# PGAC_FUNC_PRINTF_ARG_CONTROL
 
+# PGAC_FUNC_PRINTF_SIZE_T_SUPPORT
+# ---------------------------------------
+# Determine if printf supports the z length modifier for printing
+# sizeof(size_t) sized variables. That's supported by C99 and posix but not
+# all platforms play ball, so we test whether it's working.
+# Useful for printing sizes in error messages et al. without up/downcasting
+# arguments.
+#
+AC_DEFUN([PGAC_FUNC_PRINTF_SIZE_T_SUPPORT],
+[AC_MSG_CHECKING([whether printf supports the %z modifier])
+AC_CACHE_VAL(pgac_cv_printf_size_t_support,
+[AC_TRY_RUN([#include <stdio.h>
+#include <string.h>
+
+int main()
+{
+  char buf64[100];
+  char bufz[100];
+
+  /*
+   * Check whether we print correctly using %z by printing the biggest
+   * unsigned number fitting in a size_t and using both %zu and the format for
+   * 64bit numbers.
+   */
+
+  snprintf(bufz, 100, "%zu, ~(size_t)0);
+  snprintf(buf64, 100, UINT64_FORMAT, ~(size_t)0);
+  if (strcmp(bufz, buf64) != 0)
+    return 1;
+  return 0;
+}],
+[pgac_cv_printf_size_t_support=yes],
+[pgac_cv_printf_size_t_support=no],
+[pgac_cv_printf_size_t_support=cross])
+])dnl AC_CACHE_VAL
+AC_MSG_RESULT([$pgac_cv_printf_size_t_support])
+])# PGAC_FUNC_PRINTF_SIZE_T_SUPPORT
+
 
 # PGAC_TYPE_LOCALE_T
 # ------------------
diff --git a/configure b/configure
index e1ff704..d786b17 100755
--- a/configure
+++ b/configure
@@ -13036,6 +13036,58 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+# Also force our snprintf if the system's doesn't support the %z modifier.
+if test "$pgac_need_repl_snprintf" = no; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether printf supports the %z modifier" >&5
+$as_echo_n "checking whether printf supports the %z modifier... " >&6; }
+if ${pgac_cv_printf_size_t_support+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test "$cross_compiling" = yes; then :
+  pgac_cv_printf_size_t_support=cross
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <stdio.h>
+#include <string.h>
+
+int main()
+{
+  char buf64[100];
+  char bufz[100];
+
+  /*
+   * Check whether we print correctly using %z by printing the biggest
+   * unsigned number fitting in a size_t and using both %zu and the format for
+   * 64bit numbers.
+   */
+
+  snprintf(bufz, 100, "%zu, ~(size_t)0);
+  snprintf(buf64, 100, UINT64_FORMAT, ~(size_t)0);
+  if (strcmp(bufz, buf64) != 0)
+    return 1;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_run "$LINENO"; then :
+  pgac_cv_printf_size_t_support=yes
+else
+  pgac_cv_printf_size_t_support=no
+fi
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
+  conftest.$ac_objext conftest.beam conftest.$ac_ext
+fi
+
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_printf_size_t_support" >&5
+$as_echo "$pgac_cv_printf_size_t_support" >&6; }
+
+  if test $pgac_cv_printf_size_t_support != yes ; then
+    pgac_need_repl_snprintf=yes
+  fi
+fi
+
 # Now we have checked all the reasons to replace snprintf
 if test $pgac_need_repl_snprintf = yes; then
 
diff --git a/configure.in b/configure.in
index 3826237..e6cfac1 100644
--- a/configure.in
+++ b/configure.in
@@ -1617,6 +1617,14 @@ AC_DEFINE_UNQUOTED(INT64_FORMAT, $INT64_FORMAT,
 AC_DEFINE_UNQUOTED(UINT64_FORMAT, $UINT64_FORMAT,
                    [Define to the appropriate snprintf format for unsigned 64-bit ints.])
 
+# Also force our snprintf if the system's doesn't support the %z modifier.
+if test "$pgac_need_repl_snprintf" = no; then
+  PGAC_FUNC_PRINTF_SIZE_T_SUPPORT
+  if test $pgac_cv_printf_size_t_support != yes ; then
+    pgac_need_repl_snprintf=yes
+  fi
+fi
+
 # Now we have checked all the reasons to replace snprintf
 if test $pgac_need_repl_snprintf = yes; then
   AC_DEFINE(USE_REPL_SNPRINTF, 1, [Use replacement snprintf() functions.])
diff --git a/src/port/snprintf.c b/src/port/snprintf.c
index 9f71950..dc12399 100644
--- a/src/port/snprintf.c
+++ b/src/port/snprintf.c
@@ -386,6 +386,19 @@ nextch1:
 				else
 					longflag = 1;
 				goto nextch1;
+			case 'z':
+#if SIZEOF_SIZE_T == 8
+#	ifdef HAVE_LONG_INT_64
+				longflag = 1;
+#	elif defined(HAVE_LONG_LONG_INT_64)
+				longlongflag = 1;
+#	else
+#		error "Don't know how to print 64bit integers"
+#	endif
+#else
+				/* standard length is ok */
+#endif
+				goto nextch1;
 			case 'h':
 			case '\'':
 				/* ignore these */
@@ -619,6 +632,19 @@ nextch2:
 				else
 					longflag = 1;
 				goto nextch2;
+			case 'z':
+#if SIZEOF_SIZE_T == 8
+#	ifdef HAVE_LONG_INT_64
+				longflag = 1;
+#	elif defined(HAVE_LONG_LONG_INT_64)
+				longlongflag = 1;
+#	else
+#		error "Don't know how to print 64bit integers"
+#	endif
+#else
+				/* standard length is ok */
+#endif
+				goto nextch2;
 			case 'h':
 			case '\'':
 				/* ignore these */
-- 
1.8.5.rc2.dirty

