1From e6683d001a95d7c3d4d992496f00f77e01fcd268 Mon Sep 17 00:00:00 2001 2From: Hauke Mehrtens <hauke@hauke-m.de> 3Date: Sun, 22 Nov 2015 15:04:23 +0100 4Subject: [PATCH v2] Add format attribute to some function declarations 5 6GCC and Clang are able to check the format arguments given to a 7function and warn the user if there is a error in the format arguments 8or if there is a potential uncontrolled format string security problem 9in the code. GCC does this automatically for some functions like 10printf(), but it is also possible to annotate other functions in a way 11that it will check them too. This feature is used by glibc for many 12functions. This patch adds the attribute to the some functions of musl 13expect for these functions where gcc automatically adds it. 14 15GCC automatically adds checks for these functions: printf, fprintf, 16sprintf, scanf, fscanf, sscanf, strftime, vprintf, vfprintf and 17vsprintf. 18 19The documentation from gcc is here: 20https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html 21 22The documentation from Clang is here: 23http://clang.llvm.org/docs/AttributeReference.html#format-gnu-format 24 25Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de> 26--- 27 include/err.h | 26 +++++++++++++++++--------- 28 include/monetary.h | 12 ++++++++++-- 29 include/stdio.h | 29 ++++++++++++++++++++--------- 30 include/syslog.h | 12 ++++++++++-- 31 4 files changed, 57 insertions(+), 22 deletions(-) 32 33--- a/include/err.h 34+++ b/include/err.h 35@@ -8,15 +8,23 @@ 36 extern "C" { 37 #endif 38 39-void warn(const char *, ...); 40-void vwarn(const char *, va_list); 41-void warnx(const char *, ...); 42-void vwarnx(const char *, va_list); 43+#if __GNUC__ >= 3 44+#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y))) 45+#else 46+#define __fp(x, y) 47+#endif 48+ 49+void warn(const char *, ...) __fp(1, 2); 50+void vwarn(const char *, va_list) __fp(1, 0); 51+void warnx(const char *, ...) __fp(1, 2); 52+void vwarnx(const char *, va_list) __fp(1, 0); 53+ 54+_Noreturn void err(int, const char *, ...) __fp(2, 3); 55+_Noreturn void verr(int, const char *, va_list) __fp(2, 0); 56+_Noreturn void errx(int, const char *, ...) __fp(2, 3); 57+_Noreturn void verrx(int, const char *, va_list) __fp(2, 0); 58 59-_Noreturn void err(int, const char *, ...); 60-_Noreturn void verr(int, const char *, va_list); 61-_Noreturn void errx(int, const char *, ...); 62-_Noreturn void verrx(int, const char *, va_list); 63+#undef __fp 64 65 #ifdef __cplusplus 66 } 67--- a/include/monetary.h 68+++ b/include/monetary.h 69@@ -13,8 +13,16 @@ extern "C" { 70 71 #include <bits/alltypes.h> 72 73-ssize_t strfmon(char *__restrict, size_t, const char *__restrict, ...); 74-ssize_t strfmon_l(char *__restrict, size_t, locale_t, const char *__restrict, ...); 75+#if __GNUC__ >= 3 76+#define __fsfm(x, y) __attribute__ ((__format__ (__strfmon__, x, y))) 77+#else 78+#define __fsfm(x, y) 79+#endif 80+ 81+ssize_t strfmon(char *__restrict, size_t, const char *__restrict, ...) __fsfm(3, 4); 82+ssize_t strfmon_l(char *__restrict, size_t, locale_t, const char *__restrict, ...) __fsfm(4, 5); 83+ 84+#undef __fsfm 85 86 #ifdef __cplusplus 87 } 88--- a/include/stdio.h 89+++ b/include/stdio.h 90@@ -21,6 +21,14 @@ extern "C" { 91 92 #include <bits/alltypes.h> 93 94+#if __GNUC__ >= 3 95+#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y))) 96+#define __fs(x, y) __attribute__ ((__format__ (__scanf__, x, y))) 97+#else 98+#define __fp(x, y) 99+#define __fs(x, y) 100+#endif 101+ 102 #ifdef __cplusplus 103 #define NULL 0L 104 #else 105@@ -102,19 +110,19 @@ int puts(const char *); 106 int printf(const char *__restrict, ...); 107 int fprintf(FILE *__restrict, const char *__restrict, ...); 108 int sprintf(char *__restrict, const char *__restrict, ...); 109-int snprintf(char *__restrict, size_t, const char *__restrict, ...); 110+int snprintf(char *__restrict, size_t, const char *__restrict, ...) __fp(3, 4); 111 112 int vprintf(const char *__restrict, __isoc_va_list); 113 int vfprintf(FILE *__restrict, const char *__restrict, __isoc_va_list); 114 int vsprintf(char *__restrict, const char *__restrict, __isoc_va_list); 115-int vsnprintf(char *__restrict, size_t, const char *__restrict, __isoc_va_list); 116+int vsnprintf(char *__restrict, size_t, const char *__restrict, __isoc_va_list) __fp(3, 0); 117 118 int scanf(const char *__restrict, ...); 119 int fscanf(FILE *__restrict, const char *__restrict, ...); 120 int sscanf(const char *__restrict, const char *__restrict, ...); 121-int vscanf(const char *__restrict, __isoc_va_list); 122-int vfscanf(FILE *__restrict, const char *__restrict, __isoc_va_list); 123-int vsscanf(const char *__restrict, const char *__restrict, __isoc_va_list); 124+int vscanf(const char *__restrict, __isoc_va_list) __fs(1, 0); 125+int vfscanf(FILE *__restrict, const char *__restrict, __isoc_va_list) __fs(2, 0); 126+int vsscanf(const char *__restrict, const char *__restrict, __isoc_va_list) __fs(2, 0); 127 128 void perror(const char *); 129 130@@ -135,8 +143,8 @@ int pclose(FILE *); 131 int fileno(FILE *); 132 int fseeko(FILE *, off_t, int); 133 off_t ftello(FILE *); 134-int dprintf(int, const char *__restrict, ...); 135-int vdprintf(int, const char *__restrict, __isoc_va_list); 136+int dprintf(int, const char *__restrict, ...) __fp(2, 3); 137+int vdprintf(int, const char *__restrict, __isoc_va_list) __fp(2, 0); 138 void flockfile(FILE *); 139 int ftrylockfile(FILE *); 140 void funlockfile(FILE *); 141@@ -175,8 +183,8 @@ int fileno_unlocked(FILE *); 142 int getw(FILE *); 143 int putw(int, FILE *); 144 char *fgetln(FILE *, size_t *); 145-int asprintf(char **, const char *, ...); 146-int vasprintf(char **, const char *, __isoc_va_list); 147+int asprintf(char **, const char *, ...) __fp(2, 3); 148+int vasprintf(char **, const char *, __isoc_va_list) __fp(2, 0); 149 #endif 150 151 #ifdef _GNU_SOURCE 152@@ -184,6 +192,9 @@ char *fgets_unlocked(char *, int, FILE * 153 int fputs_unlocked(const char *, FILE *); 154 #endif 155 156+#undef __fp 157+#undef __fs 158+ 159 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) 160 #define tmpfile64 tmpfile 161 #define fopen64 fopen 162--- a/include/syslog.h 163+++ b/include/syslog.h 164@@ -56,16 +56,22 @@ extern "C" { 165 #define LOG_NOWAIT 0x10 166 #define LOG_PERROR 0x20 167 168+#if __GNUC__ >= 3 169+#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y))) 170+#else 171+#define __fp(x, y) 172+#endif 173+ 174 void closelog (void); 175 void openlog (const char *, int, int); 176 int setlogmask (int); 177-void syslog (int, const char *, ...); 178+void syslog (int, const char *, ...) __fp(2, 3); 179 180 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 181 #define _PATH_LOG "/dev/log" 182 #define __NEED_va_list 183 #include <bits/alltypes.h> 184-void vsyslog (int, const char *, va_list); 185+void vsyslog (int, const char *, va_list) __fp(2, 0); 186 #if defined(SYSLOG_NAMES) 187 #define INTERNAL_NOPRI 0x10 188 #define INTERNAL_MARK (LOG_NFACILITIES<<3) 189@@ -93,6 +99,8 @@ typedef struct { 190 #endif 191 #endif 192 193+#undef __fp 194+ 195 #ifdef __cplusplus 196 } 197 #endif 198