Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -verify %s |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdarg.h> |
| 5 | |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 6 | char * global_fmt; |
| 7 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 8 | void check_string_literal( FILE* fp, const char* s, char *buf, ... ) { |
| 9 | |
| 10 | char * b; |
| 11 | va_list ap; |
| 12 | va_start(ap,buf); |
| 13 | |
| 14 | printf(s); // expected-warning {{format string is not a string literal}} |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 15 | vprintf(s,ap); // // no-warning |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 16 | fprintf(fp,s); // expected-warning {{format string is not a string literal}} |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 17 | vfprintf(fp,s,ap); // no-warning |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 18 | asprintf(&b,s); // expected-warning {{format string is not a string lit}} |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 19 | vasprintf(&b,s,ap); // no-warning |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 20 | sprintf(buf,s); // expected-warning {{format string is not a string literal}} |
| 21 | snprintf(buf,2,s); // expected-warning {{format string is not a string lit}} |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 22 | vsprintf(buf,s,ap); // no-warning |
| 23 | vsnprintf(buf,2,s,ap); // no-warning |
| 24 | vsnprintf(buf,2,global_fmt,ap); // expected-warning {{format string is not a string literal}} |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 27 | void check_writeback_specifier() |
| 28 | { |
| 29 | int x; |
| 30 | char *b; |
| 31 | |
| 32 | printf("%n",&x); // expected-warning {{'%n' in format string discouraged}} |
| 33 | sprintf(b,"%d%%%n",1, &x); // expected-warning {{'%n' in format string dis}} |
| 34 | } |
| 35 | |
| 36 | void check_invalid_specifier(FILE* fp, char *buf) |
| 37 | { |
| 38 | printf("%s%lb%d","unix",10,20); // expected-warning {{lid conversion '%lb'}} |
| 39 | fprintf(fp,"%%%l"); // expected-warning {{lid conversion '%l'}} |
| 40 | sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // no-warning |
| 41 | snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning {{sion '%;'}} |
| 42 | } |
| 43 | |
| 44 | void check_null_char_string(char* b) |
| 45 | { |
| 46 | printf("\0this is bogus%d",1); // expected-warning {{string contains '\0'}} |
| 47 | snprintf(b,10,"%%%%%d\0%d",1,2); // expected-warning {{string contains '\0'}} |
| 48 | printf("%\0d",1); // expected-warning {{string contains '\0'}} |
| 49 | } |
| 50 | |
Anders Carlsson | 6eda8c9 | 2007-10-12 17:48:41 +0000 | [diff] [blame] | 51 | void check_empty_format_string(char* buf, ...) |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 52 | { |
| 53 | va_list ap; |
| 54 | va_start(ap,buf); |
| 55 | vprintf("",ap); // expected-warning {{format string is empty}} |
| 56 | sprintf(buf,""); // expected-warning {{format string is empty}} |
| 57 | } |
| 58 | |
Anders Carlsson | 6eda8c9 | 2007-10-12 17:48:41 +0000 | [diff] [blame] | 59 | void check_wide_string(char* b, ...) |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 60 | { |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 61 | va_list ap; |
| 62 | va_start(ap,b); |
| 63 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 64 | printf(L"foo %d",2); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}} |
| 65 | vasprintf(&b,L"bar %d",ap); // expected-warning {{incompatible pointer types}}, expected-warning {{should not be a wide string}} |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 66 | } |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 67 | |
| 68 | void check_asterisk_precision_width(int x) { |
| 69 | printf("%*d"); // expected-warning {{'*' specified field width is missing a matching 'int' argument}} |
| 70 | printf("%.*d"); // expected-warning {{'.*' specified field precision is missing a matching 'int' argument}} |
| 71 | printf("%*d",12,x); // no-warning |
| 72 | printf("%*d","foo",x); // expected-warning {{field width should have type 'int', but argument has type 'char *'}} |
| 73 | printf("%.*d","foo",x); // expected-warning {{field precision should have type 'int', but argument has type 'char *'}} |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 74 | } |