Debian Codegraph @

gcc/testsuite/g++.dg/pr79095-2.C

raw
1 /* { dg-do compile } */
2 /* { dg-options "-Wall -O3" } */
3
4 typedef long unsigned int size_t;
5
6 inline void
7 fill (int *p, size_t n, int)
8 {
9 while (n--)
10 *p++ = 0;
11 }
12
13 struct B
14 {
15 int* p0, *p1, *p2;
16
17 size_t size () const {
18 return size_t (p1 - p0);
19 }
20
21 void resize (size_t n) {
22 if (n > size())
23 append (n - size());
24 }
25
26 void append (size_t n)
27 {
28 if (size_t (p2 - p1) >= n) {
29 fill (p1, n, 0);
30 }
31 }
32 };
33
34 void foo (B &b)
35 {
36 b.resize (b.size () - 1);
37 }
38
39 /* If b.size() == 0, then the argument to b.resize is -1U (it overflowed).
40 This will result calling "fill" which turns into a memset with a bogus
41 length argument. We want to make sure we warn, which multiple
42 things. First the ldist pass converted the loop into a memset,
43 cprop and simplifications made the length a constant and the static
44 analysis pass determines it's a bogus size to pass to memset. */
45 /* { dg-warning "exceeds maximum object size" "" { target *-*-* } 0 } */
46