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

We can't call `getNumElems()` for unknown-size arrays.

>From 9b031c41c4911f295b4764b2a47e77161c9c01d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Mon, 3 Aug 2026 11:04:36 +0200
Subject: [PATCH] [clang][bytecode] Protect against invalid C++26 string repr

We can't call `getNumElems()` for unknown-size arrays.
---
 clang/lib/AST/ByteCode/Context.cpp |  3 ++-
 clang/test/AST/ByteCode/cxx26.cpp  | 12 ++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Context.cpp 
b/clang/lib/AST/ByteCode/Context.cpp
index b913d2a9f539c..9f1d9b899052d 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -208,7 +208,8 @@ bool Context::evaluateStringRepr(State &Parent, const Expr 
*SizeExpr,
       return false;
     }
 
-    if (!Ptr.isLive() || !Ptr.getFieldDesc()->isPrimitiveArray())
+    if (!Ptr.isLive() || !Ptr.isInitialized() || Ptr.isUnknownSizeArray() ||
+        !Ptr.getFieldDesc()->isPrimitiveArray())
       return false;
 
     // Must be char.
diff --git a/clang/test/AST/ByteCode/cxx26.cpp 
b/clang/test/AST/ByteCode/cxx26.cpp
index 769deb28cdf50..9dc6270b9d551 100644
--- a/clang/test/AST/ByteCode/cxx26.cpp
+++ b/clang/test/AST/ByteCode/cxx26.cpp
@@ -91,3 +91,15 @@ namespace ConstexprUnknownReference {
   }
 
 }
+
+namespace UnknownSizeArrayString {
+  constexpr const char foo[] = {bar}; // both-error {{use of undeclared 
identifier}} \
+                                      // ref-note {{declared here}}
+  struct S {
+    constexpr int size() const { return 4; }
+    constexpr const char *data() const { return foo; }
+  };
+  static_assert(false, S{}); // both-error {{the message in a static assertion 
must be produced by a constant expression}} \
+                             // ref-note {{initializer of 'foo' is unknown}} \
+                             // both-error {{static assertion failed}}
+}

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

Reply via email to