Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/overlapping-buffers.c

raw
1 /* Test of -Wanalyzer-overlapping-buffers. */
2
3 #include <string.h>
4
5 /* Use noinline functions to hide these calls from the optimizer, to avoid
6 undefined behavior being optimized away to GIMPLE_NOP before the analyzer
7 sees it. */
8
9 char * __attribute__((noinline))
10 call_strcat_symbolic_1 (char *dest, const char *src)
11 {
12 return strcat (dest, src); /* { dg-warning "overlapping buffers" } */
13 }
14
15 void test_strcat_symbolic_1 (char *p)
16 {
17 call_strcat_symbolic_1 (p, p);
18 }
19
20 char * __attribute__((noinline))
21 call_strcpy_symbolic_1 (char *dest, const char *src)
22 {
23 return strcpy (dest, src); /* { dg-warning "overlapping buffers" } */
24 }
25
26 void test_strcpy_symbolic_1 (char *p)
27 {
28 call_strcpy_symbolic_1 (p, p);
29 }
30
31 void * __attribute__((noinline))
32 call_memcpy_concrete_1 (void *dest, const void *src, size_t n)
33 {
34 return memcpy (dest, src, n); /* { dg-warning "overlapping buffers" } */
35 }
36
37 void test_memcpy_concrete_1 (void *p)
38 {
39 call_memcpy_concrete_1 (p, p, 10);
40 }
41
42 void * __attribute__((noinline))
43 call_memcpy_symbolic_1 (void *dest, const void *src, size_t n)
44 {
45 return memcpy (dest, src, n); /* { dg-warning "overlapping buffers" } */
46 }
47
48 void test_memcpy_symbolic_1 (void *p, size_t n)
49 {
50 call_memcpy_symbolic_1 (p, p, n);
51 }
52
53 /* Intersecting vs non-intersecting parts of the same buffer. */
54
55 void * __attribute__((noinline))
56 call_memcpy_nonintersecting_concrete_1 (void *dest, const void *src, size_t n)
57 {
58 return memcpy (dest, src, n); /* { dg-bogus "overlapping buffers passed as" } */
59 }
60
61 void test_memcpy_nonintersecting_concrete_1 (char *p)
62 {
63 call_memcpy_nonintersecting_concrete_1 (p, p + 10, 10);
64 }
65
66 void * __attribute__((noinline))
67 call_memcpy_nonintersecting_concrete_2 (void *dest, const void *src, size_t n)
68 {
69 return memcpy (dest, src, n); /* { dg-bogus "overlapping buffers passed as" } */
70 }
71
72 void test_memcpy_nonintersecting_concrete_2 (char *p)
73 {
74 call_memcpy_nonintersecting_concrete_2 (p + 10, p, 10);
75 }
76
77 void * __attribute__((noinline))
78 call_memcpy_intersecting_concrete_1 (void *dest, const void *src, size_t n)
79 {
80 return memcpy (dest, src, n); /* { dg-warning "overlapping buffers" } */
81 }
82
83 void test_memcpy_intersecting_concrete_1 (char *p)
84 {
85 call_memcpy_intersecting_concrete_1 (p, p + 9, 10);
86 }
87
88 void * __attribute__((noinline))
89 call_memcpy_intersecting_concrete_2 (void *dest, const void *src, size_t n)
90 {
91 return memcpy (dest, src, n); /* { dg-warning "overlapping buffers" } */
92 }
93
94 void test_memcpy_intersecting_concrete_2 (char *p)
95 {
96 call_memcpy_intersecting_concrete_2 (p + 9, p, 10);
97 }
98
99 void * __attribute__((noinline))
100 call_memcpy_intersecting_symbolic_1 (void *dest, const void *src, size_t n)
101 {
102 return memcpy (dest, src, n); /* { dg-warning "overlapping buffers" "" { xfail *-*-* } } */
103 // TODO(xfail)
104 }
105
106 void test_memcpy_intersecting_symbolic_1 (char *p, size_t n)
107 {
108 call_memcpy_intersecting_symbolic_1 (p, p + 1, n);
109 }
110
111 void * __attribute__((noinline))
112 call_memcpy_nonintersecting_symbolic_1 (void *dest, const void *src, size_t n)
113 {
114 return memcpy (dest, src, n); /* { dg-bogus "overlapping buffers passed as" } */
115 }
116
117 void test_memcpy_nonintersecting_symbolic_1 (char *p, size_t n)
118 {
119 call_memcpy_nonintersecting_symbolic_1 (p, p + n, n);
120 }
121
122 void * __attribute__((noinline))
123 call_memcpy_nonintersecting_symbolic_2 (void *dest, const void *src, size_t n)
124 {
125 return memcpy (dest, src, n); /* { dg-bogus "overlapping buffers passed as" } */
126 }
127
128 void test_memcpy_nonintersecting_symbolic_2 (char *p, size_t n)
129 {
130 call_memcpy_nonintersecting_symbolic_2 (p + n, p, n);
131 }
132 /* It's OK for memmove's arguments to overlap. */
133
134 void * __attribute__((noinline))
135 call_memmove_symbolic_1 (void *dest, const void *src, size_t n)
136 {
137 return memmove (dest, src, n); /* { dg-bogus "overlapping buffers passed as" } */
138 }
139
140 void test_memmove_symbolic_1 (void *p, size_t n)
141 {
142 call_memmove_symbolic_1 (p, p, n);
143 }
144
145 static char * __attribute__((noinline))
146 call_strncpy_1 (char *dest, const char *src, size_t n)
147 {
148 return strncpy (dest, src, n); /* { dg-warning "overlapping buffers" } */
149 }
150
151 void
152 test_strncpy_1 (char *p, size_t n)
153 {
154 call_strncpy_1 (p, p, n);
155 }