Skip to content

Commit fadd87e

Browse files
authored
[clang][bytecode] Check that a ltor cast to a complex value is possible (llvm#155152)
When we get to this point, the pointer might _not_ be backed by a primitive array, so the later code will fail. Fixes llvm#155144
1 parent 0a675f5 commit fadd87e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,11 @@ std::optional<APValue> Pointer::toRValue(const Context &Ctx,
785785

786786
// Complex types.
787787
if (const auto *CT = Ty->getAs<ComplexType>()) {
788-
QualType ElemTy = CT->getElementType();
788+
// Can happen via C casts.
789+
if (!Ptr.getFieldDesc()->isPrimitiveArray())
790+
return false;
789791

792+
QualType ElemTy = CT->getElementType();
790793
if (ElemTy->isIntegerType()) {
791794
OptPrimType ElemT = Ctx.classify(ElemTy);
792795
assert(ElemT);

clang/test/AST/ByteCode/c.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ _Static_assert(CTB3, ""); // pedantic-ref-warning {{GNU extension}} \
173173
// pedantic-expected-warning {{GNU extension}}
174174

175175

176+
void nonComplexToComplexCast(void) {
177+
_Complex double z = *(_Complex double *)&(struct { double r, i; }){0.0, 1.0};
178+
}
179+
176180
int t1 = sizeof(int);
177181
void test4(void) {
178182
t1 = sizeof(int);

0 commit comments

Comments
 (0)