xref: /llvm-project/clang/test/Sema/format-strings.c (revision 81f7f2904cf15fa74f9ab051eb3ca288442a92c7)
1 // RUN: clang -fsyntax-only -verify %s
2 
3 #include <stdio.h>
4 #include <stdarg.h>
5 
6 char * global_fmt;
7 
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}}
15   vprintf(s,ap); // // no-warning
16   fprintf(fp,s); // expected-warning {{format string is not a string literal}}
17   vfprintf(fp,s,ap); // no-warning
18   asprintf(&b,s); // expected-warning {{format string is not a string lit}}
19   vasprintf(&b,s,ap); // no-warning
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}}
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}}
25 }
26 
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 
51 void check_empty_format_string(char* buf, ...)
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 
59 void check_wide_string(char* b, ...)
60 {
61   va_list ap;
62   va_start(ap,b);
63 
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}}
66 }
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 *'}}
74 }
75