xref: /llvm-project/clang/test/Sema/format-strings.c (revision b30f47a869b68dd1ffda98dd02ed82f107c9ce84)
1 // RUN: clang -fsyntax-only -verify %s
2 
3 #include <stdio.h>
4 #include <stdarg.h>
5 
6 void 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 
24 void 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 
33 void 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 
41 void 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 
48 void 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 
56 void check_wide_string(char* b, ...)
57 {
58   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",ap); // expected-warning {{should not be a wide string}}
63 }
64 
65 void 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 }