blob: c0f6b0becb256cf019063b096e851b3265c222e8 [file] [log] [blame]
Ted Kremenek1e4c33a2010-07-16 02:11:34 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s
2
Hans Wennborg439ddaa2011-12-12 10:34:18 +00003#include <stdarg.h>
Ted Kremenek1e4c33a2010-07-16 02:11:34 +00004typedef __typeof(sizeof(int)) size_t;
5typedef struct _FILE FILE;
Ted Kremenek1e51c202010-07-20 20:04:47 +00006typedef __WCHAR_TYPE__ wchar_t;
Ted Kremenek1e4c33a2010-07-16 02:11:34 +00007
8int fscanf(FILE * restrict, const char * restrict, ...) ;
9int scanf(const char * restrict, ...) ;
Hans Wennborg439ddaa2011-12-12 10:34:18 +000010int sscanf(char * restrict, const char * restrict, ...) ;
11int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2)));
12
13int vscanf(const char * restrict, va_list);
14int vfscanf(FILE * restrict, const char * restrict, va_list);
15int vsscanf(char * restrict, const char * restrict, va_list);
Ted Kremenek1e4c33a2010-07-16 02:11:34 +000016
17void test(const char *s, int *i) {
18 scanf(s, i); // expected-warning{{ormat string is not a string literal}}
Ted Kremenek32d09002010-07-16 18:27:56 +000019 scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
20 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
Ted Kremenekbb09d1e2010-07-16 20:49:01 +000021 scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
Ted Kremenekbe86ecc2010-07-19 19:47:40 +000022
23 unsigned short s_x;
24 scanf ("%" "hu" "\n", &s_x); // no-warning
Ted Kremenekc09b6a52010-07-19 21:25:57 +000025 scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
Ted Kremenekbaa40062010-07-19 22:01:06 +000026 scanf("%%"); // no-warning
27 scanf("%%%1$d", i); // no-warning
28 scanf("%1$d%%", i); // no-warning
29 scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
30 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
31 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
32 scanf("%*d%1$d", i); // no-warning
Ted Kremenek1e4c33a2010-07-16 02:11:34 +000033}
Ted Kremenek1e51c202010-07-20 20:04:47 +000034
35void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
36 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 +000037 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 +000038 scanf("%ls", ws); // no-warning
39 scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
40}
Richard Trieu55733de2011-10-28 00:41:25 +000041
42// Test that the scanf call site is where the warning is attached. If the
43// format string is somewhere else, point to it in a note.
44void pr9751() {
45 int *i;
Hans Wennborg6fcd9322011-12-10 13:20:11 +000046 char str[100];
Richard Trieu55733de2011-10-28 00:41:25 +000047 const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
48 scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
49 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
50 const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
Hans Wennborg6fcd9322011-12-10 13:20:11 +000051 scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
52 scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
Richard Trieu55733de2011-10-28 00:41:25 +000053}
Hans Wennborg439ddaa2011-12-12 10:34:18 +000054
55void test_variants(int *i, const char *s, ...) {
56 FILE *f = 0;
57 char buf[100];
58
59 fscanf(f, "%ld", i); // expected-warning{{conversion specifies type 'long *' but the argument has type 'int *'}}
60 sscanf(buf, "%ld", i); // expected-warning{{conversion specifies type 'long *' but the argument has type 'int *'}}
61 my_scanf("%ld", i); // expected-warning{{conversion specifies type 'long *' but the argument has type 'int *'}}
62
63 va_list ap;
64 va_start(ap, s);
65
66 vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
67 vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
68 vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
69}