Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a60c8f

Browse files
committed
Distinguish printf-like functions that support %m from those that don't.
The elog/ereport family of functions certainly support the %m format spec,because they implement it "by hand". But elsewhere we have printf wrappersthat might or might not allow it depending on whether the platform's printfdoes. (Most non-glibc versions don't, and notably, src/port/snprintf.cdoesn't.) Hence, rather than using the gnu_printf format archetypeinterchangeably for all these functions, use it only for elog/ereport.This will allow us to get compiler warnings for mistakes like the onesfixed in commita13b47a, at least on platforms where printf doesn'ttake %m and gcc is correctly configured to know it. (Unfortunately,that won't happen on Linux, nor on macOS according to my testing.It remains to be seen what the buildfarm's gcc-on-Windows animals willthink of this, but we may well have to rely on less-popular platformsto warn us about unportable code of this kind.)Discussion:https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
1 parent5c047fd commit3a60c8f

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

‎config/c-compiler.m4

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ fi])# PGAC_C_SIGNED
1919

2020
# PGAC_C_PRINTF_ARCHETYPE
2121
# -----------------------
22-
# Set the format archetype used by gcc to checkprintf typefunctions. We
23-
#prefer "gnu_printf", which includes what glibc uses, such as %m for error
24-
#strings and %lld for 64 bit long longs. GCC 4.4 introduced it. It makes a
25-
#dramatic difference on Windows.
22+
# Set the format archetype used by gcc to checkelog/ereportfunctions.
23+
#This should accept %m, whether or not the platform's printf does.
24+
#We use "gnu_printf" if possible, which does that, although in some cases
25+
#it might do more than we could wish.
2626
AC_DEFUN([PGAC_PRINTF_ARCHETYPE],
27-
[AC_CACHE_CHECK([for printf format archetype],pgac_cv_printf_archetype,
27+
[AC_CACHE_CHECK([for printf format archetype for %m],pgac_cv_printf_archetype,
2828
[ac_save_c_werror_flag=$ac_c_werror_flag
2929
ac_c_werror_flag=yes
3030
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
@@ -34,8 +34,8 @@ __attribute__((format(gnu_printf, 2, 3)));], [])],
3434
[pgac_cv_printf_archetype=gnu_printf],
3535
[pgac_cv_printf_archetype=printf])
3636
ac_c_werror_flag=$ac_save_c_werror_flag])
37-
AC_DEFINE_UNQUOTED([PG_PRINTF_ATTRIBUTE],[$pgac_cv_printf_archetype],
38-
[Defineto gnu_printf if compiler supports it, else printf.])
37+
AC_DEFINE_UNQUOTED([PG_PRINTF_ATTRIBUTE_M],[$pgac_cv_printf_archetype],
38+
[Defineas a format archetype that accepts %m, if available, else printf.])
3939
])# PGAC_PRINTF_ARCHETYPE
4040

4141

‎configure

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13362,8 +13362,8 @@ _ACEOF
1336213362
;;
1336313363
esac
1336413364

13365-
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for printf format archetype" >&5
13366-
$as_echo_n "checking for printf format archetype... " >&6; }
13365+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for printf format archetype for %m" >&5
13366+
$as_echo_n "checking for printf format archetype for %m... " >&6; }
1336713367
if ${pgac_cv_printf_archetype+:} false; then :
1336813368
$as_echo_n "(cached) " >&6
1336913369
else
@@ -13394,7 +13394,7 @@ fi
1339413394
$as_echo "$pgac_cv_printf_archetype" >&6; }
1339513395

1339613396
cat >>confdefs.h <<_ACEOF
13397-
#definePG_PRINTF_ATTRIBUTE $pgac_cv_printf_archetype
13397+
#definePG_PRINTF_ATTRIBUTE_M $pgac_cv_printf_archetype
1339813398
_ACEOF
1339913399

1340013400

‎src/include/c.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,14 @@
126126
/* GCC and XLC support format attributes */
127127
#if defined(__GNUC__)|| defined(__IBMC__)
128128
#definepg_attribute_format_arg(a) __attribute__((format_arg(a)))
129-
#definepg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
129+
/* Use for functions wrapping stdio's printf, which often doesn't take %m: */
130+
#definepg_attribute_printf(f,a) __attribute__((format(printf, f, a)))
131+
/* Use for elog/ereport, which implement %m for themselves: */
132+
#definepg_attribute_printf_m(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE_M, f, a)))
130133
#else
131134
#definepg_attribute_format_arg(a)
132135
#definepg_attribute_printf(f,a)
136+
#definepg_attribute_printf_m(f,a)
133137
#endif
134138

135139
/* GCC, Sunpro and XLC support aligned, packed and noreturn */

‎src/include/pg_config.h.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,8 @@
809809
/* PostgreSQL major version as a string */
810810
#undef PG_MAJORVERSION
811811

