Skip to content

Commit 703a0b6

Browse files
committed
[clang][bytecode] Fix comparing pointers to union members
If one of them is a one-past-end pointer.
1 parent ffddf33 commit 703a0b6

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ size_t Pointer::computeOffsetForComparison() const {
375375
}
376376

377377
if (const Record *R = P.getBase().getRecord(); R && R->isUnion()) {
378+
if (P.isOnePastEnd())
379+
++Result;
378380
// Direct child of a union - all have offset 0.
379381
P = P.getBase();
380382
continue;

clang/test/AST/ByteCode/unions.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,3 +966,15 @@ namespace AddressComparison {
966966
static_assert(&U2.a[0] != &U2.b[1]);
967967
static_assert(&U2.a[0] == &U2.b[1]); // both-error {{failed}}
968968
}
969+
970+
#if __cplusplus >= 202002L
971+
namespace UnionMemberOnePastEnd {
972+
constexpr bool b() {
973+
union {
974+
int p;
975+
};
976+
return &p == (&p + 1);
977+
}
978+
static_assert(!b());
979+
}
980+
#endif

0 commit comments

Comments
 (0)