[clang] [clang][bytecode] Fix unknown size arrays crash in clang bytecode (PR #160015)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 21 15:07:35 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Osama Abdelkader (osamakader)
<details>
<summary>Changes</summary>
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 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.
---
Full diff: https://github.com/llvm/llvm-project/pull/160015.diff
1 Files Affected:
- (modified) clang/lib/AST/ByteCode/Context.cpp (+5)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index cfda6e8ded760..f9bc3906beec1 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -245,6 +245,11 @@ bool Context::evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result) {
if (!FieldDesc->isPrimitiveArray())
return false;
+ // Handle unknown size arrays - we can't determine the length at compile time
+ if (Ptr.isUnknownSizeArray()) {
+ return false;
+ }
+
unsigned N = Ptr.getNumElems();
if (Ptr.elemSize() == 1) {
Result = strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), N);
``````````
</details>
https://github.com/llvm/llvm-project/pull/160015
More information about the cfe-commits
mailing list