812-
/* Defineto gnu_printf if compiler supports it, else printf. */
813-
#undefPG_PRINTF_ATTRIBUTE
812+
/* Defineas a format archetype that accepts %m, if available, else printf. */
813+
#undefPG_PRINTF_ATTRIBUTE_M
814814

815815
/* PostgreSQL version as a string */
816816
#undef PG_VERSION

‎src/include/utils/elog.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,25 @@ extern interrcode(int sqlerrcode);
133133
externinterrcode_for_file_access(void);
134134
externinterrcode_for_socket_access(void);
135135

136-
externinterrmsg(constchar*fmt,...)pg_attribute_printf(1,2);
137-
externinterrmsg_internal(constchar*fmt,...)pg_attribute_printf(1,2);
136+
externinterrmsg(constchar*fmt,...)pg_attribute_printf_m(1,2);
137+
externinterrmsg_internal(constchar*fmt,...)pg_attribute_printf_m(1,2);
138138

139139
externinterrmsg_plural(constchar*fmt_singular,constchar*fmt_plural,
140-
unsigned longn,...)pg_attribute_printf(1,4)pg_attribute_printf(2,4);
140+
unsigned longn,...)pg_attribute_printf_m(1,4)pg_attribute_printf_m(2,4);
141141

142-
externinterrdetail(constchar*fmt,...)pg_attribute_printf(1,2);
143-
externinterrdetail_internal(constchar*fmt,...)pg_attribute_printf(1,2);
142+
externinterrdetail(constchar*fmt,...)pg_attribute_printf_m(1,2);
143+
externinterrdetail_internal(constchar*fmt,...)pg_attribute_printf_m(1,2);
144144

145-
externinterrdetail_log(constchar*fmt,...)pg_attribute_printf(1,2);
145+
externinterrdetail_log(constchar*fmt,...)pg_attribute_printf_m(1,2);
146146

147147
externinterrdetail_log_plural(constchar*fmt_singular,
148148
constchar*fmt_plural,
149-
unsigned longn,...)pg_attribute_printf(1,4)pg_attribute_printf(2,4);
149+
unsigned longn,...)pg_attribute_printf_m(1,4)pg_attribute_printf_m(2,4);
150150

151151
externinterrdetail_plural(constchar*fmt_singular,constchar*fmt_plural,
152-
unsigned longn,...)pg_attribute_printf(1,4)pg_attribute_printf(2,4);
152+
unsigned longn,...)pg_attribute_printf_m(1,4)pg_attribute_printf_m(2,4);
153153

154-
externinterrhint(constchar*fmt,...)pg_attribute_printf(1,2);
154+
externinterrhint(constchar*fmt,...)pg_attribute_printf_m(1,2);
155155

156156
/*
157157
* errcontext() is typically called in error context callback functions, not
@@ -165,7 +165,7 @@ extern interrhint(const char *fmt,...) pg_attribute_printf(1, 2);
165165

166166
externintset_errcontext_domain(constchar*domain);
167167

168-
externinterrcontext_msg(constchar*fmt,...)pg_attribute_printf(1,2);
168+
externinterrcontext_msg(constchar*fmt,...)pg_attribute_printf_m(1,2);
169169

170170
externinterrhidestmt(boolhide_stmt);
171171
externinterrhidecontext(boolhide_ctx);
@@ -222,13 +222,13 @@ extern intgetinternalerrposition(void);
222222
#endif/* HAVE__VA_ARGS */
223223

224224
externvoidelog_start(constchar*filename,intlineno,constchar*funcname);
225-
externvoidelog_finish(intelevel,constchar*fmt,...)pg_attribute_printf(2,3);
225+
externvoidelog_finish(intelevel,constchar*fmt,...)pg_attribute_printf_m(2,3);
226226

227227

228228
/* Support for constructing error strings separately from ereport() calls */
229229

230230
externvoidpre_format_elog_string(interrnumber,constchar*domain);
231-
externchar*format_elog_string(constchar*fmt,...)pg_attribute_printf(1,2);
231+
externchar*format_elog_string(constchar*fmt,...)pg_attribute_printf_m(1,2);
232232

233233

234234
/* Support for attaching context information to error reports */
@@ -407,9 +407,9 @@ extern void set_syslog_parameters(const char *ident, int facility);
407407
#endif
408408

409409
/*
410-
* Write errors to stderr (or byequal means when stderr is
411-
*notavailable). Used before ereport/elog can be used
412-
*safely (memory context,GUC load etc)
410+
* Write errors to stderr (or bycomparable means when stderr is not
411+
* available).Used before ereport/elog can be used safely (memory context,
412+
* GUC load etc). Note that this does *not* accept "%m".
413413
*/
414414
externvoidwrite_stderr(constchar*fmt,...)pg_attribute_printf(1,2);
415415

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp