[clang] [clang][bytecode] Fix unknown size arrays crash in clang bytecode (PR #160015)

Osama Abdelkader via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 22 05:27:07 PDT 2025


https://github.com/osamakader updated https://github.com/llvm/llvm-project/pull/160015

>From 89a19cb7c39be556f59f34039a18794635eb7c14 Mon Sep 17 00:00:00 2001
From: Osama Abdelkader <osama.abdelkader at gmail.com>
Date: Mon, 22 Sep 2025 00:59:48 +0300
Subject: [PATCH] [clang][bytecode] Fix unknown size arrays crash in clang
 bytecode

This fixes issue #153948 where clang crashes with assertion failure
'Array of unknown size' when evaluating strlen() on external const char[]
declarations.

The issue was in evaluateStrlen() which called getNumElems() on unknown
size arrays, leading to an assertion in Descriptor::getSize().

Fix: Add check for isDummy() || isUnknownSizeArray() before calling getNumElems() to
gracefully handle unknown size arrays by returning false (indicating
strlen cannot be evaluated at compile time).

Tested with the reproducer from the GitHub issue and added test case.

Signed-off-by: Osama Abdelkader <osama.abdelkader at gmail.com>
---
 clang/lib/AST/ByteCode/Context.cpp              |  3 +++
 .../AST/ByteCode/strlen-unknown-size-array.cpp  | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 clang/test/AST/ByteCode/strlen-unknown-size-array.cpp

diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index cfda6e8ded760..8860bcc54a9c1 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -245,6 +245,9 @@ bool Context::evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result) {
     if (!FieldDesc->isPrimitiveArray())
       return false;
 
+    if (Ptr.isDummy() || Ptr.isUnknownSizeArray())
+      return false;
+
     unsigned N = Ptr.getNumElems();
     if (Ptr.elemSize() == 1) {
       Result = strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), N);
diff --git a/clang/test/AST/ByteCode/strlen-unknown-size-array.cpp b/clang/test/AST/ByteCode/strlen-unknown-size-array.cpp
new file mode 100644
index 0000000000000..3033c8a31218f
--- /dev/null
+++ b/clang/test/AST/ByteCode/strlen-unknown-size-array.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter %s -verify
+// RUN: %clang_cc1 -std=c++20 %s -verify=ref
+
+// expected-no-diagnostics
+// ref-no-diagnostics
+
+// Test that __builtin_strlen() on external/unknown declarations doesn't crash the bytecode interpreter
+// This fixes issue #153948
+
+extern const char s[];  // External declaration (dummy block)
+
+void foo(char *x)
+{
+    // This should not crash - strlen on external declarations should be handled gracefully
+    unsigned long len = __builtin_strlen(s);
+    __builtin_strcpy(x, s);
+}



More information about the cfe-commits mailing list