Hans Wennborg | 37969b7 | 2012-01-12 17:11:12 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -triple i386-apple-darwin9 -Wformat-nonliteral %s |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 2 | |
Hans Wennborg | 439ddaa | 2011-12-12 10:34:18 +0000 | [diff] [blame] | 3 | #include <stdarg.h> |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 4 | typedef __typeof(sizeof(int)) size_t; |
| 5 | typedef struct _FILE FILE; |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 6 | typedef __WCHAR_TYPE__ wchar_t; |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 7 | |
| 8 | int fscanf(FILE * restrict, const char * restrict, ...) ; |
| 9 | int scanf(const char * restrict, ...) ; |
Hans Wennborg | d95a8ab0 | 2011-12-12 18:33:02 +0000 | [diff] [blame] | 10 | int sscanf(const char * restrict, const char * restrict, ...) ; |
Hans Wennborg | 439ddaa | 2011-12-12 10:34:18 +0000 | [diff] [blame] | 11 | int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2))); |
| 12 | |
| 13 | int vscanf(const char * restrict, va_list); |
| 14 | int vfscanf(FILE * restrict, const char * restrict, va_list); |
Hans Wennborg | c08e618 | 2011-12-12 18:46:05 +0000 | [diff] [blame] | 15 | int vsscanf(const char * restrict, const char * restrict, va_list); |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 16 | |
| 17 | void test(const char *s, int *i) { |
| 18 | scanf(s, i); // expected-warning{{ormat string is not a string literal}} |
Ted Kremenek | 32d0900 | 2010-07-16 18:27:56 +0000 | [diff] [blame] | 19 | 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 Kremenek | bb09d1e | 2010-07-16 20:49:01 +0000 | [diff] [blame] | 21 | 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] | 22 | |
| 23 | unsigned short s_x; |
| 24 | scanf ("%" "hu" "\n", &s_x); // no-warning |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 25 | scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}} |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 26 | 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 Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 33 | } |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 34 | |
| 35 | void 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 Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 37 | scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}} |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 38 | scanf("%ls", ws); // no-warning |
| 39 | scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}} |
| 40 | } |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 41 | |
| 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. |
| 44 | void pr9751() { |
| 45 | int *i; |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 46 | char str[100]; |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 47 | 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 Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 51 | 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 Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 53 | } |
Hans Wennborg | 439ddaa | 2011-12-12 10:34:18 +0000 | [diff] [blame] | 54 | |
| 55 | void 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 | } |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 70 | |
Hans Wennborg | 28058d1 | 2012-01-12 15:07:16 +0000 | [diff] [blame] | 71 | void test_scanlist(int *ip, char *sp) { |
Hans Wennborg | 6de0b48 | 2012-01-12 14:44:54 +0000 | [diff] [blame] | 72 | scanf("%[abc]", ip); // expected-warning{{conversion specifies type 'char *' but the argument has type 'int *'}} |
Hans Wennborg | 28058d1 | 2012-01-12 15:07:16 +0000 | [diff] [blame] | 73 | scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}} |
Hans Wennborg | 6de0b48 | 2012-01-12 14:44:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Hans Wennborg | 37969b7 | 2012-01-12 17:11:12 +0000 | [diff] [blame^] | 76 | void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) { |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 77 | /* Make sure "%a" gets parsed as a conversion specifier for float, |
| 78 | * even when followed by an 's', 'S' or '[', which would cause it to be |
| 79 | * parsed as a length modifier in C90. */ |
| 80 | scanf("%as", sp); // expected-warning{{conversion specifies type 'float *' but the argument has type 'char **'}} |
Hans Wennborg | 31b9a98 | 2011-12-15 11:43:45 +0000 | [diff] [blame] | 81 | scanf("%aS", lsp); // expected-warning{{conversion specifies type 'float *' but the argument has type 'wchar_t **'}} |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 82 | scanf("%a[bcd]", sp); // expected-warning{{conversion specifies type 'float *' but the argument has type 'char **'}} |
Hans Wennborg | 37969b7 | 2012-01-12 17:11:12 +0000 | [diff] [blame^] | 83 | |
| 84 | // Test that the 'm' length modifier is only allowed with s, S, c, C or [. |
| 85 | // TODO: Warn that 'm' is an extension. |
| 86 | scanf("%ms", sp); // No warning. |
| 87 | scanf("%mS", lsp); // No warning. |
| 88 | scanf("%mc", sp); // No warning. |
| 89 | scanf("%mC", lsp); // No warning. |
| 90 | scanf("%m[abc]", sp); // No warning. |
| 91 | scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}} |
| 92 | |
| 93 | // Test argument type check for the 'm' length modifier. |
| 94 | scanf("%ms", fp); // expected-warning{{conversion specifies type 'char **' but the argument has type 'float *'}} |
| 95 | scanf("%mS", fp); // expected-warning{{conversion specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}} |
| 96 | scanf("%mc", fp); // expected-warning{{conversion specifies type 'char **' but the argument has type 'float *'}} |
| 97 | scanf("%mC", fp); // expected-warning{{conversion specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}} |
| 98 | scanf("%m[abc]", fp); // expected-warning{{conversion specifies type 'char **' but the argument has type 'float *'}} |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 99 | } |