[clang] [clang][bytecode] Check that a ltor cast to a complex value is possible (PR #155152)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 24 02:12:49 PDT 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/155152

When we get to this point, the pointer might _not_ be backed by a primitive array, so the later code will fail.

Fixes #155144

>From 8a31c1128ee4f2afe69afedd87024dc3ac76ca78 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 24 Aug 2025 11:09:51 +0200
Subject: [PATCH] [clang][bytecode] Check that a ltor cast to a complex value
 is possible

When we get to this point, the pointer might _not_ be backed by a
primitive array, so the later code will fail.

Fixes #155144
---
 clang/lib/AST/ByteCode/Pointer.cpp | 5 ++++-
 clang/test/AST/ByteCode/c.c        | 4 ++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 7dc6b0c6d437b..2f67c5e3e3479 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -785,8 +785,11 @@ std::optional<APValue> Pointer::toRValue(const Context &Ctx,
 
     // Complex types.
     if (const auto *CT = Ty->getAs<ComplexType>()) {
-      QualType ElemTy = CT->getElementType();
+      // Can happen via C casts.
+      if (!Ptr.getFieldDesc()->isPrimitiveArray())
+        return false;
 
+      QualType ElemTy = CT->getElementType();
       if (ElemTy->isIntegerType()) {
         OptPrimType ElemT = Ctx.classify(ElemTy);
         assert(ElemT);
diff --git a/clang/test/AST/ByteCode/c.c b/clang/test/AST/ByteCode/c.c
index 73469d7fd6cc4..eb46330c7d35f 100644
--- a/clang/test/AST/ByteCode/c.c
+++ b/clang/test/AST/ByteCode/c.c
@@ -173,6 +173,10 @@ _Static_assert(CTB3, ""); // pedantic-ref-warning {{GNU extension}} \
                           // pedantic-expected-warning {{GNU extension}}
 
 
+void nonComplexToComplexCast(void) {
+  _Complex double z = *(_Complex double *)&(struct { double r, i; }){0.0, 1.0};
+}
+
 int t1 = sizeof(int);
 void test4(void) {
   t1 = sizeof(int);



More information about the cfe-commits mailing list