xref: /llvm-project/clang/test/Analysis/buffer-overlap.c (revision ce97312d109b21acb97d3ea243e214f20bd87cfc)
1 // RUN: %clang_analyze_cc1 -verify %s \
2 // RUN:   -analyzer-checker=alpha.unix.cstring.BufferOverlap
3 //
4 // RUN: %clang_analyze_cc1 -verify %s -DUSE_BUILTINS \
5 // RUN:   -analyzer-checker=alpha.unix.cstring.BufferOverlap
6 //
7 // RUN: %clang_analyze_cc1 -verify %s -DVARIANT \
8 // RUN:   -analyzer-checker=alpha.unix.cstring.BufferOverlap
9 //
10 // RUN: %clang_analyze_cc1 -verify %s -DVARIANT -DUSE_BUILTINS \
11 // RUN:   -analyzer-checker=alpha.unix.cstring.BufferOverlap
12 
13 // This provides us with four possible sprintf() definitions.
14 
15 #ifdef USE_BUILTINS
16 #define BUILTIN(f) __builtin_##f
17 #else /* USE_BUILTINS */
18 #define BUILTIN(f) f
19 #endif /* USE_BUILTINS */
20 
21 typedef typeof(sizeof(int)) size_t;
22 
23 #ifdef VARIANT
24 
25 #define __sprintf_chk BUILTIN(__sprintf_chk)
26 #define __snprintf_chk BUILTIN(__snprintf_chk)
27 int __sprintf_chk (char * __restrict str, int flag, size_t os,
28         const char * __restrict fmt, ...);
29 int __snprintf_chk (char * __restrict str, size_t len, int flag, size_t os,
30         const char * __restrict fmt, ...);
31 
32 #define sprintf(str, ...) __sprintf_chk(str, 0, __builtin_object_size(str, 0), __VA_ARGS__)
33 #define snprintf(str, len, ...) __snprintf_chk(str, len, 0, __builtin_object_size(str, 0), __VA_ARGS__)
34 
35 #else /* VARIANT */
36 
37 #define sprintf BUILTIN(sprintf)
38 int sprintf(char *restrict buffer, const char *restrict format, ... );
39 int snprintf(char *restrict buffer, size_t bufsz,
40              const char *restrict format, ... );
41 #endif /* VARIANT */
42 
test_sprintf1()43 void test_sprintf1() {
44   char a[4] = {0};
45   sprintf(a, "%d/%s", 1, a); // expected-warning{{Arguments must not be overlapping buffers}}
46 }
47 
test_sprintf2()48 void test_sprintf2() {
49   char a[4] = {0};
50   sprintf(a, "%s", a); // expected-warning{{Arguments must not be overlapping buffers}}
51 }
52 
test_sprintf3()53 void test_sprintf3() {
54   char a[4] = {0};
55   sprintf(a, "%s/%s", a, a); // expected-warning{{Arguments must not be overlapping buffers}}
56 }
57 
test_sprintf4()58 void test_sprintf4() {
59   char a[4] = {0};
60   sprintf(a, "%d", 42); // no-warning
61 }
62 
test_sprintf5()63 void test_sprintf5() {
64   char a[4] = {0};
65   char b[4] = {0};
66   sprintf(a, "%s", b); // no-warning
67 }
68 
test_snprintf1()69 void test_snprintf1() {
70   char a[4] = {0};
71   snprintf(a, sizeof(a), "%d/%s", 1, a); // expected-warning{{Arguments must not be overlapping buffers}}
72 }
73 
test_snprintf2()74 void test_snprintf2() {
75   char a[4] = {0};
76   snprintf(a+1, sizeof(a)-1, "%d/%s", 1, a); // expected-warning{{Arguments must not be overlapping buffers}}
77 }
78 
test_snprintf3()79 void test_snprintf3() {
80   char a[4] = {0};
81   snprintf(a, sizeof(a), "%s", a); // expected-warning{{Arguments must not be overlapping buffers}}
82 }
83 
test_snprintf4()84 void test_snprintf4() {
85   char a[4] = {0};
86   snprintf(a, sizeof(a), "%s/%s", a, a); // expected-warning{{Arguments must not be overlapping buffers}}
87 }
88 
test_snprintf5()89 void test_snprintf5() {
90   char a[4] = {0};
91   snprintf(a, sizeof(a), "%d", 42); // no-warning
92 }
93 
test_snprintf6()94 void test_snprintf6() {
95   char a[4] = {0};
96   char b[4] = {0};
97   snprintf(a, sizeof(a), "%s", b); // no-warning
98 }
99