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(-)
39 -void warn(const char *, ...);
40 -void vwarn(const char *, va_list);
41 -void warnx(const char *, ...);
42 -void vwarnx(const char *, va_list);
44 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
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);
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);
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);
67 --- a/include/monetary.h
68 +++ b/include/monetary.h
69 @@ -13,8 +13,16 @@ extern "C" {
71 #include <bits/alltypes.h>
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, ...);
76 +#define __fsfm(x, y) __attribute__ ((__format__ (__strfmon__, x, y)))
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);
90 @@ -21,6 +21,14 @@ extern "C" {
92 #include <bits/alltypes.h>
95 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
96 +#define __fs(x, y) __attribute__ ((__format__ (__scanf__, x, y)))
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);
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);
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);
128 void perror(const char *);
130 @@ -135,8 +143,8 @@ int pclose(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 *);
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);
152 @@ -184,6 +192,9 @@ char *fgets_unlocked(char *, int, FILE *
153 int fputs_unlocked(const char *, FILE *);
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
169 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
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);
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 {