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

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


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

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

>From c7c99f4d815366f8f49b6bc23832989c9a914ae9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 24 Aug 2026 15:36:20 +0200
Subject: [PATCH] [clang][bytecode] Protect evaluateString() from invalid
 pointers

We can't call `Pointer::getNumElems()` on pointers to unknown size
arrays.
---
 clang/lib/AST/ByteCode/Context.cpp  | 3 +++
 clang/test/AST/ByteCode/invalid.cpp | 9 +++++++++
 2 files changed, 12 insertions(+)

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}}



More information about the cfe-commits mailing list