1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s
2*67e74705SXin Li
3*67e74705SXin Li // Test that -Wformat=0 works:
4*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -Werror -Wformat=0 %s
5*67e74705SXin Li
6*67e74705SXin Li #include <stdarg.h>
7*67e74705SXin Li typedef __typeof(sizeof(int)) size_t;
8*67e74705SXin Li typedef struct _FILE FILE;
9*67e74705SXin Li typedef __WCHAR_TYPE__ wchar_t;
10*67e74705SXin Li
11*67e74705SXin Li int fscanf(FILE * restrict, const char * restrict, ...) ;
12*67e74705SXin Li int scanf(const char * restrict, ...) ;
13*67e74705SXin Li int sscanf(const char * restrict, const char * restrict, ...) ;
14*67e74705SXin Li int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2)));
15*67e74705SXin Li
16*67e74705SXin Li int vscanf(const char * restrict, va_list);
17*67e74705SXin Li int vfscanf(FILE * restrict, const char * restrict, va_list);
18*67e74705SXin Li int vsscanf(const char * restrict, const char * restrict, va_list);
19*67e74705SXin Li
test(const char * s,int * i)20*67e74705SXin Li void test(const char *s, int *i) {
21*67e74705SXin Li scanf(s, i); // expected-warning{{format string is not a string literal}}
22*67e74705SXin Li scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
23*67e74705SXin Li scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
24*67e74705SXin Li scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
25*67e74705SXin Li
26*67e74705SXin Li unsigned short s_x;
27*67e74705SXin Li scanf ("%" "hu" "\n", &s_x); // no-warning
28*67e74705SXin Li scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
29*67e74705SXin Li scanf("%%"); // no-warning
30*67e74705SXin Li scanf("%%%1$d", i); // no-warning
31*67e74705SXin Li scanf("%1$d%%", i); // no-warning
32*67e74705SXin Li scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
33*67e74705SXin Li scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
34*67e74705SXin Li scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
35*67e74705SXin Li scanf("%*d%1$d", i); // no-warning
36*67e74705SXin Li
37*67e74705SXin Li scanf("%s", (char*)0); // no-warning
38*67e74705SXin Li scanf("%s", (volatile char*)0); // no-warning
39*67e74705SXin Li scanf("%s", (signed char*)0); // no-warning
40*67e74705SXin Li scanf("%s", (unsigned char*)0); // no-warning
41*67e74705SXin Li scanf("%hhu", (signed char*)0); // no-warning
42*67e74705SXin Li }
43*67e74705SXin Li
bad_length_modifiers(char * s,void * p,wchar_t * ws,long double * ld)44*67e74705SXin Li void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
45*67e74705SXin Li scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
46*67e74705SXin Li scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
47*67e74705SXin Li scanf("%ls", ws); // no-warning
48*67e74705SXin Li scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
49*67e74705SXin Li }
50*67e74705SXin Li
51*67e74705SXin Li // Test that the scanf call site is where the warning is attached. If the
52*67e74705SXin Li // format string is somewhere else, point to it in a note.
pr9751()53*67e74705SXin Li void pr9751() {
54*67e74705SXin Li int *i;
55*67e74705SXin Li char str[100];
56*67e74705SXin Li const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
57*67e74705SXin Li scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
58*67e74705SXin Li scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
59*67e74705SXin Li const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
60*67e74705SXin Li scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
61*67e74705SXin Li scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
62*67e74705SXin Li const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}}
63*67e74705SXin Li scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}}
64*67e74705SXin Li const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}}
65*67e74705SXin Li scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}}
66*67e74705SXin Li }
67*67e74705SXin Li
test_variants(int * i,const char * s,...)68*67e74705SXin Li void test_variants(int *i, const char *s, ...) {
69*67e74705SXin Li FILE *f = 0;
70*67e74705SXin Li char buf[100];
71*67e74705SXin Li
72*67e74705SXin Li fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
73*67e74705SXin Li sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
74*67e74705SXin Li my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
75*67e74705SXin Li
76*67e74705SXin Li va_list ap;
77*67e74705SXin Li va_start(ap, s);
78*67e74705SXin Li
79*67e74705SXin Li vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
80*67e74705SXin Li vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
81*67e74705SXin Li vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
82*67e74705SXin Li }
83*67e74705SXin Li
test_scanlist(int * ip,char * sp,wchar_t * ls)84*67e74705SXin Li void test_scanlist(int *ip, char *sp, wchar_t *ls) {
85*67e74705SXin Li scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
86*67e74705SXin Li scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}}
87*67e74705SXin Li scanf("%l[xyx]", ls); // no-warning
88*67e74705SXin Li scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}}
89*67e74705SXin Li
90*67e74705SXin Li // PR19559
91*67e74705SXin Li scanf("%[]% ]", sp); // no-warning
92*67e74705SXin Li scanf("%[^]% ]", sp); // no-warning
93*67e74705SXin Li scanf("%[a^]% ]", sp); // expected-warning {{invalid conversion specifier ' '}}
94*67e74705SXin Li }
95*67e74705SXin Li
test_alloc_extension(char ** sp,wchar_t ** lsp,float * fp)96*67e74705SXin Li void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) {
97*67e74705SXin Li /* Make sure "%a" gets parsed as a conversion specifier for float,
98*67e74705SXin Li * even when followed by an 's', 'S' or '[', which would cause it to be
99*67e74705SXin Li * parsed as a length modifier in C90. */
100*67e74705SXin Li scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
101*67e74705SXin Li scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}}
102*67e74705SXin Li scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
103*67e74705SXin Li
104*67e74705SXin Li // Test that the 'm' length modifier is only allowed with s, S, c, C or [.
105*67e74705SXin Li // TODO: Warn that 'm' is an extension.
106*67e74705SXin Li scanf("%ms", sp); // No warning.
107*67e74705SXin Li scanf("%mS", lsp); // No warning.
108*67e74705SXin Li scanf("%mc", sp); // No warning.
109*67e74705SXin Li scanf("%mC", lsp); // No warning.
110*67e74705SXin Li scanf("%m[abc]", sp); // No warning.
111*67e74705SXin Li scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}}
112*67e74705SXin Li
113*67e74705SXin Li // Test argument type check for the 'm' length modifier.
114*67e74705SXin Li scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
115*67e74705SXin Li scanf("%mS", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
116*67e74705SXin Li scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
117*67e74705SXin Li scanf("%mC", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
118*67e74705SXin Li scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
119*67e74705SXin Li }
120*67e74705SXin Li
test_quad(int * x,long long * llx)121*67e74705SXin Li void test_quad(int *x, long long *llx) {
122*67e74705SXin Li scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
123*67e74705SXin Li scanf("%qd", llx); // no-warning
124*67e74705SXin Li }
125*67e74705SXin Li
test_writeback(int * x)126*67e74705SXin Li void test_writeback(int *x) {
127*67e74705SXin Li scanf("%n", (void*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'void *'}}
128*67e74705SXin Li scanf("%n %c", x, x); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
129*67e74705SXin Li
130*67e74705SXin Li scanf("%hhn", (signed char*)0); // no-warning
131*67e74705SXin Li scanf("%hhn", (char*)0); // no-warning
132*67e74705SXin Li scanf("%hhn", (unsigned char*)0); // no-warning
133*67e74705SXin Li scanf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
134*67e74705SXin Li
135*67e74705SXin Li scanf("%hn", (short*)0); // no-warning
136*67e74705SXin Li scanf("%hn", (unsigned short*)0); // no-warning
137*67e74705SXin Li scanf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
138*67e74705SXin Li
139*67e74705SXin Li scanf("%n", (int*)0); // no-warning
140*67e74705SXin Li scanf("%n", (unsigned int*)0); // no-warning
141*67e74705SXin Li scanf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
142*67e74705SXin Li
143*67e74705SXin Li scanf("%ln", (long*)0); // no-warning
144*67e74705SXin Li scanf("%ln", (unsigned long*)0); // no-warning
145*67e74705SXin Li scanf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
146*67e74705SXin Li
147*67e74705SXin Li scanf("%lln", (long long*)0); // no-warning
148*67e74705SXin Li scanf("%lln", (unsigned long long*)0); // no-warning
149*67e74705SXin Li scanf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
150*67e74705SXin Li
151*67e74705SXin Li scanf("%qn", (long long*)0); // no-warning
152*67e74705SXin Li scanf("%qn", (unsigned long long*)0); // no-warning
153*67e74705SXin Li scanf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
154*67e74705SXin Li
155*67e74705SXin Li }
156*67e74705SXin Li
test_qualifiers(const int * cip,volatile int * vip,const char * ccp,volatile char * vcp,const volatile int * cvip)157*67e74705SXin Li void test_qualifiers(const int *cip, volatile int* vip,
158*67e74705SXin Li const char *ccp, volatile char* vcp,
159*67e74705SXin Li const volatile int *cvip) {
160*67e74705SXin Li scanf("%d", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
161*67e74705SXin Li scanf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
162*67e74705SXin Li scanf("%s", ccp); // expected-warning{{format specifies type 'char *' but the argument has type 'const char *'}}
163*67e74705SXin Li scanf("%d", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
164*67e74705SXin Li
165*67e74705SXin Li scanf("%d", vip); // No warning.
166*67e74705SXin Li scanf("%n", vip); // No warning.
167*67e74705SXin Li scanf("%c", vcp); // No warning.
168*67e74705SXin Li
169*67e74705SXin Li typedef int* ip_t;
170*67e74705SXin Li typedef const int* cip_t;
171*67e74705SXin Li scanf("%d", (ip_t)0); // No warning.
172*67e74705SXin Li scanf("%d", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
173*67e74705SXin Li }
174*67e74705SXin Li
check_conditional_literal(char * s,int * i)175*67e74705SXin Li void check_conditional_literal(char *s, int *i) {
176*67e74705SXin Li scanf(0 ? "%s" : "%d", i); // no warning
177*67e74705SXin Li scanf(1 ? "%s" : "%d", i); // expected-warning{{format specifies type 'char *'}}
178*67e74705SXin Li scanf(0 ? "%d %d" : "%d", i); // no warning
179*67e74705SXin Li scanf(1 ? "%d %d" : "%d", i); // expected-warning{{more '%' conversions than data arguments}}
180*67e74705SXin Li scanf(0 ? "%d %d" : "%d", i, s); // expected-warning{{data argument not used}}
181*67e74705SXin Li scanf(1 ? "%d %s" : "%d", i, s); // no warning
182*67e74705SXin Li scanf(i ? "%d %s" : "%d", i, s); // no warning
183*67e74705SXin Li scanf(i ? "%d" : "%d", i, s); // expected-warning{{data argument not used}}
184*67e74705SXin Li scanf(i ? "%s" : "%d", s); // expected-warning{{format specifies type 'int *'}}
185*67e74705SXin Li }
186