blob: 2d6e719e3ad09f41c20514ba05d4eb40aab9f689 [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
Ted Kremenek4a336462007-12-17 19:03:13 +00006char * global_fmt;
7
Chris Lattner59907c42007-08-10 20:18:51 +00008void 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 Kremenek4a336462007-12-17 19:03:13 +000015 vprintf(s,ap); // // no-warning
Chris Lattner59907c42007-08-10 20:18:51 +000016 fprintf(fp,s); // expected-warning {{format string is not a string literal}}
Ted Kremenek4a336462007-12-17 19:03:13 +000017 vfprintf(fp,s,ap); // no-warning
Chris Lattner59907c42007-08-10 20:18:51 +000018 asprintf(&b,s); // expected-warning {{format string is not a string lit}}
Ted Kremenek4a336462007-12-17 19:03:13 +000019 vasprintf(&b,s,ap); // no-warning
Chris Lattner59907c42007-08-10 20:18:51 +000020 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 Kremenek4a336462007-12-17 19:03:13 +000022 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 Lattner59907c42007-08-10 20:18:51 +000025}
26
Ted Kremenek71895b92007-08-14 17:39:48 +000027void 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
36void 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
44void 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 Carlsson6eda8c92007-10-12 17:48:41 +000051void check_empty_format_string(char* buf, ...)
Ted Kremenek71895b92007-08-14 17:39:48 +000052{
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 Carlsson6eda8c92007-10-12 17:48:41 +000059void check_wide_string(char* b, ...)
Ted Kremenek71895b92007-08-14 17:39:48 +000060{
Ted Kremenek71895b92007-08-14 17:39:48 +000061 va_list ap;
62 va_start(ap,b);
63
Daniel Dunbar4489fe12008-08-05 00:07:51 +000064 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 Kremenek71895b92007-08-14 17:39:48 +000066}
Ted Kremenek580b6642007-10-12 20:51:52 +000067
68void 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 Dunbar4489fe12008-08-05 00:07:51 +000074}