2012-01-13 01:11:12 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -triple i386-apple-darwin9 -Wformat-nonliteral %s
|
2010-07-16 10:11:34 +08:00
|
|
|
|
2012-01-17 17:30:38 +08:00
|
|
|
// Test that -Wformat=0 works:
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Werror -Wformat=0 %s
|
|
|
|
|
2011-12-12 18:34:18 +08:00
|
|
|
#include <stdarg.h>
|
2010-07-16 10:11:34 +08:00
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
|
|
|
typedef struct _FILE FILE;
|
2010-07-21 04:04:47 +08:00
|
|
|
typedef __WCHAR_TYPE__ wchar_t;
|
2010-07-16 10:11:34 +08:00
|
|
|
|
|
|
|
int fscanf(FILE * restrict, const char * restrict, ...) ;
|
|
|
|
int scanf(const char * restrict, ...) ;
|
2011-12-13 02:33:02 +08:00
|
|
|
int sscanf(const char * restrict, const char * restrict, ...) ;
|
2011-12-12 18:34:18 +08:00
|
|
|
int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2)));
|
|
|
|
|
|
|
|
int vscanf(const char * restrict, va_list);
|
|
|
|
int vfscanf(FILE * restrict, const char * restrict, va_list);
|
2011-12-13 02:46:05 +08:00
|
|
|
int vsscanf(const char * restrict, const char * restrict, va_list);
|
2010-07-16 10:11:34 +08:00
|
|
|
|
|
|
|
void test(const char *s, int *i) {
|
|
|
|
scanf(s, i); // expected-warning{{ormat string is not a string literal}}
|
2010-07-17 02:27:56 +08:00
|
|
|
scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
|
|
|
|
scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
|
2010-07-17 04:49:01 +08:00
|
|
|
scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
|
2010-07-20 03:47:40 +08:00
|
|
|
|
|
|
|
unsigned short s_x;
|
|
|
|
scanf ("%" "hu" "\n", &s_x); // no-warning
|
2010-07-20 05:25:57 +08:00
|
|
|
scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
|
2010-07-20 06:01:06 +08:00
|
|
|
scanf("%%"); // no-warning
|
|
|
|
scanf("%%%1$d", i); // no-warning
|
|
|
|
scanf("%1$d%%", i); // no-warning
|
|
|
|
scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
|
|
|
|
scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
|
|
|
|
scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
|
|
|
|
scanf("%*d%1$d", i); // no-warning
|
2010-07-16 10:11:34 +08:00
|
|
|
}
|
2010-07-21 04:04:47 +08:00
|
|
|
|
|
|
|
void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
|
|
|
|
scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
|
2011-12-10 21:20:11 +08:00
|
|
|
scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
|
2010-07-21 04:04:47 +08:00
|
|
|
scanf("%ls", ws); // no-warning
|
|
|
|
scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
|
|
|
|
}
|
2011-10-28 08:41:25 +08:00
|
|
|
|
|
|
|
// Test that the scanf call site is where the warning is attached. If the
|
|
|
|
// format string is somewhere else, point to it in a note.
|
|
|
|
void pr9751() {
|
|
|
|
int *i;
|
2011-12-10 21:20:11 +08:00
|
|
|
char str[100];
|
2011-10-28 08:41:25 +08:00
|
|
|
const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
|
|
|
|
scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
|
|
|
|
scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
|
|
|
|
const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
|
2011-12-10 21:20:11 +08:00
|
|
|
scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
|
|
|
|
scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
|
2012-02-01 02:12:08 +08:00
|
|
|
const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}}
|
|
|
|
scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}}
|
|
|
|
const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}}
|
|
|
|
scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}}
|
2011-10-28 08:41:25 +08:00
|
|
|
}
|
2011-12-12 18:34:18 +08:00
|
|
|
|
|
|
|
void test_variants(int *i, const char *s, ...) {
|
|
|
|
FILE *f = 0;
|
|
|
|
char buf[100];
|
|
|
|
|
2012-01-21 05:52:58 +08:00
|
|
|
fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
|
|
|
|
sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
|
|
|
|
my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
|
2011-12-12 18:34:18 +08:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
|
|
|
|
|
|
|
vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
|
|
|
|
vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
|
|
|
|
vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
|
|
|
|
}
|
2011-12-15 18:25:47 +08:00
|
|
|
|
2012-01-21 06:11:52 +08:00
|
|
|
void test_scanlist(int *ip, char *sp, wchar_t *ls) {
|
2012-01-21 05:52:58 +08:00
|
|
|
scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
|
2012-01-12 23:07:16 +08:00
|
|
|
scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}}
|
2012-01-21 06:11:52 +08:00
|
|
|
scanf("%l[xyx]", ls); // no-warning
|
|
|
|
scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}}
|
2012-01-12 22:44:54 +08:00
|
|
|
}
|
|
|
|
|
2012-01-13 01:11:12 +08:00
|
|
|
void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) {
|
2011-12-15 18:25:47 +08:00
|
|
|
/* Make sure "%a" gets parsed as a conversion specifier for float,
|
|
|
|
* even when followed by an 's', 'S' or '[', which would cause it to be
|
|
|
|
* parsed as a length modifier in C90. */
|
2012-01-21 05:52:58 +08:00
|
|
|
scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
|
|
|
|
scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}}
|
|
|
|
scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
|
2012-01-13 01:11:12 +08:00
|
|
|
|
|
|
|
// Test that the 'm' length modifier is only allowed with s, S, c, C or [.
|
|
|
|
// TODO: Warn that 'm' is an extension.
|
|
|
|
scanf("%ms", sp); // No warning.
|
|
|
|
scanf("%mS", lsp); // No warning.
|
|
|
|
scanf("%mc", sp); // No warning.
|
|
|
|
scanf("%mC", lsp); // No warning.
|
|
|
|
scanf("%m[abc]", sp); // No warning.
|
|
|
|
scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}}
|
|
|
|
|
|
|
|
// Test argument type check for the 'm' length modifier.
|
2012-01-21 05:52:58 +08:00
|
|
|
scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
|
|
|
|
scanf("%mS", fp); // expected-warning{{format specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}}
|
|
|
|
scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
|
|
|
|
scanf("%mC", fp); // expected-warning{{format specifies type 'wchar_t **' (aka 'int **') but the argument has type 'float *'}}
|
|
|
|
scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
|
2011-12-15 18:25:47 +08:00
|
|
|
}
|
2012-01-21 06:11:52 +08:00
|
|
|
|
2012-01-25 05:29:54 +08:00
|
|
|
void test_longlong(long long *x, unsigned long long *y) {
|
|
|
|
scanf("%Ld", y); // no-warning
|
|
|
|
scanf("%Lu", y); // no-warning
|
|
|
|
scanf("%Lx", y); // no-warning
|
|
|
|
scanf("%Ld", x); // no-warning
|
|
|
|
scanf("%Lu", x); // no-warning
|
|
|
|
scanf("%Lx", x); // no-warning
|
|
|
|
scanf("%Ls", "hello"); // expected-warning {{length modifier 'L' results in undefined behavior or no effect with 's' conversion specifier}}
|
|
|
|
}
|
|
|
|
|