| 1 |
/* PR middle-end/101977 - bogus -Warray-bounds on a negative index into |
| 2 |
a parameter in conditional with null |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-O2 -Wall" } */ |
| 5 |
|
| 6 |
struct A { int i; }; |
| 7 |
struct B { struct A a1; struct A a2; }; |
| 8 |
|
| 9 |
|
| 10 |
void nowarn_p_0_0 (struct A *p, int i) |
| 11 |
{ |
| 12 |
struct A *q = i < 0 ? p : 0 < i ? (struct A*)0 : 0; |
| 13 |
struct B *r = (struct B*)((char *)q - __builtin_offsetof (struct B, a2)); |
| 14 |
r->a1.i = 0; |
| 15 |
} |
| 16 |
|
| 17 |
void nowarn_0_p_0 (struct A *p, int i) |
| 18 |
{ |
| 19 |
struct A *q = i < 0 ? 0 : 0 < i ? p : 0; |
| 20 |
struct B *r = (struct B*)((char *)q - __builtin_offsetof (struct B, a2)); |
| 21 |
r->a1.i = 0; // { dg-bogus "-Warray-bounds" } |
| 22 |
} |
| 23 |
|
| 24 |
void nowarn_0_0_p (struct A *p, int i) |
| 25 |
{ |
| 26 |
struct A *q = i < 0 ? 0 : 0 < i ? 0 : p; |
| 27 |
struct B *r = (struct B*)((char *)q - __builtin_offsetof (struct B, a2)); |
| 28 |
r->a1.i = 0; // { dg-bogus "-Warray-bounds" } |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
void nowarn_p_q_0 (struct A *p, struct A *q, int i) |
| 33 |
{ |
| 34 |
struct A *r = i < 0 ? p : 0 < i ? q : 0; |
| 35 |
struct B *s = (struct B*)((char *)r - __builtin_offsetof (struct B, a2)); |
| 36 |
s->a1.i = 0; // { dg-bogus "-Warray-bounds" } |
| 37 |
} |
| 38 |
|
| 39 |
void nowarn_p_0_q (struct A *p, struct A *q, int i) |
| 40 |
{ |
| 41 |
struct A *r = i < 0 ? p : 0 < i ? 0 : q; |
| 42 |
struct B *s = (struct B*)((char *)r - __builtin_offsetof (struct B, a2)); |
| 43 |
s->a1.i = 0; // { dg-bogus "-Warray-bounds" } |
| 44 |
} |
| 45 |
|
| 46 |
void nowarn_0_p_q (struct A *p, struct A *q, int i) |
| 47 |
{ |
| 48 |
struct A *r = i < 0 ? 0 : 0 < i ? p : q; |
| 49 |
struct B *s = (struct B*)((char *)r - __builtin_offsetof (struct B, a2)); |
| 50 |
s->a1.i = 0; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
void nowarn_p_q_r (struct A *p, struct A *q, struct A *r, int i) |
| 55 |
{ |
| 56 |
struct A *s = i < 0 ? p : 0 < i ? q : r; |
| 57 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 58 |
t->a1.i = 0; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
extern struct B b1, b2, b3; |
| 63 |
|
| 64 |
void nowarn_p_b1_0 (struct A *p, int i) |
| 65 |
{ |
| 66 |
struct A *r = i < 0 ? p : 0 < i ? &b1.a2 : 0; |
| 67 |
struct B *s = (struct B*)((char *)r - __builtin_offsetof (struct B, a2)); |
| 68 |
s->a1.i = 0; // { dg-bogus "-Warray-bounds" } |
| 69 |
} |
| 70 |
|
| 71 |
void nowarn_p_0_b1 (struct A *p, int i) |
| 72 |
{ |
| 73 |
struct A *r = i < 0 ? p : 0 < i ? 0 : &b1.a2; |
| 74 |
struct B *s = (struct B*)((char *)r - __builtin_offsetof (struct B, a2)); |
| 75 |
s->a1.i = 0; // { dg-bogus "-Warray-bounds" } |
| 76 |
} |
| 77 |
|
| 78 |
void nowarn_0_p_b1 (struct A *p, int i) |
| 79 |
{ |
| 80 |
struct A *r = i < 0 ? 0 : 0 < i ? p : &b1.a2; |
| 81 |
struct B *s = (struct B*)((char *)r - __builtin_offsetof (struct B, a2)); |
| 82 |
s->a1.i = 0; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void nowarn_p_b1_b2 (struct A *p, int i) |
| 87 |
{ |
| 88 |
struct A *s = i < 0 ? p : 0 < i ? &b1.a2 : &b2.a2; |
| 89 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 90 |
t->a1.i = 0; |
| 91 |
} |
| 92 |
|
| 93 |
void nowarn_b1_p_b2 (struct A *p, int i) |
| 94 |
{ |
| 95 |
struct A *s = i < 0 ? &b1.a2 : 0 < i ? p : &b2.a2; |
| 96 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 97 |
t->a1.i = 0; |
| 98 |
} |
| 99 |
|
| 100 |
void nowarn_b1_b2_p (struct A *p, int i) |
| 101 |
{ |
| 102 |
struct A *s = i < 0 ? &b1.a2 : 0 < i ? &b2.a2 : p; |
| 103 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 104 |
t->a1.i = 0; |
| 105 |
} |
| 106 |
|
| 107 |
void nowarn_b1_b2_b3 (struct A *p, int i) |
| 108 |
{ |
| 109 |
struct A *s = i < 0 ? &b1.a2 : 0 < i ? &b2.a2 : &b3.a2; |
| 110 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 111 |
t->a1.i = 0; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void nowarn_0_b1_b2 (int i) |
| 116 |
{ |
| 117 |
struct A *s = i < 0 ? 0 : 0 < i ? &b1.a2 : &b2.a2; |
| 118 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 119 |
t->a1.i = 0; |
| 120 |
} |
| 121 |
|
| 122 |
void warn_b1_0_b2 (int i) |
| 123 |
{ |
| 124 |
struct A *s = i < 0 ? &b1.a2 : 0 < i ? 0 : &b2.a2; |
| 125 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 126 |
t->a1.i = 0; |
| 127 |
} |
| 128 |
|
| 129 |
void warn_b1_b2_0 (int i) |
| 130 |
{ |
| 131 |
struct A *s = i < 0 ? &b1.a2 : 0 < i ? &b2.a2 : 0; |
| 132 |
struct B *t = (struct B*)((char *)s - __builtin_offsetof (struct B, a2)); |
| 133 |
t->a1.i = 0; |
| 134 |
} |