2018-05-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-stringop-chk-2.c
blobd537fb040c1c4135e406db322e0924e5eca8678f
1 /* This file was miscompiled by an earlier version of the object size
2 checking patch. Object size in one of the memcpy calls was
3 incorrectly determined to be 0 while it should be (size_t) -1
4 (== unknown). */
5 /* { dg-do compile } */
6 /* { dg-options "-O2 -ftrack-macro-expansion=0" } */
8 #include "../gcc.c-torture/execute/builtins/chk.h"
10 void *bar (int);
11 extern void *malloc (__SIZE_TYPE__);
13 struct A
15 int i, j, k;
18 /* Here all object sizes are not known at compile time. There
19 should be no warning, nor any checker functions called. */
21 void
22 foo (const struct A *x, int y, const unsigned char *z)
24 unsigned int b;
25 unsigned char *c = 0;
27 b = (x->i & 0xff) == 1 ? 3 : 4;
28 if (y)
29 c = bar (x->j * x->k);
31 const unsigned char *d = z;
32 unsigned char *e = c;
33 unsigned char *f = c + x->j * x->k;
34 int g = 0;
36 while (e < f)
38 unsigned int h = *d++;
40 if (h & 128)
42 h = h - 128;
43 g = e + h * b > f;
44 if (g)
45 h = (f - e) / b;
46 if (b < 4)
49 memcpy (e, d, 3);
50 e += 3;
52 while (--h);
53 else
56 memcpy (e, d, 4);
57 e += 4;
59 while (--h);
60 d += b;
62 else
64 h *= b;
65 g = e + h > f;
66 if (g)
67 h = f - e;
68 memcpy (e, d, h);
69 e += h;
70 d += h;
75 /* The same routine, slightly modified:
76 1) c has known size at compile time
77 2) e += h was changed into e += 16.
78 GCC could actually through VRP determine that
79 in e += h is (h >= 0 && h <= 127), thus know
80 it is pointer addition and not subtraction and
81 know e's __builtin_object_size (e, 0) is at 512,
82 but we are not there yet. */
84 unsigned char *
85 baz (const struct A *x, const unsigned char *z)
87 unsigned int b;
88 unsigned char *c = 0;
90 b = (x->i & 0xff) == 1 ? 3 : 4;
91 c = malloc (512);
93 const unsigned char *d = z;
94 unsigned char *e = c;
95 unsigned char *f = c + x->j * x->k;
96 int g = 0;
98 while (e < f)
100 unsigned int h = *d++;
102 if (h & 128)
104 h = h - 128;
105 g = e + h * b > f;
106 if (g)
107 h = (f - e) / b;
108 if (b < 4)
111 memcpy (e, d, 3);
112 e += 3;
114 while (--h);
115 else
118 memcpy (e, d, 513); /* { dg-warning "writing" "memcpy" } */
119 e += 4;
121 while (--h);
122 d += b;
124 else
126 h *= b;
127 g = e + h > f;
128 if (g)
129 h = f - e;
130 memcpy (e, d, h);
131 /* e += h; */
132 e += 16;
133 d += h;
136 return c;