Debian Codegraph @

clang/test/Sema/format-strings.c

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