Skip to content

Commit 96bf88d

Browse files
committed
Always use our own versions of *printf().
We've spent an awful lot of effort over the years in coping with platform-specific vagaries of the *printf family of functions. Let's just forget all that mess and standardize on always using src/port/snprintf.c. This gets rid of a lot of configure logic, and it will allow a saner approach to dealing with %m (though actually changing that is left for a follow-on patch). Preliminary performance testing suggests that as it stands, snprintf.c is faster than the native printf functions for some tasks on some platforms, and slower for other cases. A pending patch will improve that, though cases with floating-point conversions will doubtless remain slower unless we want to put a *lot* of effort into that. Still, we've not observed that *printf is really a performance bottleneck for most workloads, so I doubt this matters much. Patch by me, reviewed by Michael Paquier Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
1 parent 758ce9b commit 96bf88d

File tree

16 files changed

+19
-509
lines changed

16 files changed

+19
-509
lines changed

config/c-compiler.m4

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,8 @@ fi])# PGAC_C_SIGNED
2020
# PGAC_C_PRINTF_ARCHETYPE
2121
# -----------------------
2222
# Select the format archetype to be used by gcc to check printf-type functions.
23-
# We prefer "gnu_printf", which matches the features glibc supports, notably
24-
# %m, 'z' and 'll' width modifiers ('ll' only matters if int64 requires it),
25-
# and argument order control if we're doing --enable-nls. On platforms where
26-
# the native printf doesn't have 'z'/'ll' or arg control, we replace it with
27-
# src/port/snprintf.c which does, so that the only potential mismatch here is
28-
# whether or not %m is supported. We need that for elog/ereport, so we live
29-
# with the fact that erroneous use of %m in plain printf calls won't be
30-
# detected. (It appears that many versions of gcc/clang wouldn't report it
31-
# even if told to check according to plain printf archetype, anyway.)
23+
# We prefer "gnu_printf", as that most closely matches the features supported
24+
# by src/port/snprintf.c (particularly the %m conversion spec).
3225
AC_DEFUN([PGAC_PRINTF_ARCHETYPE],
3326
[AC_CACHE_CHECK([for printf format archetype], pgac_cv_printf_archetype,
3427
[ac_save_c_werror_flag=$ac_c_werror_flag

config/c-library.m4

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -171,106 +171,6 @@ AC_DEFUN([PGAC_STRUCT_ADDRINFO],
171171
])])# PGAC_STRUCT_ADDRINFO
172172

173173

174-
# PGAC_FUNC_SNPRINTF_ARG_CONTROL
175-
# ---------------------------------------
176-
# Determine if snprintf supports %1$ argument selection, e.g. %5$ selects
177-
# the fifth argument after the printf format string.
178-
# This is not in the C99 standard, but in the Single Unix Specification (SUS).
179-
# It is used in our language translation strings.
180-
#
181-
AC_DEFUN([PGAC_FUNC_SNPRINTF_ARG_CONTROL],
182-
[AC_MSG_CHECKING([whether snprintf supports argument control])
183-
AC_CACHE_VAL(pgac_cv_snprintf_arg_control,
184-
[AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <stdio.h>
185-
#include <string.h>
186-
187-
int main()
188-
{
189-
char buf[100];
190-
191-
/* can it swap arguments? */
192-
snprintf(buf, 100, "%2\$d %1\$d", 3, 4);
193-
if (strcmp(buf, "4 3") != 0)
194-
return 1;
195-
return 0;
196-
}]])],
197-
[pgac_cv_snprintf_arg_control=yes],
198-
[pgac_cv_snprintf_arg_control=no],
199-
[pgac_cv_snprintf_arg_control=cross])
200-
])dnl AC_CACHE_VAL
201-
AC_MSG_RESULT([$pgac_cv_snprintf_arg_control])
202-
])# PGAC_FUNC_SNPRINTF_ARG_CONTROL
203-
204-
# PGAC_FUNC_SNPRINTF_SIZE_T_SUPPORT
205-
# ---------------------------------
206-
# Determine if snprintf supports the z length modifier for printing
207-
# size_t-sized variables. That's supported by C99 and POSIX but not
208-
# all platforms play ball, so we must test whether it's working.
209-
#
210-
AC_DEFUN([PGAC_FUNC_SNPRINTF_SIZE_T_SUPPORT],
211-
[AC_MSG_CHECKING([whether snprintf supports the %z modifier])
212-
AC_CACHE_VAL(pgac_cv_snprintf_size_t_support,
213-
[AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <stdio.h>
214-
#include <string.h>
215-
216-
int main()
217-
{
218-
char bufz[100];
219-
char buf64[100];
220-
221-
/*
222-
* Print the largest unsigned number fitting in a size_t using both %zu
223-
* and the previously-determined format for 64-bit integers. Note that
224-
* we don't run this code unless we know snprintf handles 64-bit ints.
225-
*/
226-
bufz[0] = '\0'; /* in case snprintf fails to emit anything */
227-
snprintf(bufz, sizeof(bufz), "%zu", ~((size_t) 0));
228-
snprintf(buf64, sizeof(buf64), "%" INT64_MODIFIER "u",
229-
(unsigned PG_INT64_TYPE) ~((size_t) 0));
230-
if (strcmp(bufz, buf64) != 0)
231-
return 1;
232-
return 0;
233-
}]])],
234-
[pgac_cv_snprintf_size_t_support=yes],
235-
[pgac_cv_snprintf_size_t_support=no],
236-
[pgac_cv_snprintf_size_t_support=cross])
237-
])dnl AC_CACHE_VAL
238-
AC_MSG_RESULT([$pgac_cv_snprintf_size_t_support])
239-
])# PGAC_FUNC_SNPRINTF_SIZE_T_SUPPORT
240-
241-
# PGAC_FUNC_SNPRINTF_C99_RESULT
242-
# -----------------------------
243-
# Determine whether snprintf returns the desired buffer length when
244-
# it overruns the actual buffer length. That's required by C99 and POSIX
245-
# but ancient platforms don't behave that way, so we must test.
246-
# While we're at it, let's just verify that it doesn't physically overrun
247-
# the buffer.
248-
#
249-
AC_DEFUN([PGAC_FUNC_SNPRINTF_C99_RESULT],
250-
[AC_MSG_CHECKING([whether snprintf handles buffer overrun per C99])
251-
AC_CACHE_VAL(pgac_cv_snprintf_c99_result,
252-
[AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <stdio.h>
253-
#include <string.h>
254-
255-
int main()
256-
{
257-
char buf[10];
258-
259-
strcpy(buf, "abcdefghi");
260-
if (snprintf(buf, 4, "%d", 123456) != 6)
261-
return 1;
262-
if (strcmp(buf, "123") != 0 || buf[4] != 'e')
263-
return 1;
264-
return 0;
265-
}]])],
266-
[pgac_cv_snprintf_c99_result=yes],
267-
[pgac_cv_snprintf_c99_result=no],
268-
[pgac_cv_snprintf_c99_result=cross])
269-
])dnl AC_CACHE_VAL
270-
AC_MSG_RESULT([$pgac_cv_snprintf_c99_result])
271-
])# PGAC_FUNC_SNPRINTF_C99_RESULT
272-
273-
274174
# PGAC_TYPE_LOCALE_T
275175
# ------------------
276176
# Check for the locale_t type and find the right header file. macOS

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy