summaryrefslogtreecommitdiffstats
path: root/test/Sema/format-strings.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/Sema/format-strings.c')
-rw-r--r--test/Sema/format-strings.c81
1 files changed, 73 insertions, 8 deletions
diff --git a/test/Sema/format-strings.c b/test/Sema/format-strings.c
index 20e4dcd..f1fa658 100644
--- a/test/Sema/format-strings.c
+++ b/test/Sema/format-strings.c
@@ -40,18 +40,22 @@ void check_string_literal( FILE* fp, const char* s, char *buf, ... ) {
// rdar://6079877
printf("abc"
- "%*d", (unsigned) 1, 1); // expected-warning {{field width should have type 'int'}}
+ "%*d", 1, 1); // no-warning
printf("abc\
def"
- "%*d", (unsigned) 1, 1); // expected-warning {{field width should have type 'int'}}
-
+ "%*d", 1, 1); // no-warning
+
+ // <rdar://problem/6079850>, allow 'unsigned' (instead of 'int') to be used for both
+ // the field width and precision. This deviates from C99, but is reasonably safe
+ // and is also accepted by GCC.
+ printf("%*d", (unsigned) 1, 1); // no-warning
}
void check_conditional_literal(const char* s, int i) {
printf(i == 1 ? "yes" : "no"); // no-warning
printf(i == 0 ? (i == 1 ? "yes" : "no") : "dont know"); // no-warning
printf(i == 0 ? (i == 1 ? s : "no") : "dont know"); // expected-warning{{format string is not a string literal}}
- printf("yes" ?: "no %d", 1); // expected-warning{{more data arguments than '%' conversions}}
+ printf("yes" ?: "no %d", 1); // expected-warning{{more data arguments than format specifiers}}
}
void check_writeback_specifier()
@@ -65,10 +69,10 @@ void check_writeback_specifier()
void check_invalid_specifier(FILE* fp, char *buf)
{
- printf("%s%lb%d","unix",10,20); // expected-warning {{lid conversion '%lb'}}
- fprintf(fp,"%%%l"); // expected-warning {{lid conversion '%l'}}
- sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // no-warning
- snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning {{sion '%;'}}
+ printf("%s%lb%d","unix",10,20); // expected-warning {{invalid conversion specifier 'b'}}
+ fprintf(fp,"%%%l"); // expected-warning {{incomplete format specifier}}
+ sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // expected-warning{{conversion specifies type 'long' but the argument has type 'int'}}
+ snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning{{conversion specifies type 'long' but the argument has type 'int'}} expected-warning {{invalid conversion specifier ';'}}
}
void check_null_char_string(char* b)
@@ -137,5 +141,66 @@ void test9(char *P) {
void torture(va_list v8) {
vprintf ("%*.*d", v8); // no-warning
+
+}
+
+void test10(int x, float f, int i, long long lli) {
+ printf("%@", 12); // expected-warning{{invalid conversion specifier '@'}}
+ printf("\0"); // expected-warning{{format string contains '\0' within the string body}}
+ printf("xs\0"); // expected-warning{{format string contains '\0' within the string body}}
+ printf("%*d\n"); // expected-warning{{'*' specified field width is missing a matching 'int' argument}}
+ printf("%*.*d\n", x); // expected-warning{{'.*' specified field precision is missing a matching 'int' argument}}
+ printf("%*d\n", f, x); // expected-warning{{field width should have type 'int', but argument has type 'double'}}
+ printf("%*.*d\n", x, f, x); // expected-warning{{field precision should have type 'int', but argument has type 'double'}}
+ printf("%**\n"); // expected-warning{{invalid conversion specifier '*'}}
+ printf("%n", &i); // expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}
+ printf("%d%d\n", x); // expected-warning{{more '%' conversions than data arguments}}
+ printf("%d\n", x, x); // expected-warning{{more data arguments than format specifiers}}
+ printf("%W%d%Z\n", x, x, x); // expected-warning{{invalid conversion specifier 'W'}} expected-warning{{invalid conversion specifier 'Z'}}
+ printf("%"); // expected-warning{{incomplete format specifier}}
+ printf("%.d", x); // no-warning
+ printf("%.", x); // expected-warning{{incomplete format specifier}}
+ printf("%f", 4); // expected-warning{{conversion specifies type 'double' but the argument has type 'int'}}
+ printf("%qd", lli);
+ printf("hhX %hhX", (unsigned char)10); // no-warning
+ printf("llX %llX", (long long) 10); // no-warning
+ // This is fine, because there is an implicit conversion to an int.
+ printf("%d", (unsigned char) 10); // no-warning
+ printf("%d", (long long) 10); // expected-warning{{conversion specifies type 'int' but the argument has type 'long long'}}
+ printf("%Lf\n", (long double) 1.0); // no-warning
+ printf("%f\n", (long double) 1.0); // expected-warning{{conversion specifies type 'double' but the argument has type 'long double'}}
+}
+
+void test11(void *p, char *s) {
+ printf("%p", p); // no-warning
+ printf("%.4p", p); // expected-warning{{precision used in 'p' conversion specifier (where it has no meaning)}}
+ printf("%+p", p); // expected-warning{{flag '+' results in undefined behavior in 'p' conversion specifier}}
+ printf("% p", p); // expected-warning{{flag ' ' results in undefined behavior in 'p' conversion specifier}}
+ printf("%0p", p); // expected-warning{{flag '0' results in undefined behavior in 'p' conversion specifier}}
+ printf("%s", s); // no-warning
+ printf("%+s", p); // expected-warning{{flag '+' results in undefined behavior in 's' conversion specifier}}
+ printf("% s", p); // expected-warning{{flag ' ' results in undefined behavior in 's' conversion specifier}}
+ printf("%0s", p); // expected-warning{{flag '0' results in undefined behavior in 's' conversion specifier}}
+}
+
+void test12(char *b) {
+ unsigned char buf[4];
+ printf ("%.4s\n", buf); // no-warning
+ printf ("%.4s\n", &buf); // expected-warning{{conversion specifies type 'char *' but the argument has type 'unsigned char (*)[4]'}}
+
+ // Verify that we are checking asprintf
+ asprintf(&b, "%d", "asprintf"); // expected-warning{{conversion specifies type 'int' but the argument has type 'char *'}}
+}
+
+typedef struct __aslclient *aslclient;
+typedef struct __aslmsg *aslmsg;
+int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __attribute__((__format__ (__printf__, 4, 5)));
+void test_asl(aslclient asl) {
+ // Test case from <rdar://problem/7341605>.
+ asl_log(asl, 0, 3, "Error: %m"); // no-warning
+ asl_log(asl, 0, 3, "Error: %W"); // expected-warning{{invalid conversion specifier 'W'}}
}
+// <rdar://problem/7595366>
+typedef enum { A } int_t;
+void f0(int_t x) { printf("%d\n", x); }
OpenPOWER on IntegriCloud