1 /* PR middle-end/103215 - bogus -Warray-bounds with two pointers with
3 Test for accesses into distinct arrays through pointers with different
6 If/when -Warray-bounds is enhanced to issue "maybe" kinds of warnings
7 some of the accesses here will trigger those and will need updating.
10 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
12 #define NOIPA __attribute__ ((noipa))
14 #define A(p, off) ((p)[off] = __COUNTER__)
16 extern int a4
[4], a8
[8];
19 NOIPA
void a4_p1_a8_p3 (int i
)
23 int *q
= i
? a4_p1
: a8_p3
;
24 A (q
, -4); // { dg-warning "-Warray-bounds" }
25 /* Because -3 is a valid offset into a8 but not a4, q must point
26 to the former and so subscripts between -3 and +4 refer to its
28 A (q
, -3); A (q
, -2); A (q
, -1); A (q
, 0);
29 A (q
, 1); A (q
, 2); A (q
, 3); A (q
, 4);
30 A (q
, 5); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
31 /* Both of the following are definitely out of bounds but the first isn't
32 diagnosed because the code conservatively merges the offsets into A4
34 A (q
, 7); // { dg-warning "-Warray-bounds" }
38 NOIPA
void a4_p1_a8_p5 (int i
)
42 int *q
= i
? a4_p1
: a8_p5
;
43 A (q
, -6); // { dg-warning "-Warray-bounds" }
44 /* Similarly to the above, because -5 is a valid offset into a8 but
45 not a4, q must point to the former and so subscripts between -5
46 and +2 refer to its elements. */
47 A (q
, -5); A (q
, -4); A (q
, -3); A (q
, -2);
48 A (q
, -1); A (q
, 0); A (q
, 1); A (q
, 2);
49 A (q
, 3); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
50 A (q
, 7); // { dg-warning "-Warray-bounds" }
54 NOIPA
void a4_p1_a8_p7 (int i
)
58 int *q
= i
? a4_p1
: a8_p7
;
59 A (q
, -8); // { dg-warning "-Warray-bounds" }
60 A (q
, -7); A (q
, -6); A (q
, -5); A (q
, -4);
61 A (q
, -3); A (q
, -2); A (q
, -1); A (q
, 0);
62 /* Since -7 is valid, q must point to a8 and the last valid subscript
64 A (q
, 1); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
65 A (q
, 2); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
66 A (q
, 3); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
67 A (q
, 7); // { dg-warning "-Warray-bounds" }
71 NOIPA
void mp_1_a4_p1_a8_p7 (int i
, int j
)
75 int *p
= i
? a4_p1
: a8_p7
;
76 int *q
= j
? p
+ 1 : p
- 1;
78 A (q
, -9); // { dg-warning "-Warray-bounds" }
80 /* q points either to a8 + [6, 8] or a4 + [0, 2]. */
81 A (q
, -8); A (q
, -7); A (q
, -6); A (q
, -5);
82 A (q
, -4); A (q
, -3); A (q
, -2); A (q
, -1);
84 /* Since all the above are valid, q must point to a8 + 8. But as
85 mentioned above, the warning for each subscript is independent
86 of prior subscripts into the same object so the access below
88 A (q
, 0); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
89 A (q
, 1); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
90 A (q
, 2); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
91 A (q
, 3); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
92 A (q
, 4); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
93 A (q
, 8); // { dg-warning "-Warray-bounds" }
97 NOIPA
void mp1_a4_p1_a8_p5 (int i
, int j
)
101 int *p
= i
? a4_p1
: a8_p5
;
103 int *q
= j
? p
+ 1 : p
- 1;
105 // q is assumed to point to a8 + 6
106 A (q
, -7); // { dg-warning "-Warray-bounds" }
107 A (q
, -6); A (q
, -5); A (q
, -4); A (q
, -3);
108 A (q
, -2); A (q
, -1); A (q
, 0); A (q
, 1);
109 /* Even though the above accesses rule it out, q is now assumed
110 to point to either a4 + [0, 2] or a8 + [4, 5]. */
112 /* q is now assumed to point tp a4. Given that, only the first store
114 A (q
, 3); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
115 A (q
, 4); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
116 A (q
, 5); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
117 A (q
, 6); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
118 A (q
, 7); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
119 A (q
, 8); // { dg-warning "-Warray-bounds" }
123 NOIPA
void mp1_a4_p1_a8_p4 (int i
, int j
)
127 int *p
= i
? a4_p1
: a8_p4
;
129 int *q
= j
? p
+ 1 : p
- 1;
131 // q is assumed to point to a8 + 5
132 A (q
, -6); // { dg-warning "-Warray-bounds" }
141 /* Even though the above accesses rule it out, q is now assumed
142 to point tp a4. Given that, only the first store is valid. */
143 A (q
, 3); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
144 A (q
, 4); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
145 A (q
, 5); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
146 A (q
, 6); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
147 A (q
, 7); // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
148 A (q
, 8); // { dg-warning "-Warray-bounds" }