musl: Add format attribute to some function declarations
[openwrt.git] / toolchain / musl / patches / 040-Add-format-attribute-to-some-function-declarations.patch
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
5
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.
14
15 GCC automatically adds checks for these functions: printf, fprintf,
16 sprintf, scanf, fscanf, sscanf, strftime, vprintf, vfprintf and
17 vsprintf.
18
19 The documentation from gcc is here:
20 https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
21
22 The documentation from Clang is here:
23 http://clang.llvm.org/docs/AttributeReference.html#format-gnu-format
24
25 Signed-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 diff --git a/include/err.h b/include/err.h
34 index 9f5cb6b..a5e3cde 100644
35 --- a/include/err.h
36 +++ b/include/err.h
37 @@ -8,15 +8,23 @@
38  extern "C" {
39  #endif
40  
41 -void warn(const char *, ...);
42 -void vwarn(const char *, va_list);
43 -void warnx(const char *, ...);
44 -void vwarnx(const char *, va_list);
45 -
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);
50 +#if __GNUC__ >= 3
51 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
52 +#else
53 +#define __fp(x, y)
54 +#endif
55 +
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);
60 +
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);
65 +
66 +#undef __fp
67  
68  #ifdef __cplusplus
69  }
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" {
75  
76  #include <bits/alltypes.h>
77  
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, ...);
80 +#if __GNUC__ >= 3
81 +#define __fsfm(x, y) __attribute__ ((__format__ (__strfmon__, x, y)))
82 +#else
83 +#define __fsfm(x, y)
84 +#endif
85 +
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);
88 +
89 +#undef __fsfm
90  
91  #ifdef __cplusplus
92  }
93 diff --git a/include/stdio.h b/include/stdio.h
94 index 884d2e6..17ca68e 100644
95 --- a/include/stdio.h
96 +++ b/include/stdio.h
97 @@ -21,6 +21,14 @@ extern "C" {
98  
99  #include <bits/alltypes.h>
100  
101 +#if __GNUC__ >= 3
102 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
103 +#define __fs(x, y) __attribute__ ((__format__ (__scanf__, x, y)))
104 +#else
105 +#define __fp(x, y)
106 +#define __fs(x, y)
107 +#endif
108 +
109  #ifdef __cplusplus
110  #define NULL 0L
111  #else
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);
118  
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);
124  
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);
134  
135  void perror(const char *);
136  
137 @@ -135,8 +143,8 @@ int pclose(FILE *);
138  int fileno(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 *);
149  int getw(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);
156  #endif
157  
158  #ifdef _GNU_SOURCE
159 @@ -184,6 +192,9 @@ char *fgets_unlocked(char *, int, FILE *);
160  int fputs_unlocked(const char *, FILE *);
161  #endif
162  
163 +#undef __fp
164 +#undef __fs
165 +
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
176  
177 +#if __GNUC__ >= 3
178 +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
179 +#else
180 +#define __fp(x, y)
181 +#endif
182 +
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);
188  
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 {
199  #endif
200  #endif
201  
202 +#undef __fp
203 +
204  #ifdef __cplusplus
205  }
206  #endif
207 -- 
208 2.7.0.rc3
209