blob: 83947cf7c356a98dc8f99f3bd883706fcc2dfccf [file] [log] [blame]
Chris Lattner3b427b32007-10-11 00:18:28 +00001// RUN: clang -fsyntax-only -verify %s
Chris Lattner59907c42007-08-10 20:18:51 +00002
3#include <stdio.h>
4#include <stdarg.h>
5
6void check_string_literal( FILE* fp, const char* s, char *buf, ... ) {
7
8 char * b;
9 va_list ap;
10 va_start(ap,buf);
11
12 printf(s); // expected-warning {{format string is not a string literal}}
13 vprintf(s,ap); // expected-warning {{format string is not a string liter}}
14 fprintf(fp,s); // expected-warning {{format string is not a string literal}}
15 vfprintf(fp,s,ap); // expected-warning {{format string is not a string lit}}
16 asprintf(&b,s); // expected-warning {{format string is not a string lit}}
17 vasprintf(&b,s,ap); // expected-warning {{format string is not a string lit}}
18 sprintf(buf,s); // expected-warning {{format string is not a string literal}}
19 snprintf(buf,2,s); // expected-warning {{format string is not a string lit}}
20 vsprintf(buf,s,ap); // expected-warning {{format string is not a string lit}}
21 vsnprintf(buf,2,s,ap); // expected-warning {{mat string is not a string lit}}
22}
23
Ted Kremenek71895b92007-08-14 17:39:48 +000024void check_writeback_specifier()
25{
26 int x;
27 char *b;
28
29 printf("%n",&x); // expected-warning {{'%n' in format string discouraged}}
30 sprintf(b,"%d%%%n",1, &x); // expected-warning {{'%n' in format string dis}}
31}
32
33void check_invalid_specifier(FILE* fp, char *buf)
34{
35 printf("%s%lb%d","unix",10,20); // expected-warning {{lid conversion '%lb'}}
36 fprintf(fp,"%%%l"); // expected-warning {{lid conversion '%l'}}
37 sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // no-warning
38 snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning {{sion '%;'}}
39}
40
41void check_null_char_string(char* b)
42{
43 printf("\0this is bogus%d",1); // expected-warning {{string contains '\0'}}
44 snprintf(b,10,"%%%%%d\0%d",1,2); // expected-warning {{string contains '\0'}}
45 printf("%\0d",1); // expected-warning {{string contains '\0'}}
46}
47
48void check_empty_format_string(char* buf)
49{
50 va_list ap;
51 va_start(ap,buf);
52 vprintf("",ap); // expected-warning {{format string is empty}}
53 sprintf(buf,""); // expected-warning {{format string is empty}}
54}
55
56void check_wide_string()
57{
58 char *b;
59 va_list ap;
60 va_start(ap,b);
61
62 printf(L"foo %d",2); // expected-warning {{should not be a wide string}}
63 vasprintf(&b,L"bar %d",2); // expected-warning {{should not be a wide string}}
64}