blob: d89dbc494b71d6c69345266cce9e1d18bf690034 [file] [log] [blame]
Hans Wennborg37969b72012-01-12 17:11:12 +00001// RUN: %clang_cc1 -fsyntax-only -verify -triple i386-apple-darwin9 -Wformat-nonliteral %s
Ted Kremenek1e4c33a2010-07-16 02:11:34 +00002
Hans Wennborgc8769462012-01-17 09:30:38 +00003// Test that -Wformat=0 works:
4// RUN: %clang_cc1 -fsyntax-only -Werror -Wformat=0 %s
5
Hans Wennborg439ddaa2011-12-12 10:34:18 +00006#include <stdarg.h>
Ted Kremenek1e4c33a2010-07-16 02:11:34 +00007typedef __typeof(sizeof(int)) size_t;
8typedef struct _FILE FILE;
Ted Kremenek1e51c202010-07-20 20:04:47 +00009typedef __WCHAR_TYPE__ wchar_t;
Ted Kremenek1e4c33a2010-07-16 02:11:34 +000010
11int fscanf(FILE * restrict, const char * restrict, ...) ;
12int scanf(const char * restrict, ...) ;
Hans Wennborgd95a8ab02011-12-12 18:33:02 +000013int sscanf(const char * restrict, const char * restrict, ...) ;
Hans Wennborg439ddaa2011-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 Wennborgc08e6182011-12-12 18:46:05 +000018int vsscanf(const char * restrict, const char * restrict, va_list);
Ted Kremenek1e4c33a2010-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 Kremenek32d09002010-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 Kremenekbb09d1e2010-07-16 20:49:01 +000024 scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
Ted Kremenekbe86ecc2010-07-19 19:47:40 +000025
26 unsigned short s_x;
27 scanf ("%" "hu" "\n", &s_x); // no-warning
Ted Kremenekc09b6a52010-07-19 21:25:57 +000028 scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
Ted Kremenekbaa40062010-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
Ted Kremenek1e4c33a2010-07-16 02:11:34 +000036}
Ted Kremenek1e51c202010-07-20 20:04:47 +000037
38void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
39 scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
Hans Wennborg6fcd9322011-12-10 13:20:11 +000040 scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
Ted Kremenek1e51c202010-07-20 20:04:47 +000041 scanf("%ls", ws); // no-warning
42 scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
43}
Richard Trieu55733de2011-10-28 00:41:25 +000044
45// Test that the scanf call site is where the warning is attached. If the
46// format string is somewhere else, point to it in a note.
47void pr9751() {
48 int *i;
Hans Wennborg6fcd9322011-12-10 13:20:11 +000049 char str[100];
Richard Trieu55733de2011-10-28 00:41:25 +000050 const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
51 scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
52 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
53 const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
Hans Wennborg6fcd9322011-12-10 13:20:11 +000054 scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
55 scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
Richard Trieu55733de2011-10-28 00:41:25 +000056}
Hans Wennborg439ddaa2011-12-12 10:34:18 +000057
58void test_variants(int *i, const char *s, ...) {
59 FILE *f = 0;
60 char buf[100];
61
Ted Kremenekce506ae2012-01-20 21:52:58 +000062 fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
63 sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
64 my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
Hans Wennborg439ddaa2011-12-12 10:34:18 +000065
66 va_list ap;
67 va_start(ap, s);
68
69 vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
70 vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
71 vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
72}
Hans Wennborgd02deeb2011-12-15 10:25:47 +000073
Ted Kremenekef1440b2012-01-20 22:11:52 +000074void test_scanlist(int *ip, char *sp, wchar_t *ls) {
Ted Kremenekce506ae2012-01-20 21:52:58 +000075 scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
Hans Wennborg28058d12012-01-12 15:07:16 +000076 scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}}
Ted Kremenekef1440b2012-01-20 22:11:52 +000077 scanf("%l[xyx]", ls); // no-warning
78 scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}}
Hans Wennborg6de0b482012-01-12 14:44:54 +000079}
80
Hans Wennborg37969b72012-01-12 17:11:12 +000081void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) {
Hans Wennborgd02deeb2011-12-15 10:25:47 +000082 /* Make sure "%a" gets parsed as a conversion specifier for float,
83 * even when followed by an 's', 'S' or '[', which would cause it to be
84 * parsed as a length modifier in C90. */
Ted Kremenekce506ae2012-01-20 21:52:58 +000085 scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
86 scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}}
87 scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
Hans Wennborg37969b72012-01-12 17:11:12 +000088
89 // Test that the 'm' length modifier is only allowed with s, S, c, C or [.
90 // TODO: Warn that 'm' is an extension.
91 scanf("%ms", sp); // No warning.
92 scanf("%mS", lsp); // No warning.
93 scanf("%mc", sp); // No warning.
94 scanf("%mC", lsp); // No warning.
95 scanf("%m[abc]", sp); // No warning.
96 scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}}
97
98 // Test argument type check for the 'm' length modifier.
Ted Kremenekce506ae2012-01-20 21:52:58 +000099 scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
100 scanf("%mS", fp); // expected-warning{{format specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}}
101 scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
102 scanf("%mC", fp); // expected-warning{{format specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}}
103 scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000104}
Ted Kremenekef1440b2012-01-20 22:11:52 +0000105
Ted Kremenek9d24c2c2012-01-24 21:29:54 +0000106void test_longlong(long long *x, unsigned long long *y) {
107 scanf("%Ld", y); // no-warning
108 scanf("%Lu", y); // no-warning
109 scanf("%Lx", y); // no-warning
110 scanf("%Ld", x); // no-warning
111 scanf("%Lu", x); // no-warning
112 scanf("%Lx", x); // no-warning
113 scanf("%Ls", "hello"); // expected-warning {{length modifier 'L' results in undefined behavior or no effect with 's' conversion specifier}}
114}
115