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

None

>From 72c2128f15ac78a5ebbbd7b25208852c3386ad9f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Tue, 12 May 2026 15:45:56 +0200
Subject: [PATCH] [clang][bytecode] Diagnose reading mutable fields in Memcpy
 op

---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp | 10 ++++++----
 clang/test/AST/ByteCode/records.cpp      | 10 ++++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 5ba15b7ad4f63..726bae10b300d 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -6649,15 +6649,17 @@ static bool copyRecord(InterpState &S, CodePtr OpPC, 
const Pointer &Src,
   assert(SrcDesc->ElemRecord == DestDesc->ElemRecord);
   const Record *R = DestDesc->ElemRecord;
   for (const Record::Field &F : R->fields()) {
+    Pointer FP = Src.atField(F.Offset);
+
+    if (!CheckMutable(S, OpPC, FP))
+      return false;
+
     if (R->isUnion()) {
       // For unions, only copy the active field. Zero all others.
-      const Pointer &SrcField = Src.atField(F.Offset);
-      if (SrcField.isActive()) {
+      if (FP.isActive()) {
         if (!copyField(F, /*Activate=*/true))
           return false;
       } else {
-        if (!CheckMutable(S, OpPC, Src.atField(F.Offset)))
-          return false;
         Pointer DestField = Dest.atField(F.Offset);
         zeroAll(DestField);
       }
diff --git a/clang/test/AST/ByteCode/records.cpp 
b/clang/test/AST/ByteCode/records.cpp
index 20ead8cffdd99..b3f07c3cf007e 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -2011,3 +2011,13 @@ namespace RVOPtrIsExtern {
   }
   static_assert(test_all(), "");
 }
+
+namespace MutableInMemcpy {
+  union H {
+    mutable struct {} gx; // both-note {{declared here}}
+  };
+  constexpr H h1 = {};
+  constexpr H h2 = h1; // both-error {{must be initialized by a constant 
expression}} \
+                       // both-note {{read of mutable member 'gx' is not 
allowed in a constant expression}} \
+                       // both-note {{in call}}
+}

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

Reply via email to