Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s |
| 2 | |
| 3 | typedef __typeof(sizeof(int)) size_t; |
| 4 | typedef struct _FILE FILE; |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 5 | typedef __WCHAR_TYPE__ wchar_t; |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 6 | |
| 7 | int fscanf(FILE * restrict, const char * restrict, ...) ; |
| 8 | int scanf(const char * restrict, ...) ; |
| 9 | int sscanf(const char * restrict, const char * restrict, ...) ; |
| 10 | |
| 11 | void test(const char *s, int *i) { |
| 12 | scanf(s, i); // expected-warning{{ormat string is not a string literal}} |
Ted Kremenek | 32d0900 | 2010-07-16 18:27:56 +0000 | [diff] [blame] | 13 | 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 Kremenek | bb09d1e | 2010-07-16 20:49:01 +0000 | [diff] [blame] | 15 | scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
Ted Kremenek | be86ecc | 2010-07-19 19:47:40 +0000 | [diff] [blame] | 16 | |
| 17 | unsigned short s_x; |
| 18 | scanf ("%" "hu" "\n", &s_x); // no-warning |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 19 | scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}} |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 20 | 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 Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 27 | } |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 28 | |
| 29 | void 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 Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 35 | |
| 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. |
| 38 | void 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 | } |