From b5c838e170e8fd138baf6938db472e448b200fed Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed, 3 Jun 2009 20:24:51 +0000
Subject: [PATCH] Clean up ecpg's use of mmerror(): const-ify the format
 argument, add an __attribute__() marker so that gcc can validate the format
 string against the actual arguments, get rid of overcomplicated and unsafe
 usage in base_yyerror().

---
 src/interfaces/ecpg/preproc/ecpg.header  |  4 ++--
 src/interfaces/ecpg/preproc/ecpg.trailer | 11 ++++-------
 src/interfaces/ecpg/preproc/extern.h     |  6 ++++--
 3 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/interfaces/ecpg/preproc/ecpg.header b/src/interfaces/ecpg/preproc/ecpg.header
index 9d90b662182..faaf607c610 100644
--- a/src/interfaces/ecpg/preproc/ecpg.header
+++ b/src/interfaces/ecpg/preproc/ecpg.header
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.header,v 1.5 2009/01/23 12:43:32 petere Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.header,v 1.6 2009/06/03 20:24:51 tgl Exp $ */
 
 /* Copyright comment */
 %{
@@ -56,7 +56,7 @@ struct ECPGtype ecpg_query = {ECPGt_char_variable, NULL, NULL, {NULL}, 0};
  * Handle parsing errors and warnings
  */
 void
-mmerror(int error_code, enum errortype type, char * error, ...)
+mmerror(int error_code, enum errortype type, const char *error, ...)
 {
 	va_list ap;
 
diff --git a/src/interfaces/ecpg/preproc/ecpg.trailer b/src/interfaces/ecpg/preproc/ecpg.trailer
index 93cc43d176a..718e74a9b30 100644
--- a/src/interfaces/ecpg/preproc/ecpg.trailer
+++ b/src/interfaces/ecpg/preproc/ecpg.trailer
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.trailer,v 1.7 2009/04/06 08:42:53 heikki Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.trailer,v 1.8 2009/06/03 20:24:51 tgl Exp $ */
 
 statements: /*EMPTY*/
                 | statements statement
@@ -1985,13 +1985,10 @@ ecpg_into: INTO into_list	{ $$ = EMPTY; }
 
 %%
 
-void base_yyerror(const char * error)
+void base_yyerror(const char *error)
 {
-	char buf[1024];
-
-	snprintf(buf,sizeof buf, _("%s at or near \"%s\""), error, token_start ? token_start : yytext);
-	buf[sizeof(buf)-1]=0;
-	mmerror(PARSE_ERROR, ET_ERROR, buf);
+	mmerror(PARSE_ERROR, ET_ERROR, "%s at or near \"%s\"",
+			error, token_start ? token_start : yytext);
 }
 
 void parser_init(void)
diff --git a/src/interfaces/ecpg/preproc/extern.h b/src/interfaces/ecpg/preproc/extern.h
index 7f55441da34..e5e1e412180 100644
--- a/src/interfaces/ecpg/preproc/extern.h
+++ b/src/interfaces/ecpg/preproc/extern.h
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/extern.h,v 1.71 2008/05/20 23:17:32 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/extern.h,v 1.72 2009/06/03 20:24:51 tgl Exp $ */
 
 #ifndef _ECPG_PREPROC_EXTERN_H
 #define _ECPG_PREPROC_EXTERN_H
@@ -74,7 +74,9 @@ extern int	base_yylex(void);
 extern void base_yyerror(const char *);
 extern void *mm_alloc(size_t), *mm_realloc(void *, size_t);
 extern char *mm_strdup(const char *);
-extern void mmerror(int, enum errortype, char *,...);
+extern void mmerror(int, enum errortype, const char *, ...)
+/* This extension allows gcc to check the format string */
+__attribute__((format(printf, 3, 4)));
 extern void output_get_descr_header(char *);
 extern void output_get_descr(char *, char *);
 extern void output_set_descr_header(char *);
-- 
GitLab