Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / buffer-overlap.c
blob8414a764541e211b19e403d853d5a3db4e53bd02
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
13 // This provides us with four possible sprintf() definitions.
15 #ifdef USE_BUILTINS
16 #define BUILTIN(f) __builtin_##f
17 #else /* USE_BUILTINS */
18 #define BUILTIN(f) f
19 #endif /* USE_BUILTINS */
21 typedef typeof(sizeof(int)) size_t;
23 #ifdef VARIANT
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, ...);
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__)
35 #else /* VARIANT */
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 */
43 void test_sprintf1() {
44 char a[4] = {0};
45 sprintf(a, "%d/%s", 1, a); // expected-warning{{Arguments must not be overlapping buffers}}
48 void test_sprintf2() {
49 char a[4] = {0};
50 sprintf(a, "%s", a); // expected-warning{{Arguments must not be overlapping buffers}}
53 void test_sprintf3() {
54 char a[4] = {0};
55 sprintf(a, "%s/%s", a, a); // expected-warning{{Arguments must not be overlapping buffers}}
58 void test_sprintf4() {
59 char a[4] = {0};
60 sprintf(a, "%d", 42); // no-warning
63 void test_sprintf5() {
64 char a[4] = {0};
65 char b[4] = {0};
66 sprintf(a, "%s", b); // no-warning
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}}
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}}
79 void test_snprintf3() {
80 char a[4] = {0};
81 snprintf(a, sizeof(a), "%s", a); // expected-warning{{Arguments must not be overlapping buffers}}
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}}
89 void test_snprintf5() {
90 char a[4] = {0};
91 snprintf(a, sizeof(a), "%d", 42); // no-warning
94 void test_snprintf6() {
95 char a[4] = {0};
96 char b[4] = {0};
97 snprintf(a, sizeof(a), "%s", b); // no-warning