blob: d3a03adf61959f358b86be44e631a01fb15364b5 [file] [log] [blame]
Jordan Rose92303592012-09-08 04:00:03 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s
Ted Kremenek55301312010-07-16 02:11:34 +00002
Hans Wennborg507b5672012-01-17 09:30:38 +00003// Test that -Wformat=0 works:
4// RUN: %clang_cc1 -fsyntax-only -Werror -Wformat=0 %s
5
Hans Wennborgbb4189d2011-12-12 10:34:18 +00006#include <stdarg.h>
Ted Kremenek55301312010-07-16 02:11:34 +00007typedef __typeof(sizeof(int)) size_t;
8typedef struct _FILE FILE;
Ted Kremenek4407ea42010-07-20 20:04:47 +00009typedef __WCHAR_TYPE__ wchar_t;
Ted Kremenek55301312010-07-16 02:11:34 +000010
11int fscanf(FILE * restrict, const char * restrict, ...) ;
12int scanf(const char * restrict, ...) ;
Hans Wennborg8d5a4092011-12-12 18:33:02 +000013int sscanf(const char * restrict, const char * restrict, ...) ;
Hans Wennborgbb4189d2011-12-12 10:34:18 +000014int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2)));
15
16int vscanf(const char * restrict, va_list);
17int vfscanf(FILE * restrict, const char * restrict, va_list);
Hans Wennborg4951c672011-12-12 18:46:05 +000018int vsscanf(const char * restrict, const char * restrict, va_list);
Ted Kremenek55301312010-07-16 02:11:34 +000019
20void test(const char *s, int *i) {
21 scanf(s, i); // expected-warning{{ormat string is not a string literal}}
Ted Kremenekc6187282010-07-16 18:27:56 +000022 scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
23 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
Ted Kremenek0030be82010-07-16 20:49:01 +000024 scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
Ted Kremenek5c3e1b92010-07-19 19:47:40 +000025
26 unsigned short s_x;
27 scanf ("%" "hu" "\n", &s_x); // no-warning
Ted Kremenekce815422010-07-19 21:25:57 +000028 scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
Ted Kremenek6cd69422010-07-19 22:01:06 +000029 scanf("%%"); // no-warning
30 scanf("%%%1$d", i); // no-warning
31 scanf("%1$d%%", i); // no-warning
32 scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
33 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
34 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
35 scanf("%*d%1$d", i); // no-warning
Hans Wennborgb1ab2a82012-08-07 08:59:46 +000036
37 scanf("%s", (char*)0); // no-warning
38 scanf("%s", (volatile char*)0); // no-warning
39 scanf("%s", (signed char*)0); // no-warning
40 scanf("%s", (unsigned char*)0); // no-warning
41 scanf("%hhu", (signed char*)0); // no-warning
Ted Kremenek55301312010-07-16 02:11:34 +000042}
Ted Kremenek4407ea42010-07-20 20:04:47 +000043
44void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
45 scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
Hans Wennborgb1a5e092011-12-10 13:20:11 +000046 scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
Ted Kremenek4407ea42010-07-20 20:04:47 +000047 scanf("%ls", ws); // no-warning
48 scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
49}
Richard Trieu03cf7b72011-10-28 00:41:25 +000050
51// Test that the scanf call site is where the warning is attached. If the
52// format string is somewhere else, point to it in a note.
53void pr9751() {
54 int *i;
Hans Wennborgb1a5e092011-12-10 13:20:11 +000055 char str[100];
Richard Trieu03cf7b72011-10-28 00:41:25 +000056 const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
57 scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
58 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
59 const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
Hans Wennborgb1a5e092011-12-10 13:20:11 +000060 scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
61 scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
Jean-Daniel Dupascb197b02012-01-31 18:12:08 +000062 const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}}
63 scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}}
64 const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}}
65 scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}}
Richard Trieu03cf7b72011-10-28 00:41:25 +000066}
Hans Wennborgbb4189d2011-12-12 10:34:18 +000067
68void test_variants(int *i, const char *s, ...) {
69 FILE *f = 0;
70 char buf[100];
71
Ted Kremeneke7b9d432012-01-20 21:52:58 +000072 fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
73 sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
74 my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
Hans Wennborgbb4189d2011-12-12 10:34:18 +000075
76 va_list ap;
77 va_start(ap, s);
78
79 vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
80 vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
81 vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
82}
Hans Wennborg23926bd2011-12-15 10:25:47 +000083
Ted Kremenek91398922012-01-20 22:11:52 +000084void test_scanlist(int *ip, char *sp, wchar_t *ls) {
Ted Kremeneke7b9d432012-01-20 21:52:58 +000085 scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
Hans Wennborgfd950872012-01-12 15:07:16 +000086 scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}}
Ted Kremenek91398922012-01-20 22:11:52 +000087 scanf("%l[xyx]", ls); // no-warning
88 scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}}
Hans Wennborgdf51ee62014-04-29 19:42:27 +000089
90 // PR19559
91 scanf("%[]% ]", sp); // no-warning
92 scanf("%[^]% ]", sp); // no-warning
93 scanf("%[a^]% ]", sp); // expected-warning {{invalid conversion specifier ' '}}
Hans Wennborg32f115f2012-01-12 14:44:54 +000094}
95
Hans Wennborg6073e312012-01-12 17:11:12 +000096void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) {
Hans Wennborg23926bd2011-12-15 10:25:47 +000097 /* Make sure "%a" gets parsed as a conversion specifier for float,
98 * even when followed by an 's', 'S' or '[', which would cause it to be
99 * parsed as a length modifier in C90. */
Ted Kremeneke7b9d432012-01-20 21:52:58 +0000100 scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
101 scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}}
102 scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
Hans Wennborg6073e312012-01-12 17:11:12 +0000103
104 // Test that the 'm' length modifier is only allowed with s, S, c, C or [.
105 // TODO: Warn that 'm' is an extension.
106 scanf("%ms", sp); // No warning.
107 scanf("%mS", lsp); // No warning.
108 scanf("%mc", sp); // No warning.
109 scanf("%mC", lsp); // No warning.
110 scanf("%m[abc]", sp); // No warning.
111 scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}}
112
113 // Test argument type check for the 'm' length modifier.
Ted Kremeneke7b9d432012-01-20 21:52:58 +0000114 scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
Alp Toker6ed72512013-12-14 01:07:05 +0000115 scanf("%mS", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
Ted Kremeneke7b9d432012-01-20 21:52:58 +0000116 scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
Alp Toker6ed72512013-12-14 01:07:05 +0000117 scanf("%mC", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
Ted Kremeneke7b9d432012-01-20 21:52:58 +0000118 scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
Hans Wennborg23926bd2011-12-15 10:25:47 +0000119}
Ted Kremenek91398922012-01-20 22:11:52 +0000120
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000121void test_quad(int *x, long long *llx) {
122 scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
123 scanf("%qd", llx); // no-warning
124}
Hans Wennborgebcd1c72012-07-30 17:11:32 +0000125
126void test_writeback(int *x) {
127 scanf("%n", (void*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'void *'}}
128 scanf("%n %c", x, x); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
Hans Wennborgabc1e222012-08-07 09:13:19 +0000129
130 scanf("%hhn", (signed char*)0); // no-warning
131 scanf("%hhn", (char*)0); // no-warning
132 scanf("%hhn", (unsigned char*)0); // no-warning
133 scanf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
134
135 scanf("%hn", (short*)0); // no-warning
136 scanf("%hn", (unsigned short*)0); // no-warning
137 scanf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
138
139 scanf("%n", (int*)0); // no-warning
140 scanf("%n", (unsigned int*)0); // no-warning
141 scanf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
142
143 scanf("%ln", (long*)0); // no-warning
144 scanf("%ln", (unsigned long*)0); // no-warning
145 scanf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
146
147 scanf("%lln", (long long*)0); // no-warning
148 scanf("%lln", (unsigned long long*)0); // no-warning
149 scanf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
150
151 scanf("%qn", (long long*)0); // no-warning
152 scanf("%qn", (unsigned long long*)0); // no-warning
153 scanf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
154
Hans Wennborgebcd1c72012-07-30 17:11:32 +0000155}
Hans Wennborg16250c72012-07-31 16:37:47 +0000156
157void test_qualifiers(const int *cip, volatile int* vip,
158 const char *ccp, volatile char* vcp,
159 const volatile int *cvip) {
160 scanf("%d", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
161 scanf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
162 scanf("%s", ccp); // expected-warning{{format specifies type 'char *' but the argument has type 'const char *'}}
163 scanf("%d", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
164
165 scanf("%d", vip); // No warning.
166 scanf("%n", vip); // No warning.
167 scanf("%c", vcp); // No warning.
168
169 typedef int* ip_t;
170 typedef const int* cip_t;
171 scanf("%d", (ip_t)0); // No warning.
172 scanf("%d", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
173}