xref: /llvm-project/clang/test/Sema/format-strings.c (revision 6e302b2e6e8912a2e92ee035b6412a5d098ade1c)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s
2 
3 #include <stdarg.h>
4 typedef __typeof(sizeof(int)) size_t;
5 typedef struct _FILE FILE;
6 int fprintf(FILE *, const char *restrict, ...);
7 int printf(const char *restrict, ...); // expected-note{{passing argument to parameter here}}
8 int snprintf(char *restrict, size_t, const char *restrict, ...);
9 int sprintf(char *restrict, const char *restrict, ...);
10 int vasprintf(char **, const char *, va_list);
11 int asprintf(char **, const char *, ...);
12 int vfprintf(FILE *, const char *restrict, va_list);
13 int vprintf(const char *restrict, va_list);
14 int vsnprintf(char *, size_t, const char *, va_list);
15 int vsprintf(char *restrict, const char *restrict, va_list); // expected-note{{passing argument to parameter here}}
16 
17 char * global_fmt;
18 
19 void check_string_literal( FILE* fp, const char* s, char *buf, ... ) {
20 
21   char * b;
22   va_list ap;
23   va_start(ap,buf);
24 
25   printf(s); // expected-warning {{format string is not a string literal}}
26   vprintf(s,ap); // // no-warning
27   fprintf(fp,s); // expected-warning {{format string is not a string literal}}
28   vfprintf(fp,s,ap); // no-warning
29   asprintf(&b,s); // expected-warning {{format string is not a string lit}}
30   vasprintf(&b,s,ap); // no-warning
31   sprintf(buf,s); // expected-warning {{format string is not a string literal}}
32   snprintf(buf,2,s); // expected-warning {{format string is not a string lit}}
33   __builtin___sprintf_chk(buf,0,-1,s); // expected-warning {{format string is not a string literal}}
34   __builtin___snprintf_chk(buf,2,0,-1,s); // expected-warning {{format string is not a string lit}}
35   vsprintf(buf,s,ap); // no-warning
36   vsnprintf(buf,2,s,ap); // no-warning
37   vsnprintf(buf,2,global_fmt,ap); // expected-warning {{format string is not a string literal}}
38   __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // no-warning
39   __builtin___vsnprintf_chk(buf,2,0,-1,global_fmt,ap); // expected-warning {{format string is not a string literal}}
40 
41   // rdar://6079877
42   printf("abc"
43          "%*d", 1, 1); // no-warning
44   printf("abc\
45 def"
46          "%*d", 1, 1); // no-warning
47 
48   // <rdar://problem/6079850>, allow 'unsigned' (instead of 'int') to be used for both
49   // the field width and precision.  This deviates from C99, but is reasonably safe
50   // and is also accepted by GCC.
51   printf("%*d", (unsigned) 1, 1); // no-warning
52 }
53 
54 void check_conditional_literal(const char* s, int i) {
55   printf(i == 1 ? "yes" : "no"); // no-warning
56   printf(i == 0 ? (i == 1 ? "yes" : "no") : "dont know"); // no-warning
57   printf(i == 0 ? (i == 1 ? s : "no") : "dont know"); // expected-warning{{format string is not a string literal}}
58   printf("yes" ?: "no %d", 1); // expected-warning{{data argument not used by format string}}
59 }
60 
61 void check_writeback_specifier()
62 {
63   int x;
64   char *b;
65 
66   printf("%n",&x); // expected-warning {{'%n' in format string discouraged}}
67   sprintf(b,"%d%%%n",1, &x); // expected-warning {{'%n' in format string dis}}
68 }
69 
70 void check_invalid_specifier(FILE* fp, char *buf)
71 {
72   printf("%s%lb%d","unix",10,20); // expected-warning {{invalid conversion specifier 'b'}}
73   fprintf(fp,"%%%l"); // expected-warning {{incomplete format specifier}}
74   sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // expected-warning{{conversion specifies type 'long' but the argument has type 'int'}}
75   snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning{{conversion specifies type 'long' but the argument has type 'int'}} expected-warning {{invalid conversion specifier ';'}}
76 }
77 
78 void check_null_char_string(char* b)
79 {
80   printf("\0this is bogus%d",1); // expected-warning {{string contains '\0'}}
81   snprintf(b,10,"%%%%%d\0%d",1,2); // expected-warning {{string contains '\0'}}
82   printf("%\0d",1); // expected-warning {{string contains '\0'}}
83 }
84 
85 void check_empty_format_string(char* buf, ...)
86 {
87   va_list ap;
88   va_start(ap,buf);
89   vprintf("",ap); // expected-warning {{format string is empty}}
90   sprintf(buf, "", 1); // expected-warning {{format string is empty}}
91 
92   // Don't warn about empty format strings when there are no data arguments.
93   // This can arise from macro expansions and non-standard format string
94   // functions.
95   sprintf(buf, ""); // no-warning
96 }
97 
98 void check_wide_string(char* b, ...)
99 {
100   va_list ap;
101   va_start(ap,b);
102 
103   printf(L"foo %d",2); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}}
104   vsprintf(b,L"bar %d",ap); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}}
105 }
106 
107 void check_asterisk_precision_width(int x) {
108   printf("%*d"); // expected-warning {{'*' specified field width is missing a matching 'int' argument}}
109   printf("%.*d"); // expected-warning {{'.*' specified field precision is missing a matching 'int' argument}}
110   printf("%*d",12,x); // no-warning
111   printf("%*d","foo",x); // expected-warning {{field width should have type 'int', but argument has type 'char *'}}
112   printf("%.*d","foo",x); // expected-warning {{field precision should have type 'int', but argument has type 'char *'}}
113 }
114 
115 void __attribute__((format(printf,1,3))) myprintf(const char*, int blah, ...);
116 
117 void test_myprintf() {
118   myprintf("%d", 17, 18); // okay
119 }
120 
121 void test_constant_bindings(void) {
122   const char * const s1 = "hello";
123   const char s2[] = "hello";
124   const char *s3 = "hello";
125   char * const s4 = "hello";
126   extern const char s5[];
127 
128   printf(s1); // no-warning
129   printf(s2); // no-warning
130   printf(s3); // expected-warning{{not a string literal}}
131   printf(s4); // expected-warning{{not a string literal}}
132   printf(s5); // expected-warning{{not a string literal}}
133 }
134 
135 
136 // Test what happens when -Wformat-security only.
137 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
138 #pragma GCC diagnostic warning "-Wformat-security"
139 
140 void test9(char *P) {
141   int x;
142   printf(P);   // expected-warning {{format string is not a string literal (potentially insecure)}}
143   printf(P, 42);
144   printf("%n", &x); // expected-warning {{use of '%n' in format string discouraged }}
145 }
146 
147 void torture(va_list v8) {
148   vprintf ("%*.*d", v8);  // no-warning
149 
150 }
151 
152 void test10(int x, float f, int i, long long lli) {
153   printf("%s"); // expected-warning{{more '%' conversions than data arguments}}
154   printf("%@", 12); // expected-warning{{invalid conversion specifier '@'}}
155   printf("\0"); // expected-warning{{format string contains '\0' within the string body}}
156   printf("xs\0"); // expected-warning{{format string contains '\0' within the string body}}
157   printf("%*d\n"); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
158   printf("%*.*d\n", x); // expected-warning{{'.*' specified field precision is missing a matching 'int' argument}}
159   printf("%*d\n", f, x); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
160   printf("%*.*d\n", x, f, x); // expected-warning{{field precision should have type 'int', but argument has type 'double'}}
161   printf("%**\n"); // expected-warning{{invalid conversion specifier '*'}}
162   printf("%n", &i); // expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}
163   printf("%d%d\n", x); // expected-warning{{more '%' conversions than data arguments}}
164   printf("%d\n", x, x); // expected-warning{{data argument not used by format string}}
165   printf("%W%d%Z\n", x, x, x); // expected-warning{{invalid conversion specifier 'W'}} expected-warning{{invalid conversion specifier 'Z'}}
166   printf("%"); // expected-warning{{incomplete format specifier}}
167   printf("%.d", x); // no-warning
168   printf("%.", x);  // expected-warning{{incomplete format specifier}}
169   printf("%f", 4); // expected-warning{{conversion specifies type 'double' but the argument has type 'int'}}
170   printf("%qd", lli);
171   printf("hhX %hhX", (unsigned char)10); // no-warning
172   printf("llX %llX", (long long) 10); // no-warning
173   // This is fine, because there is an implicit conversion to an int.
174   printf("%d", (unsigned char) 10); // no-warning
175   printf("%d", (long long) 10); // expected-warning{{conversion specifies type 'int' but the argument has type 'long long'}}
176   printf("%Lf\n", (long double) 1.0); // no-warning
177   printf("%f\n", (long double) 1.0); // expected-warning{{conversion specifies type 'double' but the argument has type 'long double'}}
178   // The man page says that a zero precision is okay.
179   printf("%.0Lf", (long double) 1.0); // no-warning
180   printf("%c\n", "x"); // expected-warning{{conversion specifies type 'int' but the argument has type 'char *'}}
181   printf("%c\n", 1.23); // expected-warning{{conversion specifies type 'int' but the argument has type 'double'}}
182   printf("Format %d, is %! %f", 1, 2, 4.4); // expected-warning{{invalid conversion specifier '!'}}
183 }
184 
185 typedef unsigned char uint8_t;
186 
187 void should_understand_small_integers() {
188   printf("%hhu", (short) 10); // expected-warning{{conversion specifies type 'unsigned char' but the argument has type 'short'}}
189   printf("%hu\n", (unsigned char) 1); // expected-warning{{conversion specifies type 'unsigned short' but the argument has type 'unsigned char'}}
190   printf("%hu\n", (uint8_t)1); // expected-warning{{conversion specifies type 'unsigned short' but the argument has type 'uint8_t'}}
191 }
192 
193 void test11(void *p, char *s) {
194   printf("%p", p); // no-warning
195   printf("%p", 123); // expected-warning{{conversion specifies type 'void *' but the argument has type 'int'}}
196   printf("%.4p", p); // expected-warning{{precision used with 'p' conversion specifier, resulting in undefined behavior}}
197   printf("%+p", p); // expected-warning{{flag '+' results in undefined behavior with 'p' conversion specifier}}
198   printf("% p", p); // expected-warning{{flag ' ' results in undefined behavior with 'p' conversion specifier}}
199   printf("%0p", p); // expected-warning{{flag '0' results in undefined behavior with 'p' conversion specifier}}
200   printf("%s", s); // no-warning
201   printf("%+s", p); // expected-warning{{flag '+' results in undefined behavior with 's' conversion specifier}}
202   printf("% s", p); // expected-warning{{flag ' ' results in undefined behavior with 's' conversion specifier}}
203   printf("%0s", p); // expected-warning{{flag '0' results in undefined behavior with 's' conversion specifier}}
204 }
205 
206 void test12(char *b) {
207   unsigned char buf[4];
208   printf ("%.4s\n", buf); // no-warning
209   printf ("%.4s\n", &buf); // expected-warning{{conversion specifies type 'char *' but the argument has type 'unsigned char (*)[4]'}}
210 
211   // Verify that we are checking asprintf
212   asprintf(&b, "%d", "asprintf"); // expected-warning{{conversion specifies type 'int' but the argument has type 'char *'}}
213 }
214 
215 typedef struct __aslclient *aslclient;
216 typedef struct __aslmsg *aslmsg;
217 int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __attribute__((__format__ (__printf__, 4, 5)));
218 void test_asl(aslclient asl) {
219   // Test case from <rdar://problem/7341605>.
220   asl_log(asl, 0, 3, "Error: %m"); // no-warning
221   asl_log(asl, 0, 3, "Error: %W"); // expected-warning{{invalid conversion specifier 'W'}}
222 }
223 
224 // <rdar://problem/7595366>
225 typedef enum { A } int_t;
226 void f0(int_t x) { printf("%d\n", x); }
227 
228 // Unicode test cases.  These are possibly specific to Mac OS X.  If so, they should
229 // eventually be moved into a separate test.
230 typedef __WCHAR_TYPE__ wchar_t;
231 
232 void test_unicode_conversions(wchar_t *s) {
233   printf("%S", s); // no-warning
234   printf("%s", s); // expected-warning{{conversion specifies type 'char *' but the argument has type 'wchar_t *'}}
235   printf("%C", s[0]); // no-warning
236   printf("%c", s[0]);
237   // FIXME: This test reports inconsistent results. On Windows, '%C' expects
238   // 'unsigned short'.
239   // printf("%C", 10);
240   // FIXME: we report the expected type as 'int*' instead of 'wchar_t*'
241   printf("%S", "hello"); // expected-warning{{but the argument has type 'char *'}}
242 }
243 
244 // Mac OS X supports positional arguments in format strings.
245 // This is an IEEE extension (IEEE Std 1003.1).
246 // FIXME: This is probably not portable everywhere.
247 void test_positional_arguments() {
248   printf("%0$", (int)2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
249   printf("%1$*0$d", (int) 2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
250   printf("%1$d", (int) 2); // no-warning
251   printf("%1$d", (int) 2, 2); // expected-warning{{data argument not used by format string}}
252   printf("%1$d%1$f", (int) 2); // expected-warning{{conversion specifies type 'double' but the argument has type 'int'}}
253   printf("%1$2.2d", (int) 2); // no-warning
254   printf("%2$*1$.2d", (int) 2, (int) 3); // no-warning
255   printf("%2$*8$d", (int) 2, (int) 3); // expected-warning{{specified field width is missing a matching 'int' argument}}
256   printf("%%%1$d", (int) 2); // no-warning
257   printf("%1$d%%", (int) 2); // no-warning
258 }
259 
260 // PR 6697 - Handle format strings where the data argument is not adjacent to the format string
261 void myprintf_PR_6697(const char *format, int x, ...) __attribute__((__format__(printf,1, 3)));
262 void test_pr_6697() {
263   myprintf_PR_6697("%s\n", 1, "foo"); // no-warning
264   myprintf_PR_6697("%s\n", 1, (int)0); // expected-warning{{conversion specifies type 'char *' but the argument has type 'int'}}
265   // FIXME: Not everything should clearly support positional arguments,
266   // but we need a way to identify those cases.
267   myprintf_PR_6697("%1$s\n", 1, "foo"); // no-warning
268   myprintf_PR_6697("%2$s\n", 1, "foo"); // expected-warning{{data argument position '2' exceeds the number of data arguments (1)}}
269   myprintf_PR_6697("%18$s\n", 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (1)}}
270   myprintf_PR_6697("%1$s\n", 1, (int) 0); // expected-warning{{conversion specifies type 'char *' but the argument has type 'int'}}
271 }
272 
273 void rdar8026030(FILE *fp) {
274   fprintf(fp, "\%"); // expected-warning{{incomplete format specifier}}
275 }
276 
277 void bug7377_bad_length_mod_usage() {
278   // Bad length modifiers
279   printf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
280   printf("%1$zp", (void *)0); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
281   printf("%ls", L"foo"); // no-warning
282   printf("%#.2Lf", (long double)1.234); // no-warning
283 
284   // Bad flag usage
285   printf("%#p", (void *) 0); // expected-warning{{flag '#' results in undefined behavior with 'p' conversion specifier}}
286   printf("%0d", -1); // no-warning
287   printf("%#n", (void *) 0); // expected-warning{{flag '#' results in undefined behavior with 'n' conversion specifier}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}
288   printf("%-n", (void *) 0); // expected-warning{{flag '-' results in undefined behavior with 'n' conversion specifier}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}
289   printf("%-p", (void *) 0); // no-warning
290 
291   // Bad optional amount use
292   printf("%.2c", 'a'); // expected-warning{{precision used with 'c' conversion specifier, resulting in undefined behavior}}
293   printf("%1n", (void *) 0); // expected-warning{{field width used with 'n' conversion specifier, resulting in undefined behavior}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}
294   printf("%.9n", (void *) 0); // expected-warning{{precision used with 'n' conversion specifier, resulting in undefined behavior}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}
295 
296   // Ignored flags
297   printf("% +f", 1.23); // expected-warning{{flag ' ' is ignored when flag '+' is present}}
298   printf("%+ f", 1.23); // expected-warning{{flag ' ' is ignored when flag '+' is present}}
299   printf("%0-f", 1.23); // expected-warning{{flag '0' is ignored when flag '-' is present}}
300   printf("%-0f", 1.23); // expected-warning{{flag '0' is ignored when flag '-' is present}}
301   printf("%-+f", 1.23); // no-warning
302 }
303 
304 // PR 7981 - handle '%lc' (wint_t)
305 #ifndef wint_t
306 typedef int __darwin_wint_t;
307 typedef __darwin_wint_t wint_t;
308 #endif
309 
310 void pr7981(wint_t c, wchar_t c2) {
311   printf("%lc", c); // no-warning
312   printf("%lc", 1.0); // expected-warning{{the argument has type 'double'}}
313   printf("%lc", (char) 1); // no-warning
314   printf("%lc", &c); // expected-warning{{the argument has type 'wint_t *' (aka 'int *')}}
315   printf("%lc", c2); // no-warning
316 }
317 
318 // <rdar://problem/8269537> -Wformat-security says NULL is not a string literal
319 void rdar8269537() {
320   // This is likely to crash in most cases, but -Wformat-nonliteral technically
321   // doesn't warn in this case.
322   printf(0); // no-warning
323 }
324 
325 // Handle functions with multiple format attributes.
326 extern void rdar8332221_vprintf_scanf(const char *, va_list, const char *, ...)
327      __attribute__((__format__(__printf__, 1, 0)))
328      __attribute__((__format__(__scanf__, 3, 4)));
329 
330 void rdar8332221(va_list ap, int *x, long *y) {
331   rdar8332221_vprintf_scanf("%", ap, "%d", x); // expected-warning{{incomplete format specifier}}
332 }
333 
334 // PR8641
335 void pr8641() {
336   printf("%#x\n", 10);
337   printf("%#X\n", 10);
338 }
339 
340 void posix_extensions() {
341   // Test %'d, "thousands grouping".
342   // <rdar://problem/8816343>
343   printf("%'d\n", 123456789); // no-warning
344   printf("%'i\n", 123456789); // no-warning
345   printf("%'f\n", (float) 1.0); // no-warning
346   printf("%'p\n", (void*) 0); // expected-warning{{results in undefined behavior with 'p' conversion specifier}}
347 }
348 
349 // PR8486
350 //
351 // Test what happens when -Wformat is on, but -Wformat-security is off.
352 #pragma GCC diagnostic warning "-Wformat"
353 #pragma GCC diagnostic ignored "-Wformat-security"
354 
355 void pr8486() {
356   printf("%s", 1); // expected-warning{{conversion specifies type 'char *' but the argument has type 'int'}}
357 }
358 
359 // PR9314
360 // Don't warn about string literals that are PreDefinedExprs, e.g. __func__.
361 void pr9314() {
362   printf(__PRETTY_FUNCTION__); // no-warning
363   printf(__func__); // no-warning
364 }
365 
366 int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
367 
368 void rdar9612060(void) {
369   printf("%s", 2); // expected-warning{{conversion specifies type 'char *' but the argument has type 'int'}}
370 }
371 
372 void check_char(unsigned char x, signed char y) {
373   printf("%c", y); // no-warning
374   printf("%hhu", x); // no-warning
375   printf("%hhi", y); // no-warning
376   printf("%hhi", x); // no-warning
377   printf("%c", x); // no-warning
378   printf("%hhu", y); // no-warning
379 }
380 
381 // Test suppression of individual warnings.
382 
383 void test_suppress_invalid_specifier() {
384 #pragma clang diagnostic push
385 #pragma clang diagnostic ignored "-Wformat-invalid-specifier"
386   printf("%@", 12); // no-warning
387 #pragma clang diagnostic pop
388 }
389 
390