changeset 1716:ef5d02d0d37f draft

let the compiler check format strings i'll be AFK for a week, so here's the patch i've been using this evening to find other format string mistakes. BSD uses __printflike and takes two arguments instead of hard-coding (1,2), but i figured that as long as you don't need the generality you'd prefer not to have it. and it's easy enough to retrofit if we ever do have a formatting function that takes other arguments.
author Elliott Hughes <enh@google.com>
date Sun, 01 Mar 2015 16:16:50 -0600
parents a471f338b055
children 2f2c7ae058d7
files lib/lib.h lib/portability.h
diffstat 2 files changed, 8 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.h	Sun Mar 01 16:11:50 2015 -0600
+++ b/lib/lib.h	Sun Mar 01 16:16:50 2015 -0600
@@ -88,8 +88,8 @@
 void *xrealloc(void *ptr, size_t size);
 char *xstrndup(char *s, size_t n);
 char *xstrdup(char *s);
-char *xmprintf(char *format, ...);
-void xprintf(char *format, ...);
+char *xmprintf(char *format, ...) printf_format;
+void xprintf(char *format, ...) printf_format;
 void xputs(char *s);
 void xputc(char c);
 void xflush(void);
@@ -132,10 +132,10 @@
 
 // lib.c
 void verror_msg(char *msg, int err, va_list va);
-void error_msg(char *msg, ...);
-void perror_msg(char *msg, ...);
-void error_exit(char *msg, ...) noreturn;
-void perror_exit(char *msg, ...) noreturn;
+void error_msg(char *msg, ...) printf_format;
+void perror_msg(char *msg, ...) printf_format;
+void error_exit(char *msg, ...) printf_format noreturn;
+void perror_exit(char *msg, ...) printf_format noreturn;
 ssize_t readall(int fd, void *buf, size_t len);
 ssize_t writeall(int fd, void *buf, size_t len);
 off_t lskip(int fd, off_t offset);
--- a/lib/portability.h	Sun Mar 01 16:11:50 2015 -0600
+++ b/lib/portability.h	Sun Mar 01 16:16:50 2015 -0600
@@ -11,8 +11,10 @@
 
 #ifdef __GNUC__
 #define noreturn	__attribute__((noreturn))
+#define printf_format	__attribute__((format(printf, 1, 2)))
 #else
 #define noreturn
+#define printf_format
 #endif
 
 // Always use long file support.