Author: Timm Baeder
Date: 2026-06-16T11:17:24+02:00
New Revision: 22bda6c4ca2aac6d90ac96edad6d099b210aa748

URL: 
https://github.com/llvm/llvm-project/commit/22bda6c4ca2aac6d90ac96edad6d099b210aa748
DIFF: 
https://github.com/llvm/llvm-project/commit/22bda6c4ca2aac6d90ac96edad6d099b210aa748.diff

LOG: [clang][bytecode] Add more checks around _Complex values (#204076)

Check the actual source type when converting a pointer to an rvalue. We
otherwise allow converting form a two-element primitive array.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Pointer.cpp
    clang/lib/AST/ByteCode/Pointer.h
    clang/test/AST/ByteCode/complex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Pointer.cpp 
b/clang/lib/AST/ByteCode/Pointer.cpp
index 995a4495f1438..96cd8bb9e11c2 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -908,7 +908,7 @@ std::optional<APValue> Pointer::toRValue(const Context &Ctx,
     if (Ty->isAnyComplexType()) {
       const Descriptor *Desc = Ptr.getFieldDesc();
       // Can happen via C casts.
-      if (!Desc->isPrimitiveArray())
+      if (!Desc->getType()->isAnyComplexType())
         return false;
 
       PrimType ElemT = Desc->getPrimType();

diff  --git a/clang/lib/AST/ByteCode/Pointer.h 
b/clang/lib/AST/ByteCode/Pointer.h
index 0fd8082c73d69..cae3f7476ec83 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -671,7 +671,11 @@ class Pointer {
   bool isTypeidPointer() const { return StorageKind == Storage::Typeid; }
 
   /// Returns the record descriptor of a class.
-  const Record *getRecord() const { return view().getRecord(); }
+  const Record *getRecord() const {
+    if (!isBlockPointer())
+      return nullptr;
+    return view().getRecord();
+  }
   /// Returns the element record type, if this is a non-primive array.
   const Record *getElemRecord() const { return view().getElemRecord(); }
   /// Returns the field information.

diff  --git a/clang/test/AST/ByteCode/complex.cpp 
b/clang/test/AST/ByteCode/complex.cpp
index f0b86a968d492..6995b8a79cb6e 100644
--- a/clang/test/AST/ByteCode/complex.cpp
+++ b/clang/test/AST/ByteCode/complex.cpp
@@ -477,3 +477,10 @@ namespace MemcpyOp {
     z = *(_Complex double *)&x;
   };
 }
+
+namespace LessThanTwoElements {
+  void foo()  { _Complex float z = *(_Complex double *)&(int[]){0};    } // 
both-error {{taking the address of a temporary object of type 'int[1]'}}
+  void foo2() { _Complex float z = *(_Complex double *)&(int[]){0, 0}; } // 
both-error {{taking the address of a temporary object of type 'int[2]'}}
+
+  double _Complex z = *(double _Complex *)&(*(int *)0x1234);
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to