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