mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Allow use of "z" flag in our printf calls, and use it where appropriate.
Since C99, it's been standard for printf and friends to accept a "z" size modifier, meaning "whatever size size_t has". Up to now we've generally dealt with printing size_t values by explicitly casting them to unsigned long and using the "l" modifier; but this is really the wrong thing on platforms where pointers are wider than longs (such as Win64). So let's start using "z" instead. To ensure we can do that on all platforms, teach src/port/snprintf.c to understand "z", and add a configure test to force use of that implementation when the platform's version doesn't handle "z". Having done that, modify a bunch of places that were using the unsigned-long hack to use "z" instead. This patch doesn't pretend to have gotten everyplace that could benefit, but it catches many of them. I made an effort in particular to ensure that all uses of the same error message text were updated together, so as not to increase the number of translatable strings. It's possible that this change will result in format-string warnings from pre-C99 compilers. We might have to reconsider if there are any popular compilers that will warn about this; but let's start by seeing what the buildfarm thinks. Andres Freund, with a little additional work by me
This commit is contained in:
70
configure
vendored
70
configure
vendored
@ -12698,13 +12698,13 @@ fi
|
||||
# Force use of our snprintf if system's doesn't do arg control
|
||||
# See comment above at snprintf test for details.
|
||||
if test "$enable_nls" = yes -a "$pgac_need_repl_snprintf" = no; then
|
||||
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether printf supports argument control" >&5
|
||||
$as_echo_n "checking whether printf supports argument control... " >&6; }
|
||||
if ${pgac_cv_printf_arg_control+:} false; then :
|
||||
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether snprintf supports argument control" >&5
|
||||
$as_echo_n "checking whether snprintf supports argument control... " >&6; }
|
||||
if ${pgac_cv_snprintf_arg_control+:} false; then :
|
||||
$as_echo_n "(cached) " >&6
|
||||
else
|
||||
if test "$cross_compiling" = yes; then :
|
||||
pgac_cv_printf_arg_control=cross
|
||||
pgac_cv_snprintf_arg_control=cross
|
||||
else
|
||||
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
|
||||
/* end confdefs.h. */
|
||||
@ -12723,9 +12723,9 @@ int main()
|
||||
}
|
||||
_ACEOF
|
||||
if ac_fn_c_try_run "$LINENO"; then :
|
||||
pgac_cv_printf_arg_control=yes
|
||||
pgac_cv_snprintf_arg_control=yes
|
||||
else
|
||||
pgac_cv_printf_arg_control=no
|
||||
pgac_cv_snprintf_arg_control=no
|
||||
fi
|
||||
rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
|
||||
conftest.$ac_objext conftest.beam conftest.$ac_ext
|
||||
@ -12733,10 +12733,10 @@ fi
|
||||
|
||||
|
||||
fi
|
||||
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_printf_arg_control" >&5
|
||||
$as_echo "$pgac_cv_printf_arg_control" >&6; }
|
||||
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_snprintf_arg_control" >&5
|
||||
$as_echo "$pgac_cv_snprintf_arg_control" >&6; }
|
||||
|
||||
if test $pgac_cv_printf_arg_control != yes ; then
|
||||
if test $pgac_cv_snprintf_arg_control != yes ; then
|
||||
pgac_need_repl_snprintf=yes
|
||||
fi
|
||||
fi
|
||||
@ -13036,6 +13036,58 @@ cat >>confdefs.h <<_ACEOF
|
||||
_ACEOF
|
||||
|
||||
|
||||
# Also force use of our snprintf if the system's doesn't support the %z flag.
|
||||
if test "$pgac_need_repl_snprintf" = no; then
|
||||
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether snprintf supports the %z modifier" >&5
|
||||
$as_echo_n "checking whether snprintf supports the %z modifier... " >&6; }
|
||||
if ${pgac_cv_snprintf_size_t_support+:} false; then :
|
||||
$as_echo_n "(cached) " >&6
|
||||
else
|
||||
if test "$cross_compiling" = yes; then :
|
||||
pgac_cv_snprintf_size_t_support=cross
|
||||
else
|
||||
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
|
||||
/* end confdefs.h. */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char bufz[100];
|
||||
char buf64[100];
|
||||
|
||||
/*
|
||||
* Print the largest unsigned number fitting in a size_t using both %zu
|
||||
* and the previously-determined format for 64-bit integers. Note that
|
||||
* we don't run this code unless we know snprintf handles 64-bit ints.
|
||||
*/
|
||||
bufz[0] = '\0'; /* in case snprintf fails to emit anything */
|
||||
snprintf(bufz, sizeof(bufz), "%zu", ~((size_t) 0));
|
||||
snprintf(buf64, sizeof(buf64), UINT64_FORMAT, (PG_INT64_TYPE) ~((size_t) 0));
|
||||
if (strcmp(bufz, buf64) != 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
_ACEOF
|
||||
if ac_fn_c_try_run "$LINENO"; then :
|
||||
pgac_cv_snprintf_size_t_support=yes
|
||||
else
|
||||
pgac_cv_snprintf_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_snprintf_size_t_support" >&5
|
||||
$as_echo "$pgac_cv_snprintf_size_t_support" >&6; }
|
||||
|
||||
if test "$pgac_cv_snprintf_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
|
||||
|
||||
|
Reference in New Issue
Block a user