1 /* PR tree-optimization/101397 - spurious warning writing to the result
2 of stpcpy minus 1
3 Verify warnings for indexing into a pointer returned from stpncpy.
4 The call stpncpy(S1, S2, N) returns the address of the copy of
5 the first NUL is it exists or &S1[N] otherwise.
6 { dg-do compile }
7 { dg-options "-O2 -Wall -Wno-stringop-truncation" } */
8
9 typedef __SIZE_TYPE__ size_t;
10
11 __attribute__ ((alloc_size (1))) const void* alloc (size_t);
12
13 void* memchr (const void*, int, size_t);
14
15 void sink (int, ...);
16
17 extern char ax[], a3[3], a5[5], a7[7], a9[9];
18
19 volatile int x;
20
21 /* Verify warnings for indexing into the result of memchr. */
22
23 void test_memchr (int i, int n, int n3_5, int n3_9)
24 {
25 {
26 /* Because memchr never returns a past-the-end pointer the result
27 below is in [ax, ax + 4]. */
28 const char *p = memchr (ax, x, 5);
29 x = p[-5]; // { dg-warning "\\\[-Warray-bounds" }
30 x = p[-4];
31 x = p[-1];
32 x = p[ 0];
33 x = p[ 9];
34 }
35
36 {
37 // The returned pointer is in [ax, ax + n].
38 const char *p = memchr (ax, x, n);
39 sink (p[-99], p[-6], p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
40 sink (p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[99]);
41 }
42
43
44 {
45 // The returned pointer is in [a5, a5 + 2].
46 const char *p = memchr (a5, x, 3);
47 x = p[-3]; // { dg-warning "\\\[-Warray-bounds" }
48 sink (p[-2], p[-1], p[0]);
49 sink (p[1], p[2], p[3], p[4]);
50 x = p[ 5]; // { dg-warning "\\\[-Warray-bounds" }
51 }
52
53 {
54 // The returned pointer is a5 + 4.
55 const char *p = memchr (a5, x, 4);
56 x = p[-4]; // { dg-warning "\\\[-Warray-bounds" }
57 sink (p[-3], p[-2], p[-1], p[0]);
58 sink (p[1], p[2], p[3], p[4]);
59 x = p[ 5]; // { dg-warning "\\\[-Warray-bounds" }
60 }
61
62 {
63 // The returned pointer is in [a5, a5 + 4].
64 const char *p = memchr (a5, x, n);
65 x = p[-5]; // { dg-warning "\\\[-Warray-bounds" }
66 sink (p[-4], p[-3], p[-2], p[-1], p[0]);
67 sink (p[1], p[2], p[3], p[4]);
68 x = p[ 5]; // { dg-warning "\\\[-Warray-bounds" }
69 }
70
71 {
72 if (n3_5 < 3 || 5 < n3_5)
73 n3_5 = 3;
74
75 // The returned pointer is in [a7, a7 + 4].
76 const char *p = memchr (a7, x, n3_5);
77 x = p[-5]; // { dg-warning "\\\[-Warray-bounds" }
78 sink (p[-4], p[-3], p[-2], p[-1], p[0]);
79 sink (p[1], p[2], p[3], p[4], p[5], p[6]);
80 x = p[7]; // { dg-warning "\\\[-Warray-bounds" }
81 }
82
83 {
84 if (n3_9 < 3 || 9 < n3_9)
85 n3_9 = 3;
86
87 // The returned pointer is in [a5, a5 + 4].
88 const char *p = memchr (a5, x, n3_9);
89 x = p[-5]; // { dg-warning "\\\[-Warray-bounds" }
90 sink (p[-4], p[-3], p[-2], p[-1], p[0]);
91 sink (p[1], p[2], p[3], p[4]);
92 x = p[ 5]; // { dg-warning "\\\[-Warray-bounds" }
93 }
94
95 {
96 const char *p = memchr (a5, x, 4);
97
98 if (i > -1) i = -1;
99 x = p[i];
100
101 if (i > -2) i = -2;
102 x = p[i];
103
104 if (i > -3) i = -3;
105 x = p[i];
106
107 if (i > -4) i = -4;
108 x = p[i]; // { dg-warning "\\\[-Warray-bounds" }
109 }
110 }
111
112
113 void test_memchr_in_allocated (int i, int n, int n5_7, int n3_9)
114 {
115 if (n5_7 < 5 || 7 < n5_7)
116 n5_7 = 5;
117
118 {
119 const char *s = alloc (n5_7);
120 const char *p = memchr (s, x, n);
121 x = p[-7]; // { dg-warning "\\\[-Warray-bounds" }
122 sink (p[-6], p[-6], p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
123 sink (p[1], p[2], p[3], p[4], p[5], p[6]);
124 x = p[7]; // { dg-warning "\\\[-Warray-bounds" }
125 }
126
127 {
128 const char *s = alloc (n5_7);
129 const char *p = memchr (s, x, n);
130 x = p[-7]; // { dg-warning "\\\[-Warray-bounds" }
131 sink (p[-6], p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
132 sink (p[1], p[2], p[3], p[4], p[5], p[6]);
133 x = p[7]; // { dg-warning "\\\[-Warray-bounds" }
134 }
135
136 {
137 const char *s = alloc (n5_7);
138 const char *p = memchr (s, x, n);
139 x = p[-7]; // { dg-warning "\\\[-Warray-bounds" }
140 sink (p[-6], p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
141 sink (p[1], p[2], p[3], p[4], p[5], p[6]);
142 x = p[7]; // { dg-warning "\\\[-Warray-bounds" }
143 }
144
145 {
146 const char *s = alloc (n5_7);
147 const char *p = memchr (s, x, n);
148 x = p[-7]; // { dg-warning "\\\[-Warray-bounds" }
149 sink (p[-6], p[-5], p[-3], p[-4], p[-2], p[-1], p[0]);
150 sink (p[1], p[2], p[3], p[4], p[5], p[6]);
151 x = p[7]; // { dg-warning "\\\[-Warray-bounds" }
152 }
153
154 {
155 const char *s = alloc (n5_7);
156 const char *p = memchr (s, x, n3_9);
157 x = p[-7]; // { dg-warning "\\\[-Warray-bounds" }
158 sink (p[-6], p[-5], p[-4], p[-3], p[-2], p[-1], p[0]);
159 sink (p[1], p[2], p[3], p[4], p[5], p[6]);
160 x = p[7]; // { dg-warning "\\\[-Warray-bounds" }
161 }
162
163 {
164 const char *s = alloc (n5_7);
165 const char *p = memchr (s, x, n3_9);
166 x = p[-7]; // { dg-warning "\\\[-Warray-bounds" }
167 sink (p[-6], p[-5], p[-4], p[-4], p[-2], p[-1], p[0]);
168 sink (p[1], p[2], p[3], p[4], p[5], p[6]);
169 x = p[7]; // { dg-warning "\\\[-Warray-bounds" }
170 }
171
172 }