1 From e6683d001a95d7c3d4d992496f00f77e01fcd268 Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Sun, 22 Nov 2015 15:04:23 +0100
4 Subject: [PATCH v2] Add format attribute to some function declarations
6 GCC and Clang are able to check the format arguments given to a
7 function and warn the user if there is a error in the format arguments
8 or if there is a potential uncontrolled format string security problem
9 in the code. GCC does this automatically for some functions like
10 printf(), but it is also possible to annotate other functions in a way
11 that it will check them too. This feature is used by glibc for many
12 functions. This patch adds the attribute to the some functions of musl
13 expect for these functions where gcc automatically adds it.
15 GCC automatically adds checks for these functions: printf, fprintf,
16 sprintf, scanf, fscanf, sscanf, strftime, vprintf, vfprintf and
19 The documentation from gcc is here:
20 https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
22 The documentation from Clang is here:
23 http://clang.llvm.org/docs/AttributeReference.html#format-gnu-format
25 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
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(-)
33 diff --git a/include/err.h b/include/err.h
34 index 9f5cb6b..a5e3cde 100644
41 -void warn(const char *, ...);
42 -void vwarn(const char *, va_list);
43 -void warnx(const char *, ...);
44 -void vwarnx(const char *, va_list);
46 -_Noreturn void err(int, const char *, ...);
47 -_Noreturn void verr(int, const char *, va_list);
48 -_Noreturn void errx(int, const char *, ...);
49 -_Noreturn void verrx(int, const char *, va_list);
51 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
56 +void warn(const char *, ...) __fp(1, 2);
57 +void vwarn(const char *, va_list) __fp(1, 0);
58 +void warnx(const char *, ...) __fp(1, 2);
59 +void vwarnx(const char *, va_list) __fp(1, 0);
61 +_Noreturn void err(int, const char *, ...) __fp(2, 3);
62 +_Noreturn void verr(int, const char *, va_list) __fp(2, 0);
63 +_Noreturn void errx(int, const char *, ...) __fp(2, 3);
64 +_Noreturn void verrx(int, const char *, va_list) __fp(2, 0);
70 diff --git a/include/monetary.h b/include/monetary.h
71 index a91fa56..85c4d23 100644
72 --- a/include/monetary.h
73 +++ b/include/monetary.h
74 @@ -13,8 +13,16 @@ extern "C" {
76 #include <bits/alltypes.h>
78 -ssize_t strfmon(char *__restrict, size_t, const char *__restrict, ...);
79 -ssize_t strfmon_l(char *__restrict, size_t, locale_t, const char *__restrict, ...);
81 +#define __fsfm(x, y) __attribute__ ((__format__ (__strfmon__, x, y)))
86 +ssize_t strfmon(char *__restrict, size_t, const char *__restrict, ...) __fsfm(3, 4);
87 +ssize_t strfmon_l(char *__restrict, size_t, locale_t, const char *__restrict, ...) __fsfm(4, 5);
93 diff --git a/include/stdio.h b/include/stdio.h
94 index 884d2e6..17ca68e 100644
97 @@ -21,6 +21,14 @@ extern "C" {
99 #include <bits/alltypes.h>
102 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
103 +#define __fs(x, y) __attribute__ ((__format__ (__scanf__, x, y)))
112 @@ -102,19 +110,19 @@ int puts(const char *);
113 int printf(const char *__restrict, ...);
114 int fprintf(FILE *__restrict, const char *__restrict, ...);
115 int sprintf(char *__restrict, const char *__restrict, ...);
116 -int snprintf(char *__restrict, size_t, const char *__restrict, ...);
117 +int snprintf(char *__restrict, size_t, const char *__restrict, ...) __fp(3, 4);
119 int vprintf(const char *__restrict, __isoc_va_list);
120 int vfprintf(FILE *__restrict, const char *__restrict, __isoc_va_list);
121 int vsprintf(char *__restrict, const char *__restrict, __isoc_va_list);
122 -int vsnprintf(char *__restrict, size_t, const char *__restrict, __isoc_va_list);
123 +int vsnprintf(char *__restrict, size_t, const char *__restrict, __isoc_va_list) __fp(3, 0);
125 int scanf(const char *__restrict, ...);
126 int fscanf(FILE *__restrict, const char *__restrict, ...);
127 int sscanf(const char *__restrict, const char *__restrict, ...);
128 -int vscanf(const char *__restrict, __isoc_va_list);
129 -int vfscanf(FILE *__restrict, const char *__restrict, __isoc_va_list);
130 -int vsscanf(const char *__restrict, const char *__restrict, __isoc_va_list);
131 +int vscanf(const char *__restrict, __isoc_va_list) __fs(1, 0);
132 +int vfscanf(FILE *__restrict, const char *__restrict, __isoc_va_list) __fs(2, 0);
133 +int vsscanf(const char *__restrict, const char *__restrict, __isoc_va_list) __fs(2, 0);
135 void perror(const char *);
137 @@ -135,8 +143,8 @@ int pclose(FILE *);
139 int fseeko(FILE *, off_t, int);
140 off_t ftello(FILE *);
141 -int dprintf(int, const char *__restrict, ...);
142 -int vdprintf(int, const char *__restrict, __isoc_va_list);
143 +int dprintf(int, const char *__restrict, ...) __fp(2, 3);
144 +int vdprintf(int, const char *__restrict, __isoc_va_list) __fp(2, 0);
145 void flockfile(FILE *);
146 int ftrylockfile(FILE *);
147 void funlockfile(FILE *);
148 @@ -175,8 +183,8 @@ int fileno_unlocked(FILE *);
150 int putw(int, FILE *);
151 char *fgetln(FILE *, size_t *);
152 -int asprintf(char **, const char *, ...);
153 -int vasprintf(char **, const char *, __isoc_va_list);
154 +int asprintf(char **, const char *, ...) __fp(2, 3);
155 +int vasprintf(char **, const char *, __isoc_va_list) __fp(2, 0);
159 @@ -184,6 +192,9 @@ char *fgets_unlocked(char *, int, FILE *);
160 int fputs_unlocked(const char *, FILE *);
166 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
167 #define tmpfile64 tmpfile
168 #define fopen64 fopen
169 diff --git a/include/syslog.h b/include/syslog.h
170 index 5b4d296..33b549d 100644
171 --- a/include/syslog.h
172 +++ b/include/syslog.h
173 @@ -56,16 +56,22 @@ extern "C" {
174 #define LOG_NOWAIT 0x10
175 #define LOG_PERROR 0x20
178 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
183 void closelog (void);
184 void openlog (const char *, int, int);
185 int setlogmask (int);
186 -void syslog (int, const char *, ...);
187 +void syslog (int, const char *, ...) __fp(2, 3);
189 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
190 #define _PATH_LOG "/dev/log"
191 #define __NEED_va_list
192 #include <bits/alltypes.h>
193 -void vsyslog (int, const char *, va_list);
194 +void vsyslog (int, const char *, va_list) __fp(2, 0);
195 #if defined(SYSLOG_NAMES)
196 #define INTERNAL_NOPRI 0x10
197 #define INTERNAL_MARK (LOG_NFACILITIES<<3)
198 @@ -93,6 +99,8 @@ typedef struct {