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