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 | c876946 | 2012-01-17 09:30:38 +0000 | [diff] [blame] | 3 | // Test that -Wformat=0 works: |
| 4 | // RUN: %clang_cc1 -fsyntax-only -Werror -Wformat=0 %s |
| 5 | |
Hans Wennborg | 439ddaa | 2011-12-12 10:34:18 +0000 | [diff] [blame] | 6 | #include <stdarg.h> |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 7 | typedef __typeof(sizeof(int)) size_t; |
| 8 | typedef struct _FILE FILE; |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 9 | typedef __WCHAR_TYPE__ wchar_t; |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 10 | |
| 11 | int fscanf(FILE * restrict, const char * restrict, ...) ; |
| 12 | int scanf(const char * restrict, ...) ; |
Hans Wennborg | d95a8ab0 | 2011-12-12 18:33:02 +0000 | [diff] [blame] | 13 | int sscanf(const char * restrict, const char * restrict, ...) ; |
Hans Wennborg | 439ddaa | 2011-12-12 10:34:18 +0000 | [diff] [blame] | 14 | int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2))); |
| 15 | |
| 16 | int vscanf(const char * restrict, va_list); |
| 17 | int vfscanf(FILE * restrict, const char * restrict, va_list); |
Hans Wennborg | c08e618 | 2011-12-12 18:46:05 +0000 | [diff] [blame] | 18 | int vsscanf(const char * restrict, const char * restrict, va_list); |
Ted Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 19 | |
| 20 | void test(const char *s, int *i) { |
| 21 | scanf(s, i); // expected-warning{{ormat string is not a string literal}} |
Ted Kremenek | 32d0900 | 2010-07-16 18:27:56 +0000 | [diff] [blame] | 22 | 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 Kremenek | bb09d1e | 2010-07-16 20:49:01 +0000 | [diff] [blame] | 24 | 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] | 25 | |
| 26 | unsigned short s_x; |
| 27 | scanf ("%" "hu" "\n", &s_x); // no-warning |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 28 | scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}} |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 29 | 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 Kremenek | 1e4c33a | 2010-07-16 02:11:34 +0000 | [diff] [blame] | 36 | } |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 37 | |
| 38 | void 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 Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 40 | 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] | 41 | scanf("%ls", ws); // no-warning |
| 42 | scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}} |
| 43 | } |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 44 | |
| 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. |
| 47 | void pr9751() { |
| 48 | int *i; |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 49 | char str[100]; |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 50 | 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 Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 54 | scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 55 | scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 56 | const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}} |
| 57 | scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}} |
| 58 | const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}} |
| 59 | scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}} |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 60 | } |
Hans Wennborg | 439ddaa | 2011-12-12 10:34:18 +0000 | [diff] [blame] | 61 | |
| 62 | void test_variants(int *i, const char *s, ...) { |
| 63 | FILE *f = 0; |
| 64 | char buf[100]; |
| 65 | |
Ted Kremenek | ce506ae | 2012-01-20 21:52:58 +0000 | [diff] [blame] | 66 | fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 67 | sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 68 | my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
Hans Wennborg | 439ddaa | 2011-12-12 10:34:18 +0000 | [diff] [blame] | 69 | |
| 70 | va_list ap; |
| 71 | va_start(ap, s); |
| 72 | |
| 73 | vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 74 | vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 75 | vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 76 | } |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | ef1440b | 2012-01-20 22:11:52 +0000 | [diff] [blame] | 78 | void test_scanlist(int *ip, char *sp, wchar_t *ls) { |
Ted Kremenek | ce506ae | 2012-01-20 21:52:58 +0000 | [diff] [blame] | 79 | scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}} |
Hans Wennborg | 28058d1 | 2012-01-12 15:07:16 +0000 | [diff] [blame] | 80 | scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}} |
Ted Kremenek | ef1440b | 2012-01-20 22:11:52 +0000 | [diff] [blame] | 81 | scanf("%l[xyx]", ls); // no-warning |
| 82 | scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}} |
Hans Wennborg | 6de0b48 | 2012-01-12 14:44:54 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Hans Wennborg | 37969b7 | 2012-01-12 17:11:12 +0000 | [diff] [blame] | 85 | void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) { |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 86 | /* Make sure "%a" gets parsed as a conversion specifier for float, |
| 87 | * even when followed by an 's', 'S' or '[', which would cause it to be |
| 88 | * parsed as a length modifier in C90. */ |
Ted Kremenek | ce506ae | 2012-01-20 21:52:58 +0000 | [diff] [blame] | 89 | scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}} |
| 90 | scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}} |
| 91 | scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}} |
Hans Wennborg | 37969b7 | 2012-01-12 17:11:12 +0000 | [diff] [blame] | 92 | |
| 93 | // Test that the 'm' length modifier is only allowed with s, S, c, C or [. |
| 94 | // TODO: Warn that 'm' is an extension. |
| 95 | scanf("%ms", sp); // No warning. |
| 96 | scanf("%mS", lsp); // No warning. |
| 97 | scanf("%mc", sp); // No warning. |
| 98 | scanf("%mC", lsp); // No warning. |
| 99 | scanf("%m[abc]", sp); // No warning. |
| 100 | scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}} |
| 101 | |
| 102 | // Test argument type check for the 'm' length modifier. |
Ted Kremenek | ce506ae | 2012-01-20 21:52:58 +0000 | [diff] [blame] | 103 | scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}} |
| 104 | scanf("%mS", fp); // expected-warning{{format specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}} |
| 105 | scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}} |
| 106 | scanf("%mC", fp); // expected-warning{{format specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}} |
| 107 | scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}} |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 108 | } |
Ted Kremenek | ef1440b | 2012-01-20 22:11:52 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 9d24c2c | 2012-01-24 21:29:54 +0000 | [diff] [blame] | 110 | void test_longlong(long long *x, unsigned long long *y) { |
| 111 | scanf("%Ld", y); // no-warning |
| 112 | scanf("%Lu", y); // no-warning |
| 113 | scanf("%Lx", y); // no-warning |
| 114 | scanf("%Ld", x); // no-warning |
| 115 | scanf("%Lu", x); // no-warning |
| 116 | scanf("%Lx", x); // no-warning |
| 117 | scanf("%Ls", "hello"); // expected-warning {{length modifier 'L' results in undefined behavior or no effect with 's' conversion specifier}} |
| 118 | } |
| 119 | |
Hans Wennborg | 32addd5 | 2012-02-16 16:34:54 +0000 | [diff] [blame] | 120 | void test_quad(int *x, long long *llx) { |
| 121 | scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}} |
| 122 | scanf("%qd", llx); // no-warning |
| 123 | } |