From fcd2e372a77ae5186e80aee20844621139fdf33c Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 3 Oct 2001 21:58:28 +0000 Subject: [PATCH] Consistently use gcc's __attribute__((format)) to check sprintf-style format strings wherever possible. Remarkably, this exercise didn't turn up any inconsistencies, but it seems a good idea for the future. --- src/backend/postmaster/postmaster.c | 10 +++------- src/bin/pg_dump/pg_backup.h | 9 ++++++--- src/bin/psql/common.h | 27 +++++++++++---------------- src/include/c.h | 8 ++++---- src/include/lib/stringinfo.h | 6 ++++-- src/include/postgres.h | 7 +++++-- src/interfaces/libpq/libpq-int.h | 7 ++----- src/interfaces/libpq/pqexpbuffer.h | 10 +++++++--- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 85197e957a..12cd5a1648 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -37,7 +37,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.244 2001/09/30 20:08:18 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.245 2001/10/03 21:58:28 tgl Exp $ * * NOTES * @@ -251,13 +251,9 @@ static void SignalChildren(int signal); static int CountChildren(void); static bool CreateOptsFile(int argc, char *argv[]); static pid_t SSDataBase(int xlop); -#ifdef __GNUC__ -/* This checks the format string for consistency. */ static void postmaster_error(const char *fmt, ...) - __attribute__((format(printf, 1, 2))); -#else -static void postmaster_error(const char *fmt, ...); -#endif +/* This lets gcc check the format string for consistency. */ +__attribute__((format(printf, 1, 2))); #define StartupDataBase() SSDataBase(BS_XLOG_STARTUP) #define CheckPointDataBase() SSDataBase(BS_XLOG_CHECKPOINT) diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index 282738d882..b06ff43973 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup.h,v 1.14 2001/08/22 20:23:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup.h,v 1.15 2001/10/03 21:58:28 tgl Exp $ * * Modifications - 28-Jun-2000 - pjw@rhyme.com.au * @@ -130,7 +130,8 @@ typedef struct _restoreOptions * Main archiver interface. */ -extern void exit_horribly(Archive *AH, const char *modulename, const char *fmt, ...) __attribute__((format(printf,3,4))); +extern void exit_horribly(Archive *AH, const char *modulename, const char *fmt, ...) +__attribute__((format(printf,3,4))); extern char * simple_prompt(const char *prompt, int maxlen, bool echo); @@ -188,6 +189,8 @@ extern void SortTocFromFile(Archive *AH, RestoreOptions *ropt); /* Convenience functions used only when writing DATA */ extern int archputs(const char *s, Archive *AH); extern int archputc(const char c, Archive *AH); -extern int archprintf(Archive *AH, const char *fmt,...); +extern int archprintf(Archive *AH, const char *fmt, ...) +/* This extension allows gcc to check the format string */ +__attribute__((format(printf, 2, 3))); #endif diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 04c91fdd66..f9604bf90d 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.13 2001/02/10 02:31:28 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.14 2001/10/03 21:58:28 tgl Exp $ */ #ifndef COMMON_H #define COMMON_H @@ -13,33 +13,28 @@ #include "pqsignal.h" #include "libpq-fe.h" -char *xstrdup(const char *string); +extern char *xstrdup(const char *string); -bool setQFout(const char *fname); +extern bool setQFout(const char *fname); -#ifndef __GNUC__ -void psql_error(const char *fmt,...); +extern void psql_error(const char *fmt, ...) +/* This lets gcc check the format string for consistency. */ +__attribute__((format(printf, 1, 2))); -#else -/* This checks the format string for consistency. */ -void psql_error(const char *fmt,...) __attribute__((format(printf, 1, 2))); +extern void NoticeProcessor(void *arg, const char *message); -#endif - -void NoticeProcessor(void *arg, const char *message); - -char *simple_prompt(const char *prompt, int maxlen, bool echo); +extern char *simple_prompt(const char *prompt, int maxlen, bool echo); extern volatile bool cancel_pressed; extern PGconn *cancelConn; #ifndef WIN32 -void handle_sigint(SIGNAL_ARGS); +extern void handle_sigint(SIGNAL_ARGS); #endif /* not WIN32 */ -PGresult *PSQLexec(const char *query); +extern PGresult *PSQLexec(const char *query); -bool SendQuery(const char *query); +extern bool SendQuery(const char *query); #endif /* COMMON_H */ diff --git a/src/include/c.h b/src/include/c.h index 37d360d941..372e548f8a 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -12,7 +12,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: c.h,v 1.101 2001/08/27 23:02:25 tgl Exp $ + * $Id: c.h,v 1.102 2001/10/03 21:58:28 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -634,13 +634,13 @@ typedef NameData *Name; */ #ifndef HAVE_SNPRINTF_DECL -extern int snprintf(char *str, size_t count, const char *fmt,...); - +extern int snprintf(char *str, size_t count, const char *fmt, ...) +/* This extension allows gcc to check the format string */ +__attribute__((format(printf, 3, 4))); #endif #ifndef HAVE_VSNPRINTF_DECL extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args); - #endif #if !defined(HAVE_MEMMOVE) && !defined(memmove) diff --git a/src/include/lib/stringinfo.h b/src/include/lib/stringinfo.h index 2ddff7053c..6f1406ff16 100644 --- a/src/include/lib/stringinfo.h +++ b/src/include/lib/stringinfo.h @@ -10,7 +10,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: stringinfo.h,v 1.19 2001/01/24 19:43:24 momjian Exp $ + * $Id: stringinfo.h,v 1.20 2001/10/03 21:58:28 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -81,7 +81,9 @@ extern void initStringInfo(StringInfo str); * to str if necessary. This is sort of like a combination of sprintf and * strcat. */ -extern void appendStringInfo(StringInfo str, const char *fmt,...); +extern void appendStringInfo(StringInfo str, const char *fmt, ...) +/* This extension allows gcc to check the format string */ +__attribute__((format(printf, 2, 3))); /*------------------------ * appendStringInfoChar diff --git a/src/include/postgres.h b/src/include/postgres.h index 964f1eaef1..3ae7bc44f8 100644 --- a/src/include/postgres.h +++ b/src/include/postgres.h @@ -10,7 +10,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1995, Regents of the University of California * - * $Id: postgres.h,v 1.52 2001/08/24 22:46:28 petere Exp $ + * $Id: postgres.h,v 1.53 2001/10/03 21:58:28 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -562,7 +562,10 @@ extern DLLIMPORT bool assert_enabled; extern int ExceptionalCondition(char *conditionName, Exception *exceptionP, char *details, char *fileName, int lineNumber); -extern char *vararg_format(const char *fmt,...); + +extern char *vararg_format(const char *fmt, ...) +/* This lets gcc check the format string for consistency. */ +__attribute__((format(printf, 1, 2))); #ifndef USE_ASSERT_CHECKING #define LogAssert(condition, printArgs) diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index 580ac0f069..6b088e94d7 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -12,7 +12,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-int.h,v 1.39 2001/08/17 02:59:20 momjian Exp $ + * $Id: libpq-int.h,v 1.40 2001/10/03 21:58:28 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -346,10 +346,7 @@ extern int pqWriteReady(PGconn *conn); #ifdef ENABLE_NLS extern char * libpq_gettext(const char *msgid) -#ifdef __GNUC__ -__attribute__((format_arg(1))) -#endif -; +__attribute__((format_arg(1))); #else #define libpq_gettext(x) (x) #endif diff --git a/src/interfaces/libpq/pqexpbuffer.h b/src/interfaces/libpq/pqexpbuffer.h index 7d5b07f730..0db0806595 100644 --- a/src/interfaces/libpq/pqexpbuffer.h +++ b/src/interfaces/libpq/pqexpbuffer.h @@ -18,7 +18,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: pqexpbuffer.h,v 1.6 2001/01/24 19:43:31 momjian Exp $ + * $Id: pqexpbuffer.h,v 1.7 2001/10/03 21:58:28 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -122,7 +122,9 @@ extern int enlargePQExpBuffer(PQExpBuffer str, size_t needed); * This is a convenience routine that does the same thing as * resetPQExpBuffer() followed by appendPQExpBuffer(). */ -extern void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...); +extern void printfPQExpBuffer(PQExpBuffer str, const char *fmt, ...) +/* This extension allows gcc to check the format string */ +__attribute__((format(printf, 2, 3))); /*------------------------ * appendPQExpBuffer @@ -131,7 +133,9 @@ extern void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...); * to str if necessary. This is sort of like a combination of sprintf and * strcat. */ -extern void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...); +extern void appendPQExpBuffer(PQExpBuffer str, const char *fmt, ...) +/* This extension allows gcc to check the format string */ +__attribute__((format(printf, 2, 3))); /*------------------------ * appendPQExpBufferStr