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