[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / format-strings-scanf.c
blobd6e403148a5c41fd63699cc46659c692758f73de
1 // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wformat-nonliteral %s
3 // Test that -Wformat=0 works:
4 // RUN: %clang_cc1 -std=c11 -fsyntax-only -Werror -Wformat=0 %s
6 #include <stdarg.h>
7 typedef __SIZE_TYPE__ size_t;
8 #define __SSIZE_TYPE__ \
9 __typeof__(_Generic((__SIZE_TYPE__)0, \
10 unsigned long long int : (long long int)0, \
11 unsigned long int : (long int)0, \
12 unsigned int : (int)0, \
13 unsigned short : (short)0, \
14 unsigned char : (signed char)0))
15 typedef __SSIZE_TYPE__ ssize_t;
17 typedef __PTRDIFF_TYPE__ ptrdiff_t;
18 #define __UNSIGNED_PTRDIFF_TYPE__ \
19 __typeof__(_Generic((__PTRDIFF_TYPE__)0, \
20 long long int : (unsigned long long int)0, \
21 long int : (unsigned long int)0, \
22 int : (unsigned int)0, \
23 short : (unsigned short)0, \
24 signed char : (unsigned char)0))
26 typedef struct _FILE FILE;
27 typedef __WCHAR_TYPE__ wchar_t;
29 int fscanf(FILE * restrict, const char * restrict, ...) ;
30 int scanf(const char * restrict, ...) ;
31 int sscanf(const char * restrict, const char * restrict, ...) ;
32 int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2)));
34 int vscanf(const char * restrict, va_list);
35 int vfscanf(FILE * restrict, const char * restrict, va_list);
36 int vsscanf(const char * restrict, const char * restrict, va_list);
38 void test(const char *s, int *i) {
39 scanf(s, i); // expected-warning{{format string is not a string literal}}
40 scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
41 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
42 scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
43 scanf("%B", i); // expected-warning{{invalid conversion specifier 'B'}}
45 unsigned short s_x;
46 scanf ("%" "hu" "\n", &s_x); // no-warning
47 scanf("%hb", &s_x);
48 scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
49 scanf("%%"); // no-warning
50 scanf("%%%1$d", i); // no-warning
51 scanf("%1$d%%", i); // no-warning
52 scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
53 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
54 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
55 scanf("%*d%1$d", i); // no-warning
57 scanf("%s", (char*)0); // no-warning
58 scanf("%s", (volatile char*)0); // no-warning
59 scanf("%s", (signed char*)0); // no-warning
60 scanf("%s", (unsigned char*)0); // no-warning
61 scanf("%hhu", (signed char*)0); // no-warning
62 scanf("%hhb", (signed char*)0); // no-warning
65 void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
66 scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
67 scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
68 scanf("%ls", ws); // no-warning
69 scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
72 // Test that the scanf call site is where the warning is attached. If the
73 // format string is somewhere else, point to it in a note.
74 void pr9751(void) {
75 int *i;
76 char str[100];
77 const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
78 scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
79 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
80 const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
81 scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
82 scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
83 const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}}
84 scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}}
85 const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}}
86 scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}}
89 void test_variants(int *i, const char *s, ...) {
90 FILE *f = 0;
91 char buf[100];
93 fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
94 sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
95 my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
97 va_list ap;
98 va_start(ap, s);
100 vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
101 vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
102 vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
105 void test_scanlist(int *ip, char *sp, wchar_t *ls) {
106 scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
107 scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}}
108 scanf("%l[xyx]", ls); // no-warning
109 scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}}
111 // PR19559
112 scanf("%[]% ]", sp); // no-warning
113 scanf("%[^]% ]", sp); // no-warning
114 scanf("%[a^]% ]", sp); // expected-warning {{invalid conversion specifier ' '}}
117 void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) {
118 /* Make sure "%a" gets parsed as a conversion specifier for float,
119 * even when followed by an 's', 'S' or '[', which would cause it to be
120 * parsed as a length modifier in C90. */
121 scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
122 scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}}
123 scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
125 // Test that the 'm' length modifier is only allowed with s, S, c, C or [.
126 // TODO: Warn that 'm' is an extension.
127 scanf("%ms", sp); // No warning.
128 scanf("%mS", lsp); // No warning.
129 scanf("%mc", sp); // No warning.
130 scanf("%mC", lsp); // No warning.
131 scanf("%m[abc]", sp); // No warning.
132 scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}}
134 // Test argument type check for the 'm' length modifier.
135 scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
136 scanf("%mS", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
137 scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
138 scanf("%mC", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
139 scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
142 void test_quad(int *x, long long *llx) {
143 scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
144 scanf("%qd", llx); // no-warning
147 void test_writeback(int *x) {
148 scanf("%n", (void*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'void *'}}
149 scanf("%n %c", x, x); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
151 scanf("%hhn", (signed char*)0); // no-warning
152 scanf("%hhn", (char*)0); // no-warning
153 scanf("%hhn", (unsigned char*)0); // no-warning
154 scanf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
156 scanf("%hn", (short*)0); // no-warning
157 scanf("%hn", (unsigned short*)0); // no-warning
158 scanf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
160 scanf("%n", (int*)0); // no-warning
161 scanf("%n", (unsigned int*)0); // no-warning
162 scanf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
164 scanf("%ln", (long*)0); // no-warning
165 scanf("%ln", (unsigned long*)0); // no-warning
166 scanf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
168 scanf("%lln", (long long*)0); // no-warning
169 scanf("%lln", (unsigned long long*)0); // no-warning
170 scanf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
172 scanf("%qn", (long long*)0); // no-warning
173 scanf("%qn", (unsigned long long*)0); // no-warning
174 scanf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
178 void test_qualifiers(const int *cip, volatile int* vip,
179 const char *ccp, volatile char* vcp,
180 const volatile int *cvip) {
181 scanf("%d", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
182 scanf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
183 scanf("%s", ccp); // expected-warning{{format specifies type 'char *' but the argument has type 'const char *'}}
184 scanf("%d", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
186 scanf("%d", vip); // No warning.
187 scanf("%n", vip); // No warning.
188 scanf("%c", vcp); // No warning.
190 typedef int* ip_t;
191 typedef const int* cip_t;
192 scanf("%d", (ip_t)0); // No warning.
193 scanf("%d", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
196 void test_size_types(void) {
197 size_t s = 0;
198 scanf("%zu", &s); // No warning.
199 scanf("%zb", &s);
201 double d1 = 0.;
202 scanf("%zu", &d1); // expected-warning-re{{format specifies type 'size_t *' (aka '{{.+}}') but the argument has type 'double *'}}
204 ssize_t ss = 0;
205 scanf("%zd", &s); // No warning.
207 double d2 = 0.;
208 scanf("%zd", &d2); // expected-warning-re{{format specifies type 'ssize_t *' (aka '{{.+}}') but the argument has type 'double *'}}
210 ssize_t sn = 0;
211 scanf("%zn", &sn); // No warning.
213 double d3 = 0.;
214 scanf("%zn", &d3); // expected-warning-re{{format specifies type 'ssize_t *' (aka '{{.+}}') but the argument has type 'double *'}}
217 void test_ptrdiff_t_types(void) {
218 __UNSIGNED_PTRDIFF_TYPE__ p1 = 0;
219 scanf("%tu", &p1); // No warning.
220 scanf("%tb", &p1);
222 double d1 = 0.;
223 scanf("%tu", &d1); // expected-warning-re{{format specifies type 'unsigned ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}}
225 ptrdiff_t p2 = 0;
226 scanf("%td", &p2); // No warning.
228 double d2 = 0.;
229 scanf("%td", &d2); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}}
231 ptrdiff_t p3 = 0;
232 scanf("%tn", &p3); // No warning.
234 double d3 = 0.;
235 scanf("%tn", &d3); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}}
238 void check_conditional_literal(char *s, int *i) {
239 scanf(0 ? "%s" : "%d", i); // no warning
240 scanf(1 ? "%s" : "%d", i); // expected-warning{{format specifies type 'char *'}}
241 scanf(0 ? "%d %d" : "%d", i); // no warning
242 scanf(1 ? "%d %d" : "%d", i); // expected-warning{{more '%' conversions than data arguments}}
243 scanf(0 ? "%d %d" : "%d", i, s); // expected-warning{{data argument not used}}
244 scanf(1 ? "%d %s" : "%d", i, s); // no warning
245 scanf(i ? "%d %s" : "%d", i, s); // no warning
246 scanf(i ? "%d" : "%d", i, s); // expected-warning{{data argument not used}}
247 scanf(i ? "%s" : "%d", s); // expected-warning{{format specifies type 'int *'}}