blob: 2222f796086bb43ec18da8bb89440f4b5a312a9e [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
Anders Carlsson6eda8c92007-10-12 17:48:41 +000048void check_empty_format_string(char* buf, ...)
Ted Kremenek71895b92007-08-14 17:39:48 +000049{
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
Anders Carlsson6eda8c92007-10-12 17:48:41 +000056void check_wide_string(char* b, ...)
Ted Kremenek71895b92007-08-14 17:39:48 +000057{
Ted Kremenek71895b92007-08-14 17:39:48 +000058 va_list ap;
59 va_start(ap,b);
60
61 printf(L"foo %d",2); // expected-warning {{should not be a wide string}}
62 vasprintf(&b,L"bar %d",2); // expected-warning {{should not be a wide string}}
63}
Ted Kremenek580b6642007-10-12 20:51:52 +000064
65void check_asterisk_precision_width(int x) {
66 printf("%*d"); // expected-warning {{'*' specified field width is missing a matching 'int' argument}}
67 printf("%.*d"); // expected-warning {{'.*' specified field precision is missing a matching 'int' argument}}
68 printf("%*d",12,x); // no-warning
69 printf("%*d","foo",x); // expected-warning {{field width should have type 'int', but argument has type 'char *'}}
70 printf("%.*d","foo",x); // expected-warning {{field precision should have type 'int', but argument has type 'char *'}}
71}