xref: /llvm-project/clang/test/Sema/format-strings.c (revision 88501dc74911b00186298fe1fffe8dfb1f09b1c1)
1 // RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs %s
2 // RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -fno-signed-char %s
3 // RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -triple=x86_64-unknown-fuchsia %s
4 // RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -triple=x86_64-linux-android %s
5 
6 #include <stdarg.h>
7 #include <stddef.h>
8 #define __need_wint_t
9 #include <stddef.h> // For wint_t and wchar_t
10 
11 typedef struct _FILE FILE;
12 int fprintf(FILE *, const char *restrict, ...);
13 int printf(const char *restrict, ...); // expected-note{{passing argument to parameter here}}
14 int snprintf(char *restrict, size_t, const char *restrict, ...);
15 int sprintf(char *restrict, const char *restrict, ...);
16 int vasprintf(char **, const char *, va_list);
17 int asprintf(char **, const char *, ...);
18 int vfprintf(FILE *, const char *restrict, va_list);
19 int vprintf(const char *restrict, va_list);
20 int vsnprintf(char *, size_t, const char *, va_list);
21 int vsprintf(char *restrict, const char *restrict, va_list); // expected-note{{passing argument to parameter here}}
22 
23 int vscanf(const char *restrict format, va_list arg);
24 
25 char * global_fmt;
26 
27 void check_string_literal( FILE* fp, const char* s, char *buf, ... ) {
28 
29   char * b;
30   va_list ap;
31   va_start(ap,buf);
32 
33   printf(s); // expected-warning {{format string is not a string literal}}
34   // expected-note@-1{{treat the string as an argument to avoid this}}
35   vprintf(s,ap); // expected-warning {{format string is not a string literal}}
36   fprintf(fp,s); // expected-warning {{format string is not a string literal}}
37   // expected-note@-1{{treat the string as an argument to avoid this}}
38   vfprintf(fp,s,ap); // expected-warning {{format string is not a string literal}}
39   asprintf(&b,s); // expected-warning {{format string is not a string lit}}
40   // expected-note@-1{{treat the string as an argument to avoid this}}
41   vasprintf(&b,s,ap); // expected-warning {{format string is not a string literal}}
42   sprintf(buf,s); // expected-warning {{format string is not a string literal}}
43   // expected-note@-1{{treat the string as an argument to avoid this}}
44   snprintf(buf,2,s); // expected-warning {{format string is not a string lit}}
45   // expected-note@-1{{treat the string as an argument to avoid this}}
46   __builtin___sprintf_chk(buf,0,-1,s); // expected-warning {{format string is not a string literal}}
47   // expected-note@-1{{treat the string as an argument to avoid this}}
48   __builtin___snprintf_chk(buf,2,0,-1,s); // expected-warning {{format string is not a string lit}}
49   // expected-note@-1{{treat the string as an argument to avoid this}}
50   vsprintf(buf,s,ap); // expected-warning {{format string is not a string lit}}
51   vsnprintf(buf,2,s,ap); // expected-warning {{format string is not a string lit}}
52   vsnprintf(buf,2,global_fmt,ap); // expected-warning {{format string is not a string literal}}
53   __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // expected-warning {{format string is not a string lit}}
54   __builtin___vsnprintf_chk(buf,2,0,-1,global_fmt,ap); // expected-warning {{format string is not a string literal}}
55 
56   vscanf(s, ap); // expected-warning {{format string is not a string literal}}
57 
58   const char *const fmt = "%d"; // FIXME -- defined here
59   printf(fmt, 1, 2); // expected-warning{{data argument not used}}
60 
61   // rdar://6079877
62   printf("abc"
63          "%*d", 1, 1); // no-warning
64   printf("abc\
65 def"
66          "%*d", 1, 1); // no-warning
67 
68   // <rdar://problem/6079850>, allow 'unsigned' (instead of 'int') to be used for both
69   // the field width and precision.  This deviates from C99, but is reasonably safe
70   // and is also accepted by GCC.
71   printf("%*d", (unsigned) 1, 1); // no-warning
72 }
73 
74 // When calling a non-variadic format function (vprintf, vscanf, NSLogv, ...),
75 // warn only if the format string argument is a parameter that is not itself
76 // declared as a format string with compatible format.
77 __attribute__((__format__ (__printf__, 2, 4)))
78 void check_string_literal2( FILE* fp, const char* s, char *buf, ... ) {
79   char * b;
80   va_list ap;
81   va_start(ap,buf);
82 
83   printf(s); // expected-warning {{format string is not a string literal}}
84   // expected-note@-1{{treat the string as an argument to avoid this}}
85   vprintf(s,ap); // no-warning
86   fprintf(fp,s); // expected-warning {{format string is not a string literal}}
87   // expected-note@-1{{treat the string as an argument to avoid this}}
88   vfprintf(fp,s,ap); // no-warning
89   asprintf(&b,s); // expected-warning {{format string is not a string lit}}
90   // expected-note@-1{{treat the string as an argument to avoid this}}
91   vasprintf(&b,s,ap); // no-warning
92   sprintf(buf,s); // expected-warning {{format string is not a string literal}}
93   // expected-note@-1{{treat the string as an argument to avoid this}}
94   snprintf(buf,2,s); // expected-warning {{format string is not a string lit}}
95   // expected-note@-1{{treat the string as an argument to avoid this}}
96   __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // no-warning
97 
98   vscanf(s, ap); // expected-warning {{format string is not a string literal}}
99 }
100 
101 void check_conditional_literal(const char* s, int i) {
102   printf(i == 1 ? "yes" : "no"); // no-warning
103   printf(i == 0 ? (i == 1 ? "yes" : "no") : "dont know"); // no-warning
104   printf(i == 0 ? (i == 1 ? s : "no") : "dont know"); // expected-warning{{format string is not a string literal}}
105   // expected-note@-1{{treat the string as an argument to avoid this}}
106   printf("yes" ?: "no %d", 1); // expected-warning{{data argument not used by format string}}
107   printf(0 ? "yes %s" : "no %d", 1); // no-warning
108   printf(0 ? "yes %d" : "no %s", 1); // expected-warning{{format specifies type 'char *'}}
109 
110   printf(0 ? "yes" : "no %d", 1); // no-warning
111   printf(0 ? "yes %d" : "no", 1); // expected-warning{{data argument not used by format string}}
112   printf(1 ? "yes" : "no %d", 1); // expected-warning{{data argument not used by format string}}
113   printf(1 ? "yes %d" : "no", 1); // no-warning
114   printf(i ? "yes" : "no %d", 1); // no-warning
115   printf(i ? "yes %s" : "no %d", 1); // expected-warning{{format specifies type 'char *'}}
116   printf(i ? "yes" : "no %d", 1, 2); // expected-warning{{data argument not used by format string}}
117 
118   printf(i ? "%*s" : "-", i, s); // no-warning
119   printf(i ? "yes" : 0 ? "no %*d" : "dont know %d", 1, 2); // expected-warning{{data argument not used by format string}}
120   printf(i ? "%i\n" : "%i %s %s\n", i, s); // expected-warning{{more '%' conversions than data arguments}}
121 }
122 
123 #if !defined(__ANDROID__) && !defined(__Fuchsia__)
124 
125 void check_writeback_specifier(void)
126 {
127   int x;
128   char *b;
129   printf("%n", b); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
130   printf("%n", &x); // no-warning
131 
132   printf("%hhn", (signed char*)0); // no-warning
133   printf("%hhn", (char*)0); // no-warning
134   printf("%hhn", (unsigned char*)0); // no-warning
135   printf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
136 
137   printf("%hn", (short*)0); // no-warning
138   printf("%hn", (unsigned short*)0); // no-warning
139   printf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
140 
141   printf("%n", (int*)0); // no-warning
142   printf("%n", (unsigned int*)0); // no-warning
143   printf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
144 
145   printf("%ln", (long*)0); // no-warning
146   printf("%ln", (unsigned long*)0); // no-warning
147   printf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
148 
149   printf("%lln", (long long*)0); // no-warning
150   printf("%lln", (unsigned long long*)0); // no-warning
151   printf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
152 
153   printf("%qn", (long long*)0); // no-warning
154   printf("%qn", (unsigned long long*)0); // no-warning
155   printf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
156 
157   printf("%Ln", 0); // expected-warning{{length modifier 'L' results in undefined behavior or no effect with 'n' conversion specifier}}
158   // expected-note@-1{{did you mean to use 'll'?}}
159 }
160 
161 #else
162 
163 void check_writeback_specifier(void)
164 {
165   int x;
166   printf("%n", &x); // expected-warning{{'%n' specifier not supported on this platform}}
167 
168   printf("%hhn", (signed char*)0); // expected-warning{{'%n' specifier not supported on this platform}}
169   printf("%hhn", (char*)0); // expected-warning{{'%n' specifier not supported on this platform}}
170   printf("%hhn", (unsigned char*)0); // expected-warning{{'%n' specifier not supported on this platform}}
171   printf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
172   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
173 
174   printf("%hn", (short*)0); // expected-warning{{'%n' specifier not supported on this platform}}
175   printf("%hn", (unsigned short*)0); // expected-warning{{'%n' specifier not supported on this platform}}
176   printf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
177   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
178 
179   printf("%n", (int*)0); // expected-warning{{'%n' specifier not supported on this platform}}
180   printf("%n", (unsigned int*)0); // expected-warning{{'%n' specifier not supported on this platform}}
181   printf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
182   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
183 
184   printf("%ln", (long*)0); // expected-warning{{'%n' specifier not supported on this platform}}
185   printf("%ln", (unsigned long*)0); // expected-warning{{'%n' specifier not supported on this platform}}
186   printf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
187   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
188 
189   printf("%lln", (long long*)0); // expected-warning{{'%n' specifier not supported on this platform}}
190   printf("%lln", (unsigned long long*)0); // expected-warning{{'%n' specifier not supported on this platform}}
191   printf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
192   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
193 
194   printf("%qn", (long long*)0); // expected-warning{{'%n' specifier not supported on this platform}}
195   printf("%qn", (unsigned long long*)0); // expected-warning{{'%n' specifier not supported on this platform}}
196 }
197 
198 #endif // !defined(__ANDROID__) && !defined(__Fuchsia__)
199 
200 void check_invalid_specifier(FILE* fp, char *buf)
201 {
202   printf("%s%lv%d","unix",10,20); // expected-warning {{invalid conversion specifier 'v'}} expected-warning {{data argument not used by format string}}
203   fprintf(fp,"%%%l"); // expected-warning {{incomplete format specifier}}
204   sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // expected-warning{{format specifies type 'long' but the argument has type 'int'}}
205   snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning{{format specifies type 'long' but the argument has type 'int'}} expected-warning {{invalid conversion specifier ';'}} expected-warning {{data argument not used by format string}}
206 }
207 
208 void check_null_char_string(char* b)
209 {
210   printf("\0this is bogus%d",1); // expected-warning {{string contains '\0'}}
211   snprintf(b,10,"%%%%%d\0%d",1,2); // expected-warning {{string contains '\0'}}
212   printf("%\0d",1); // expected-warning {{string contains '\0'}}
213 }
214 
215 void check_empty_format_string(char* buf, ...)
216 {
217   va_list ap;
218   va_start(ap,buf);
219   vprintf("",ap); // expected-warning {{format string is empty}}
220   sprintf(buf, "", 1); // expected-warning {{format string is empty}}
221 
222   // Don't warn about empty format strings when there are no data arguments.
223   // This can arise from macro expansions and non-standard format string
224   // functions.
225   sprintf(buf, ""); // no-warning
226 }
227 
228 void check_wide_string(char* b, ...)
229 {
230   va_list ap;
231   va_start(ap,b);
232 
233   printf(L"foo %d",2); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}}
234   vsprintf(b,L"bar %d",ap); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}}
235 }
236 
237 void check_asterisk_precision_width(int x) {
238   printf("%*d"); // expected-warning {{'*' specified field width is missing a matching 'int' argument}}
239   printf("%.*d"); // expected-warning {{'.*' specified field precision is missing a matching 'int' argument}}
240   printf("%*d",12,x); // no-warning
241   printf("%*d","foo",x); // expected-warning {{field width should have type 'int', but argument has type 'char *'}}
242   printf("%.*d","foo",x); // expected-warning {{field precision should have type 'int', but argument has type 'char *'}}
243 }
244 
245 void __attribute__((format(printf,1,3))) myprintf(const char*, int blah, ...);
246 
247 void test_myprintf(void) {
248   myprintf("%d", 17, 18); // okay
249 }
250 
251 void test_constant_bindings(void) {
252   const char * const s1 = "hello";
253   const char s2[] = "hello";
254   const char *s3 = "hello";
255   char * const s4 = "hello";
256   extern const char s5[];
257 
258   printf(s1); // no-warning
259   printf(s2); // no-warning
260   printf(s3); // expected-warning{{not a string literal}}
261   // expected-note@-1{{treat the string as an argument to avoid this}}
262   printf(s4); // expected-warning{{not a string literal}}
263   // expected-note@-1{{treat the string as an argument to avoid this}}
264   printf(s5); // expected-warning{{not a string literal}}
265   // expected-note@-1{{treat the string as an argument to avoid this}}
266 }
267 
268 
269 // Test what happens when -Wformat-security only.
270 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
271 #pragma GCC diagnostic warning "-Wformat-security"
272 
273 void test9(char *P) {
274   int x;
275   printf(P);   // expected-warning {{format string is not a string literal (potentially insecure)}}
276   // expected-note@-1{{treat the string as an argument to avoid this}}
277   printf(P, 42);
278 }
279 
280 void torture(va_list v8) {
281   vprintf ("%*.*d", v8);  // no-warning
282 
283 }
284 
285 void test10(int x, float f, int i, long long lli) {
286   printf("%s"); // expected-warning{{more '%' conversions than data arguments}}
287   printf("%@", 12); // expected-warning{{invalid conversion specifier '@'}}
288   printf("\0"); // expected-warning{{format string contains '\0' within the string body}}
289   printf("xs\0"); // expected-warning{{format string contains '\0' within the string body}}
290   printf("%*d\n"); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
291   printf("%*.*d\n", x); // expected-warning{{'.*' specified field precision is missing a matching 'int' argument}}
292   printf("%*d\n", f, x); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
293   printf("%*.*d\n", x, f, x); // expected-warning{{field precision should have type 'int', but argument has type 'double'}}
294   printf("%**\n"); // expected-warning{{invalid conversion specifier '*'}}
295   printf("%d%d\n", x); // expected-warning{{more '%' conversions than data arguments}}
296   printf("%d\n", x, x); // expected-warning{{data argument not used by format string}}
297   printf("%W%d\n", x, x); // expected-warning{{invalid conversion specifier 'W'}}  expected-warning {{data argument not used by format string}}
298   printf("%"); // expected-warning{{incomplete format specifier}}
299   printf("%.d", x); // no-warning
300   printf("%.", x);  // expected-warning{{incomplete format specifier}}
301   printf("%f", 4); // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
302   printf("%qd", lli); // no-warning
303   printf("%qd", x); // expected-warning{{format specifies type 'long long' but the argument has type 'int'}}
304   printf("%qp", (void *)0); // expected-warning{{length modifier 'q' results in undefined behavior or no effect with 'p' conversion specifier}}
305   printf("hhX %hhX", (unsigned char)10); // no-warning
306   printf("llX %llX", (long long) 10); // no-warning
307   printf("%llb %llB", (long long) 10, (long long) 10); // no-warning
308   // This is fine, because there is an implicit conversion to an int.
309   printf("%d", (unsigned char) 10); // no-warning
310   printf("%d", (long long) 10); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
311   printf("%Lf\n", (long double) 1.0); // no-warning
312   printf("%f\n", (long double) 1.0); // expected-warning{{format specifies type 'double' but the argument has type 'long double'}}
313   // The man page says that a zero precision is okay.
314   printf("%.0Lf", (long double) 1.0); // no-warning
315   printf("%c\n", "x"); // expected-warning{{format specifies type 'int' but the argument has type 'char *'}}
316   printf("%c\n", 1.23); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
317   printf("Format %d, is %! %f", 1, 4.4); // expected-warning{{invalid conversion specifier '!'}}
318 }
319 
320 typedef unsigned char uint8_t;
321 
322 void should_understand_small_integers(void) {
323   printf("%hhu", (short) 10); // expected-warning{{format specifies type 'unsigned char' but the argument has type 'short'}}
324   printf("%hu\n", (unsigned char)1); // warning with -Wformat-pedantic only
325   printf("%hu\n", (uint8_t)1);       // warning with -Wformat-pedantic only
326 }
327 
328 void test11(void *p, char *s) {
329   printf("%p", p); // no-warning
330   printf("%p", 123); // expected-warning{{format specifies type 'void *' but the argument has type 'int'}}
331   printf("%.4p", p); // expected-warning{{precision used with 'p' conversion specifier, resulting in undefined behavior}}
332   printf("%+p", p); // expected-warning{{flag '+' results in undefined behavior with 'p' conversion specifier}}
333   printf("% p", p); // expected-warning{{flag ' ' results in undefined behavior with 'p' conversion specifier}}
334   printf("%0p", p); // expected-warning{{flag '0' results in undefined behavior with 'p' conversion specifier}}
335   printf("%s", s); // no-warning
336   printf("%+s", p); // expected-warning{{flag '+' results in undefined behavior with 's' conversion specifier}}
337                     // expected-warning@-1 {{format specifies type 'char *' but the argument has type 'void *'}}
338   printf("% s", p); // expected-warning{{flag ' ' results in undefined behavior with 's' conversion specifier}}
339                     // expected-warning@-1 {{format specifies type 'char *' but the argument has type 'void *'}}
340   printf("%0s", p); // expected-warning{{flag '0' results in undefined behavior with 's' conversion specifier}}
341                     // expected-warning@-1 {{format specifies type 'char *' but the argument has type 'void *'}}
342 }
343 
344 void test12(char *b) {
345   unsigned char buf[4];
346   printf ("%.4s\n", buf); // no-warning
347   printf ("%.4s\n", &buf); // expected-warning{{format specifies type 'char *' but the argument has type 'unsigned char (*)[4]'}}
348 
349   // Verify that we are checking asprintf
350   asprintf(&b, "%d", "asprintf"); // expected-warning{{format specifies type 'int' but the argument has type 'char *'}}
351 }
352 
353 void test13(short x) {
354   char bel = 007;
355   printf("bel: '0%hhd'\n", bel); // no-warning
356   printf("x: '0%hhd'\n", x); // expected-warning {{format specifies type 'char' but the argument has type 'short'}}
357 }
358 
359 typedef struct __aslclient *aslclient;
360 typedef struct __aslmsg *aslmsg;
361 int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __attribute__((__format__ (__printf__, 4, 5)));
362 void test_asl(aslclient asl) {
363   // Test case from <rdar://problem/7341605>.
364   asl_log(asl, 0, 3, "Error: %m"); // no-warning
365   asl_log(asl, 0, 3, "Error: %W"); // expected-warning{{invalid conversion specifier 'W'}}
366 }
367 
368 // <rdar://problem/7595366>
369 typedef enum { A } int_t;
370 void f0(int_t x) { printf("%d\n", x); }
371 
372 // Unicode test cases.  These are possibly specific to Mac OS X.  If so, they should
373 // eventually be moved into a separate test.
374 
375 void test_unicode_conversions(wchar_t *s) {
376   printf("%S", s); // no-warning
377   printf("%s", s); // expected-warning{{format specifies type 'char *' but the argument has type 'wchar_t *'}}
378   printf("%C", s[0]); // no-warning
379 #if defined(__sun) && !defined(__LP64__)
380   printf("%c", s[0]); // expected-warning{{format specifies type 'int' but the argument has type 'wchar_t' (aka 'long')}}
381 #else
382   printf("%c", s[0]);
383 #endif
384   // FIXME: This test reports inconsistent results. On Windows, '%C' expects
385   // 'unsigned short'.
386   // printf("%C", 10);
387   printf("%S", "hello"); // expected-warning{{but the argument has type 'char *'}}
388 }
389 
390 // Mac OS X supports positional arguments in format strings.
391 // This is an IEEE extension (IEEE Std 1003.1).
392 // FIXME: This is probably not portable everywhere.
393 void test_positional_arguments(void) {
394   printf("%0$", (int)2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
395   printf("%1$*0$d", (int) 2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
396   printf("%1$d", (int) 2); // no-warning
397   printf("%1$d", (int) 2, 2); // expected-warning{{data argument not used by format string}}
398   printf("%1$d%1$f", (int) 2); // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
399   printf("%1$2.2d", (int) 2); // no-warning
400   printf("%2$*1$.2d", (int) 2, (int) 3); // no-warning
401   printf("%2$*8$d", (int) 2, (int) 3); // expected-warning{{specified field width is missing a matching 'int' argument}}
402   printf("%%%1$d", (int) 2); // no-warning
403   printf("%1$d%%", (int) 2); // no-warning
404 }
405 
406 // PR 6697 - Handle format strings where the data argument is not adjacent to the format string
407 void myprintf_PR_6697(const char *format, int x, ...) __attribute__((__format__(printf,1, 3)));
408 void test_pr_6697(void) {
409   myprintf_PR_6697("%s\n", 1, "foo"); // no-warning
410   myprintf_PR_6697("%s\n", 1, (int)0); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
411   // FIXME: Not everything should clearly support positional arguments,
412   // but we need a way to identify those cases.
413   myprintf_PR_6697("%1$s\n", 1, "foo"); // no-warning
414   myprintf_PR_6697("%2$s\n", 1, "foo"); // expected-warning{{data argument position '2' exceeds the number of data arguments (1)}}
415   myprintf_PR_6697("%18$s\n", 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (1)}}
416   myprintf_PR_6697("%1$s\n", 1, (int) 0); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
417 }
418 
419 void rdar8026030(FILE *fp) {
420   fprintf(fp, "\%"); // expected-warning{{incomplete format specifier}}
421 }
422 
423 void bug7377_bad_length_mod_usage(void) {
424   // Bad length modifiers
425   printf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
426   printf("%1$zp", (void *)0); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
427   printf("%ls", L"foo"); // no-warning
428   printf("%#.2Lf", (long double)1.234); // no-warning
429 
430   // Bad flag usage
431   printf("%#p", (void *) 0); // expected-warning{{flag '#' results in undefined behavior with 'p' conversion specifier}}
432   printf("%0d", -1); // no-warning
433   printf("%0b%0B", -1u, -1u); // no-warning
434   printf("%-p", (void *) 0); // no-warning
435 #if !defined(__ANDROID__) && !defined(__Fuchsia__)
436   printf("%#n", (int *) 0); // expected-warning{{flag '#' results in undefined behavior with 'n' conversion specifier}}
437   printf("%-n", (int *) 0); // expected-warning{{flag '-' results in undefined behavior with 'n' conversion specifier}}
438 #else
439   printf("%#n", (int *) 0); // expected-warning{{flag '#' results in undefined behavior with 'n' conversion specifier}}
440   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
441   printf("%-n", (int *) 0); // expected-warning{{flag '-' results in undefined behavior with 'n' conversion specifier}}
442   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
443 #endif // !defined(__ANDROID__) && !defined(__Fuchsia__)
444 
445   // Bad optional amount use
446   printf("%.2c", 'a'); // expected-warning{{precision used with 'c' conversion specifier, resulting in undefined behavior}}
447 #if !defined(__ANDROID__) && !defined(__Fuchsia__)
448   printf("%1n", (int *) 0); // expected-warning{{field width used with 'n' conversion specifier, resulting in undefined behavior}}
449   printf("%.9n", (int *) 0); // expected-warning{{precision used with 'n' conversion specifier, resulting in undefined behavior}}
450 #else
451   printf("%1n", (int *) 0); // expected-warning{{field width used with 'n' conversion specifier, resulting in undefined behavior}}
452   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
453   printf("%.9n", (int *) 0); // expected-warning{{precision used with 'n' conversion specifier, resulting in undefined behavior}}
454   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
455 #endif // #if !defined(__ANDROID__) && !defined(__Fuchsia__)
456 
457   // Ignored flags
458   printf("% +f", 1.23); // expected-warning{{flag ' ' is ignored when flag '+' is present}}
459   printf("%+ f", 1.23); // expected-warning{{flag ' ' is ignored when flag '+' is present}}
460   printf("%0-f", 1.23); // expected-warning{{flag '0' is ignored when flag '-' is present}}
461   printf("%-0f", 1.23); // expected-warning{{flag '0' is ignored when flag '-' is present}}
462   printf("%-+f", 1.23); // no-warning
463 }
464 
465 // PR 7981 - handle '%lc' (wint_t)
466 
467 void pr7981(wint_t c, wchar_t c2) {
468   printf("%lc", c); // no-warning
469   printf("%lc", 1.0); // expected-warning{{the argument has type 'double'}}
470 #if __WINT_WIDTH__ == 32 && !(defined(__sun) && !defined(__LP64__))
471   printf("%lc", (char) 1); // no-warning
472 #else
473   printf("%lc", (char) 1); // expected-warning{{the argument has type 'char'}}
474 #endif
475   printf("%lc", &c); // expected-warning{{the argument has type 'wint_t *'}}
476   // If wint_t and wchar_t are the same width and wint_t is signed where
477   // wchar_t is unsigned, an implicit conversion isn't possible.
478 #if defined(__WINT_UNSIGNED__) || !defined(__WCHAR_UNSIGNED__) ||   \
479   __WINT_WIDTH__ > __WCHAR_WIDTH__
480   printf("%lc", c2); // no-warning
481 #endif
482 }
483 
484 // <rdar://problem/8269537> -Wformat-security says NULL is not a string literal
485 void rdar8269537(void) {
486   // This is likely to crash in most cases, but -Wformat-nonliteral technically
487   // doesn't warn in this case.
488   printf(0); // no-warning
489 }
490 
491 // Handle functions with multiple format attributes.
492 extern void rdar8332221_vprintf_scanf(const char *, va_list, const char *, ...)
493      __attribute__((__format__(__printf__, 1, 0)))
494      __attribute__((__format__(__scanf__, 3, 4)));
495 
496 void rdar8332221(va_list ap, int *x, long *y) {
497   rdar8332221_vprintf_scanf("%", ap, "%d", x); // expected-warning{{incomplete format specifier}}
498 }
499 
500 // PR8641
501 void pr8641(void) {
502   printf("%#x\n", 10);
503   printf("%#X\n", 10);
504   printf("%#b %#15.8B\n", 10, 10u);
505 }
506 
507 void posix_extensions(void) {
508   // Test %'d, "thousands grouping".
509   // <rdar://problem/8816343>
510   printf("%'d\n", 123456789); // no-warning
511   printf("%'i\n", 123456789); // no-warning
512   printf("%'f\n", (float) 1.0); // no-warning
513   printf("%'p\n", (void*) 0); // expected-warning{{results in undefined behavior with 'p' conversion specifier}}
514   printf("%'b\n", 123456789); // expected-warning{{results in undefined behavior with 'b' conversion specifier}}
515   printf("%'B\n", 123456789); // expected-warning{{results in undefined behavior with 'B' conversion specifier}}
516 }
517 
518 // PR8486
519 //
520 // Test what happens when -Wformat is on, but -Wformat-security is off.
521 #pragma GCC diagnostic warning "-Wformat"
522 #pragma GCC diagnostic ignored "-Wformat-security"
523 
524 void pr8486(void) {
525   printf("%s", 1); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
526 }
527 
528 // PR9314
529 // Don't warn about string literals that are PreDefinedExprs, e.g. __func__.
530 void pr9314(void) {
531   printf(__PRETTY_FUNCTION__); // no-warning
532   printf(__func__); // no-warning
533 }
534 
535 int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
536 
537 void rdar9612060(void) {
538   printf("%s", 2); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
539 }
540 
541 void check_char(unsigned char x, signed char y) {
542   printf("%c", y); // no-warning
543   printf("%hhu", x); // no-warning
544   printf("%hhi", y); // no-warning
545   printf("%hhi", x); // no-warning
546   printf("%c", x); // no-warning
547   printf("%hhu", y); // no-warning
548   printf("%hhb %hhB", x, x); // no-warning
549 }
550 
551 // Test suppression of individual warnings.
552 
553 void test_suppress_invalid_specifier(void) {
554 #pragma clang diagnostic push
555 #pragma clang diagnostic ignored "-Wformat-invalid-specifier"
556   printf("%@", 12); // no-warning
557 #pragma clang diagnostic pop
558 }
559 
560 // Make sure warnings are on for next test.
561 #pragma GCC diagnostic warning "-Wformat"
562 #pragma GCC diagnostic warning "-Wformat-security"
563 
564 // Test that the printf call site is where the warning is attached.  If the
565 // format string is somewhere else, point to it in a note.
566 void pr9751(void) {
567   const char kFormat1[] = "%d %d \n"; // expected-note{{format string is defined here}}}
568   printf(kFormat1, 0); // expected-warning{{more '%' conversions than data arguments}}
569   printf("%d %s\n", 0); // expected-warning{{more '%' conversions than data arguments}}
570 
571   const char kFormat2[] = "%18$s\n"; // expected-note{{format string is defined here}}
572   printf(kFormat2, 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (2)}}
573   printf("%18$s\n", 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (2)}}
574 
575   const char kFormat4[] = "%y"; // expected-note{{format string is defined here}}
576   printf(kFormat4, 5); // expected-warning{{invalid conversion specifier 'y'}}
577   printf("%y", 5); // expected-warning{{invalid conversion specifier 'y'}}
578 
579   const char kFormat5[] = "%."; // expected-note{{format string is defined here}}
580   printf(kFormat5, 5); // expected-warning{{incomplete format specifier}}
581   printf("%.", 5); // expected-warning{{incomplete format specifier}}
582 
583   const char kFormat6[] = "%s"; // expected-note{{format string is defined here}}
584   printf(kFormat6, 5); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
585   printf("%s", 5); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
586 
587   const char kFormat7[] = "%0$"; // expected-note{{format string is defined here}}
588   printf(kFormat7, 5); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
589   printf("%0$", 5); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
590 
591   const char kFormat8[] = "%1$d %d"; // expected-note{{format string is defined here}}
592   printf(kFormat8, 4, 4); // expected-warning{{cannot mix positional and non-positional arguments in format string}}
593   printf("%1$d %d", 4, 4); // expected-warning{{cannot mix positional and non-positional arguments in format string}}
594 
595   const char kFormat9[] = ""; // expected-note{{format string is defined here}}
596   printf(kFormat9, 4, 4); // expected-warning{{format string is empty}}
597   printf("", 4, 4); // expected-warning{{format string is empty}}
598 
599   const char kFormat10[] = "\0%d"; // expected-note{{format string is defined here}}
600   printf(kFormat10, 4); // expected-warning{{format string contains '\0' within the string body}}
601   printf("\0%d", 4); // expected-warning{{format string contains '\0' within the string body}}
602 
603   const char kFormat11[] = "%*d"; // expected-note{{format string is defined here}}
604   printf(kFormat11); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
605   printf("%*d"); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
606 
607   const char kFormat12[] = "%*d"; // expected-note{{format string is defined here}}
608   printf(kFormat12, 4.4); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
609   printf("%*d", 4.4); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
610 
611   const char kFormat13[] = "%.3p"; // expected-note{{format string is defined here}}
612   void *p;
613   printf(kFormat13, p); // expected-warning{{precision used with 'p' conversion specifier, resulting in undefined behavior}}
614   printf("%.3p", p); // expected-warning{{precision used with 'p' conversion specifier, resulting in undefined behavior}}
615 
616   const char kFormat14[] = "%0s"; // expected-note{{format string is defined here}}
617   printf(kFormat14, "a"); // expected-warning{{flag '0' results in undefined behavior with 's' conversion specifier}}
618   printf("%0s", "a"); // expected-warning{{flag '0' results in undefined behavior with 's' conversion specifier}}
619 
620   const char kFormat15[] = "%hhs"; // expected-note{{format string is defined here}}
621   printf(kFormat15, "a"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
622   printf("%hhs", "a"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
623 
624   const char kFormat16[] = "%-0d"; // expected-note{{format string is defined here}}
625   printf(kFormat16, 5); // expected-warning{{flag '0' is ignored when flag '-' is present}}
626   printf("%-0d", 5); // expected-warning{{flag '0' is ignored when flag '-' is present}}
627 
628   // Make sure that the "format string is defined here" note is not emitted
629   // when the original string is within the argument expression.
630   printf(1 ? "yes %d" : "no %d"); // expected-warning{{more '%' conversions than data arguments}}
631 
632   const char kFormat17[] = "%hu"; // expected-note{{format string is defined here}}}
633   printf(kFormat17, (int[]){0}); // expected-warning{{format specifies type 'unsigned short' but the argument}}
634 
635   printf("%a", (long double)0); // expected-warning{{format specifies type 'double' but the argument has type 'long double'}}
636 
637   // Test braced char[] initializers.
638   const char kFormat18[] = { "%lld" }; // expected-note{{format string is defined here}}
639   printf(kFormat18, 0); // expected-warning{{format specifies type}}
640 
641   // Make sure we point at the offending argument rather than the format string.
642   const char kFormat19[] = "%d";  // expected-note{{format string is defined here}}
643   printf(kFormat19,
644          0.0); // expected-warning{{format specifies}}
645 }
646 
647 void pr18905(void) {
648   const char s1[] = "s\0%s"; // expected-note{{format string is defined here}}
649   const char s2[1] = "s"; // expected-note{{format string is defined here}}
650   const char s3[2] = "s\0%s"; // expected-warning{{initializer-string for char array is too long}}
651   const char s4[10] = "s";
652   const char s5[0] = "%s"; // expected-warning{{initializer-string for char array is too long}}
653                            // expected-note@-1{{format string is defined here}}
654 
655   printf(s1); // expected-warning{{format string contains '\0' within the string body}}
656   printf(s2); // expected-warning{{format string is not null-terminated}}
657   printf(s3); // no-warning
658   printf(s4); // no-warning
659   printf(s5); // expected-warning{{format string is not null-terminated}}
660 }
661 
662 void __attribute__((format(strfmon,1,2))) monformat(const char *fmt, ...);
663 void __attribute__((format(strftime,1,0))) dateformat(const char *fmt);
664 
665 // Other formats
666 void test_other_formats(void) {
667   char *str = "";
668   monformat("", 1); // expected-warning{{format string is empty}}
669   monformat(str); // expected-warning{{format string is not a string literal (potentially insecure)}}
670   dateformat(""); // expected-warning{{format string is empty}}
671   dateformat(str); // no-warning (using strftime non-literal is not unsafe)
672 }
673 
674 // Do not warn about unused arguments coming from system headers.
675 // <rdar://problem/11317765>
676 #include <format-unused-system-args.h>
677 void test_unused_system_args(int x) {
678   PRINT1("%d\n", x); // no-warning{{extra argument is system header is OK}}
679 }
680 
681 void pr12761(char c) {
682   // This should not warn even with -fno-signed-char.
683   printf("%hhx", c);
684 }
685 
686 void test_opencl_vector_format(int x) {
687   printf("%v4d", x); // expected-warning{{invalid conversion specifier 'v'}}
688   printf("%vd", x); // expected-warning{{invalid conversion specifier 'v'}}
689   printf("%0vd", x); // expected-warning{{invalid conversion specifier 'v'}}
690   printf("%hlf", x); // expected-warning{{invalid conversion specifier 'l'}}
691   printf("%hld", x); // expected-warning{{invalid conversion specifier 'l'}}
692 }
693 
694 // Test that we correctly merge the format in both orders.
695 extern void test14_foo(const char *, const char *, ...)
696      __attribute__((__format__(__printf__, 1, 3)));
697 extern void test14_foo(const char *, const char *, ...)
698      __attribute__((__format__(__scanf__, 2, 3)));
699 
700 extern void test14_bar(const char *, const char *, ...)
701      __attribute__((__format__(__scanf__, 2, 3)));
702 extern void test14_bar(const char *, const char *, ...)
703      __attribute__((__format__(__printf__, 1, 3)));
704 
705 void test14_zed(int *p) {
706   test14_foo("%", "%d", p); // expected-warning{{incomplete format specifier}}
707   test14_bar("%", "%d", p); // expected-warning{{incomplete format specifier}}
708 }
709 
710 #if !defined(__ANDROID__) && !defined(__Fuchsia__)
711 
712 void test_qualifiers(volatile int *vip, const int *cip,
713                      const volatile int *cvip) {
714   printf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
715   printf("%n", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
716 
717   printf("%n", vip); // No warning.
718   printf("%p", cip); // No warning.
719   printf("%p", cvip); // No warning.
720 
721 
722   typedef int* ip_t;
723   typedef const int* cip_t;
724   printf("%n", (ip_t)0); // No warning.
725   printf("%n", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
726 }
727 
728 #else
729 
730 void test_qualifiers(volatile int *vip, const int *cip,
731                      const volatile int *cvip) {
732   printf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
733   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
734   printf("%n", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
735   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
736 
737   printf("%n", vip); // expected-warning{{'%n' specifier not supported on this platform}}
738   printf("%p", cip); // No warning.
739   printf("%p", cvip); // No warning.
740 
741 
742   typedef int* ip_t;
743   typedef const int* cip_t;
744   printf("%n", (ip_t)0); // expected-warning{{'%n' specifier not supported on this platform}}
745   printf("%n", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
746   // expected-warning@-1 {{'%n' specifier not supported on this platform}}
747 }
748 
749 #endif // #if !defined(__ANDROID__) && !defined(__Fuchsia__)
750 
751 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
752 #pragma GCC diagnostic warning "-Wformat-security"
753 // <rdar://problem/14178260>
754 extern void test_format_security_extra_args(const char*, int, ...)
755     __attribute__((__format__(__printf__, 1, 3)));
756 void test_format_security_pos(char* string) {
757   test_format_security_extra_args(string, 5); // expected-warning {{format string is not a string literal (potentially insecure)}}
758   // expected-note@-1{{treat the string as an argument to avoid this}}
759 }
760 #pragma GCC diagnostic warning "-Wformat-nonliteral"
761 
762 void test_char_pointer_arithmetic(int b) {
763   const char s1[] = "string";
764   const char s2[] = "%s string";
765 
766   printf(s1 - 1);  // expected-warning {{format string is not a string literal (potentially insecure)}}
767   // expected-note@-1{{treat the string as an argument to avoid this}}
768 
769   printf(s1 + 2);  // no-warning
770   printf(s2 + 2);  // no-warning
771 
772   const char s3[] = "%s string";
773   printf((s3 + 2) - 2);  // expected-warning{{more '%' conversions than data arguments}}
774   // expected-note@-2{{format string is defined here}}
775   printf(2 + s2);             // no-warning
776   printf(6 + s2 - 2);         // no-warning
777   printf(2 + (b ? s1 : s2));  // no-warning
778 
779   const char s5[] = "string %s";
780   printf(2 + (b ? s2 : s5));  // expected-warning{{more '%' conversions than data arguments}}
781   // expected-note@-2{{format string is defined here}}
782   printf(2 + (b ? s2 : s5), "");      // no-warning
783   printf(2 + (b ? s1 : s2 - 2), "");  // no-warning
784 
785   const char s6[] = "%s string";
786   printf(2 + (b ? s1 : s6 - 2));  // expected-warning{{more '%' conversions than data arguments}}
787   // expected-note@-2{{format string is defined here}}
788   printf(1 ? s2 + 2 : s2);  // no-warning
789   printf(0 ? s2 : s2 + 2);  // no-warning
790   printf(2 + s2 + 5 * 3 - 16, "");  // expected-warning{{data argument not used}}
791 
792   const char s7[] = "%s string %s %s";
793   printf(s7 + 3, "");  // expected-warning{{more '%' conversions than data arguments}}
794   // expected-note@-2{{format string is defined here}}
795 }
796 
797 void PR30481(void) {
798   // This caused crashes due to invalid casts.
799   printf(1 > 0); // expected-warning{{format string is not a string literal}} expected-error{{incompatible integer to pointer conversion}} expected-note@format-strings.c:*{{passing argument to parameter here}} expected-note{{to avoid this}}
800 }
801 
802 void test_printf_opaque_ptr(void *op) {
803   printf("%s", op); // expected-warning{{format specifies type 'char *' but the argument has type 'void *'}}
804 }
805 
806 void test_block(void) {
807   void __attribute__((__format__(__printf__, 1, 2))) (^printf_arg1)(
808       const char *, ...) =
809       ^(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2))) {
810     va_list ap;
811     va_start(ap, fmt);
812     vprintf(fmt, ap);
813     va_end(ap);
814   };
815 
816   printf_arg1("%s string %i\n", "aaa", 123);
817   printf_arg1("%s string\n", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
818 
819   void __attribute__((__format__(__printf__, 2, 3))) (^printf_arg2)(
820       const char *, const char *, ...) =
821       ^(const char *not_fmt, const char *fmt, ...)
822           __attribute__((__format__(__printf__, 2, 3))) {
823     va_list ap;
824     va_start(ap, fmt);
825     vprintf(fmt, ap);
826     vprintf(not_fmt, ap); // expected-warning{{format string is not a string literal}}
827     va_end(ap);
828   };
829 
830   printf_arg2("foo", "%s string %i\n", "aaa", 123);
831   printf_arg2("%s string\n", "foo", "bar"); // expected-warning{{data argument not used by format string}}
832 }
833