blob: ee559daef6151fb36628403939c5097c987f2752 [file] [log] [blame]
Ted Kremenek1e4c33a2010-07-16 02:11:34 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s
2
3typedef __typeof(sizeof(int)) size_t;
4typedef struct _FILE FILE;
Ted Kremenek1e51c202010-07-20 20:04:47 +00005typedef __WCHAR_TYPE__ wchar_t;
Ted Kremenek1e4c33a2010-07-16 02:11:34 +00006
7int fscanf(FILE * restrict, const char * restrict, ...) ;
8int scanf(const char * restrict, ...) ;
9int sscanf(const char * restrict, const char * restrict, ...) ;
10
11void test(const char *s, int *i) {
12 scanf(s, i); // expected-warning{{ormat string is not a string literal}}
Ted Kremenek32d09002010-07-16 18:27:56 +000013 scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
14 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
Ted Kremenekbb09d1e2010-07-16 20:49:01 +000015 scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
Ted Kremenekbe86ecc2010-07-19 19:47:40 +000016
17 unsigned short s_x;
18 scanf ("%" "hu" "\n", &s_x); // no-warning
Ted Kremenekc09b6a52010-07-19 21:25:57 +000019 scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
Ted Kremenekbaa40062010-07-19 22:01:06 +000020 scanf("%%"); // no-warning
21 scanf("%%%1$d", i); // no-warning
22 scanf("%1$d%%", i); // no-warning
23 scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
24 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
25 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
26 scanf("%*d%1$d", i); // no-warning
Ted Kremenek1e4c33a2010-07-16 02:11:34 +000027}
Ted Kremenek1e51c202010-07-20 20:04:47 +000028
29void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
30 scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
31 scanf("%1$zp", p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
32 scanf("%ls", ws); // no-warning
33 scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
34}
Richard Trieu55733de2011-10-28 00:41:25 +000035
36// Test that the scanf call site is where the warning is attached. If the
37// format string is somewhere else, point to it in a note.
38void pr9751() {
39 int *i;
40 const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
41 scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
42 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
43 const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
44 scanf(kFormat2, &i); // expected-warning{{no closing ']' for '%[' in scanf format string}}
45 scanf("%[", &i); // expected-warning{{no closing ']' for '%[' in scanf format string}}
46}