1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/* PR tree-optimization/84095 - false-positive -Wrestrict warnings for
strcpy within array
{ dg-do compile }
{ dg-options "-O2 -Wrestrict -Wno-stringop-overflow -ftrack-macro-expansion=0" } */
typedef __SIZE_TYPE__ size_t;
extern void* memcpy (void* restrict, const void* restrict, size_t);
extern char* strcpy (char* restrict, const char* restrict);
#define T(d, s) strcpy (d, s)
struct MemArrays {
char pad[8];
char a2[3][9];
char a3[3][4][9];
} a[2];
struct NestedMemArrays {
struct MemArrays ma;
struct MemArrays ma1[2];
} nma[2];
/* Use a variable as the source of a copy to verify that VAR_DECL
is handled correctly when it's the source of GIMPLE assignment. */
const char *str = "1234567";
void test_obj_2_dim_const (void)
{
const char *s = strcpy (a[0].a2[0], "1234567");
T (a[0].a2[1], s);
T (a[0].a2[2], s);
/* Ideally, the offsets in the warning below would be relative to
the beginning of the accessed array member. Unfortunately, when
the offset is represented as
MEM_REF (char[9], A, offsetof (struct MemArrays, A[0].A2[0]) + 1)
as it is in this instance it's difficult to determine the member
that is being accessed and tease out from the MEM_REF the offset
as it appears in the source. As a result, the warning mentions
the offset from the beginning of A instead. This is suboptimal
and should be fixed, either by printing the correct offsets or
by mentioning the base object that the offset is relative to. */
T (a[0].a2[0] + 1, s); /* { dg-warning "accessing 8 bytes at offsets \(1|9\) and \(0|8\) overlaps 7 bytes at offset \(1|9\)." } */
T (a[0].a2[1] + 2, s);
T (a[0].a2[2] + 3, s);
T (a[1].a2[0], s);
T (a[1].a2[1], s);
T (a[1].a2[2], s);
T (a[1].a2[0] + 1, s);
T (a[1].a2[1] + 2, s);
T (a[1].a2[2] + 3, s);
}
void test_obj_nested_2_dim_const (void)
{
const char *s = strcpy (nma[0].ma.a2[0], str);
T (nma[0].ma.a2[1], s);
T (nma[0].ma.a2[2], s);
T (nma[0].ma.a2[0] + 1, s); /* { dg-warning "accessing 8 bytes at offsets 1 and 0 overlaps 7 bytes at offset 1" "bug " { xfail *-*-* } } */
T (nma[0].ma.a2[1] + 2, s);
T (nma[0].ma.a2[2] + 3, s);
T (nma[1].ma.a2[1], s);
T (nma[1].ma.a2[2], s);
T (nma[1].ma.a2[0] + 1, s);
T (nma[1].ma.a2[1] + 2, s);
T (nma[1].ma.a2[2] + 3, s);
T (nma[0].ma1[0].a2[1], s);
T (nma[0].ma1[0].a2[2], s);
T (nma[0].ma1[0].a2[0] + 1, s);
T (nma[0].ma1[0].a2[1] + 2, s);
T (nma[0].ma1[0].a2[2] + 3, s);
T (nma[1].ma1[0].a2[1], s);
T (nma[1].ma1[0].a2[2], s);
T (nma[1].ma1[0].a2[0] + 1, s);
T (nma[1].ma1[0].a2[1] + 2, s);
T (nma[1].ma1[0].a2[2] + 3, s);
}
void test_obj_2_dim_var (int i, int j)
{
const char *s = memcpy (a[0].a2[0], "1234567", 8);
T (a[i].a2[0], s); /* { dg-bogus "\\\[-Wrestrict]" } */
T (a[i].a2[1], s);
T (a[i].a2[2], s);
T (a[i].a2[0] + 1, s);
T (a[i].a2[1] + 1, s);
T (a[i].a2[2] + 1, s);
T (a[0].a2[i], s); /* { dg-bogus "\\\[-Wrestrict]" } */
T (a[1].a2[i], s);
T (a[i].a2[0] + j, s);
T (a[i].a2[1] + j, s);
T (a[i].a2[2] + j, s);
T (a[0].a2[i] + 1, s);
T (a[1].a2[i] + 1, s);
T (a[0].a2[i] + j, s);
T (a[1].a2[i] + j, s);
if (i < 0 || 1 < i)
i = 1;
T (a[i].a2[0], s);
T (a[i].a2[1], s);
T (a[i].a2[2], s);
T (a[i].a2[0] + 1, s);
T (a[i].a2[1] + 1, s);
T (a[i].a2[2] + 1, s);
T (a[0].a2[i], s);
T (a[1].a2[i], s);
T (a[i].a2[0] + j, s);
T (a[i].a2[1] + j, s);
T (a[i].a2[2] + j, s);
T (a[0].a2[i] + 1, s);
T (a[1].a2[i] + 1, s);
T (a[0].a2[i] + j, s);
T (a[1].a2[i] + j, s);
}
void test_obj_nested_2_dim_var (int i, int j)
{
const char *s = strcpy (nma[0].ma.a2[0], "1234567");
T (nma[i].ma.a2[0], s); /* { dg-bogus "\\\[-Wrestrict]" } */
T (nma[i].ma.a2[1], s);
T (nma[i].ma.a2[2], s);
T (nma[i].ma.a2[0] + 1, s);
T (nma[i].ma.a2[1] + 1, s);
T (nma[i].ma.a2[2] + 1, s);
T (nma[0].ma.a2[i], s); /* { dg-bogus "\\\[-Wrestrict]" } */
T (nma[1].ma.a2[i], s);
T (nma[i].ma.a2[0] + j, s);
T (nma[i].ma.a2[1] + j, s);
T (nma[i].ma.a2[2] + j, s);
T (nma[0].ma.a2[i] + 1, s);
T (nma[1].ma.a2[i] + 1, s);
T (nma[0].ma.a2[i] + j, s);
T (nma[1].ma.a2[i] + j, s);
}
void test_ref_2_dim_const (struct MemArrays *p)
{
strcpy (p[0].a2[0], "1234567");
const char *s = p[0].a2[0];
T (p[0].a2[1], s);
T (p[0].a2[2], s);
T (p[1].a2[0], s);
T (p[1].a2[1], s);
T (p[1].a2[2], s);
}
void test_ref_2_dim_var (struct MemArrays *p, int i, int j)
{
strcpy (p[0].a2[0], "1234567");
const char *s = p[0].a2[0];
T (p[i].a2[0], s); /* { dg-bogus "\\\[-Wrestrict]" } */
T (p[i].a2[1], s);
T (p[i].a2[2], s);
T (p[0].a2[i], s);
T (p[1].a2[i], s);
T (p[i].a2[0] + j, s);
T (p[i].a2[1] + j, s);
T (p[i].a2[2] + j, s);
T (p[0].a2[i] + j, s);
T (p[1].a2[i] + j, s);
}
void test_obj_3_dim_var (int i, int j)
{
strcpy (a[0].a3[0][0], "1234567");
const char *s = a[0].a3[0][0];
T (a[0].a3[0][i], s);
T (a[0].a3[1][i], s);
T (a[0].a3[2][i], s);
T (a[1].a3[0][i], s);
T (a[1].a3[1][i], s);
T (a[1].a3[2][i], s);
T (a[0].a3[i][0], s); /* { dg-bogus "\\\[-Wrestrict\]" } */
T (a[0].a3[i][1], s);
T (a[0].a3[i][2], s);
T (a[1].a3[i][0], s);
T (a[1].a3[i][1], s);
T (a[1].a3[i][2], s);
T (a[i].a3[0][0], s); /* { dg-bogus "\\\[-Wrestrict\]" } */
T (a[i].a3[0][1], s);
T (a[i].a3[0][2], s);
T (a[i].a3[1][0], s);
T (a[i].a3[1][1], s);
T (a[i].a3[1][2], s);
T (a[i].a3[2][0], s);
T (a[i].a3[2][1], s);
T (a[i].a3[2][2], s);
T (a[0].a3[0][i] + 1, s);
T (a[0].a3[1][i] + 1, s);
T (a[0].a3[2][i] + 1, s);
T (a[1].a3[0][i] + 1, s);
T (a[1].a3[1][i] + 1, s);
T (a[1].a3[2][i] + 1, s);
T (a[0].a3[0][i] + j, s);
T (a[0].a3[1][i] + j, s);
T (a[0].a3[2][i] + j, s);
T (a[1].a3[0][i] + j, s);
T (a[1].a3[1][i] + j, s);
T (a[1].a3[2][i] + j, s);
T (a[0].a3[i][0] + j, s);
T (a[0].a3[i][1] + j, s);
T (a[0].a3[i][2] + j, s);
T (a[1].a3[i][0] + j, s);
T (a[1].a3[i][1] + j, s);
T (a[1].a3[i][2] + j, s);
T (a[i].a3[0][0] + j, s);
T (a[i].a3[0][1] + j, s);
T (a[i].a3[0][2] + j, s);
T (a[i].a3[1][0] + j, s);
T (a[i].a3[1][1] + j, s);
T (a[i].a3[1][2] + j, s);
T (a[i].a3[2][0] + j, s);
T (a[i].a3[2][1] + j, s);
T (a[i].a3[2][2] + j, s);
}
void test_obj_3_dim_const (struct MemArrays *p)
{
strcpy (p[0].a3[0][0], "1234567");
const char *s = p[0].a3[0][0];
T (p[0].a3[0][1], s);
T (p[0].a3[0][2], s);
T (p[0].a3[0][3], s);
T (p[0].a3[0][1] + 1, s);
T (p[0].a3[0][2] + 1, s);
T (p[0].a3[0][3] + 1, s);
T (p[0].a3[1][0], s);
T (p[0].a3[1][1], s);
T (p[0].a3[1][2], s);
T (p[0].a3[1][3], s);
T (p[0].a3[1][0] + 1, s);
T (p[0].a3[1][1] + 1, s);
T (p[0].a3[1][2] + 1, s);
T (p[0].a3[1][3] + 1, s);
T (p[0].a3[2][0], s);
T (p[0].a3[2][1], s);
T (p[0].a3[2][2], s);
T (p[0].a3[2][3], s);
T (p[1].a3[0][0], s);
T (p[1].a3[0][1], s);
T (p[1].a3[0][2], s);
T (p[1].a3[0][3], s);
T (p[1].a3[1][0], s);
T (p[1].a3[1][1], s);
T (p[1].a3[1][2], s);
T (p[1].a3[1][3], s);
T (p[1].a3[2][0], s);
T (p[1].a3[2][1], s);
T (p[1].a3[2][2], s);
T (p[1].a3[2][3], s);
}
|