[clang] [clang][bytecode] Protect evaluateString() from invalid pointers (PR #218411)

via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 24 06:38:49 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

We can't call `Pointer::getNumElems()` on pointers to unknown size arrays.

---
Full diff: https://github.com/llvm/llvm-project/pull/218411.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Context.cpp (+3) 
- (modified) clang/test/AST/ByteCode/invalid.cpp (+9) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index 0678b07d0bbf2..9dc0654d6f5cf 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -284,6 +284,9 @@ bool Context::evaluateString(State &Parent, const Expr *E,
     if (!Ptr.isConst())
       return false;
 
+    if (Ptr.isDummy() || Ptr.isUnknownSizeArray() || Ptr.isPastEnd())
+      return false;
+
     unsigned N = Ptr.getNumElems();
 
     if (Ptr.elemSize() == 1 /* bytes */) {
diff --git a/clang/test/AST/ByteCode/invalid.cpp b/clang/test/AST/ByteCode/invalid.cpp
index 247c8ad732708..67c82cb352d90 100644
--- a/clang/test/AST/ByteCode/invalid.cpp
+++ b/clang/test/AST/ByteCode/invalid.cpp
@@ -247,3 +247,12 @@ namespace InvalidStaticInvoker {
   constexpr int (*baz)(int) = foo;
   int i = baz(42);
 }
+
+namespace UnknownSizeArrayInEvaluateString {
+  void foo() {
+    constexpr char K[] = {'\0'; // both-error {{expected '}'}} \
+                                // both-note {{to match this}}
+    __builtin_verbose_trap("bar", K); // both-error {{argument to __builtin_verbose_trap must be a pointer to a constant string}}
+  }
+  }
+} // both-error {{extraneous closing brace}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/218411


More information about the cfe-commits mailing list