[clang] [clang][bytecode] Check strcmp input for unknown-size arrays (PR #220835)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 3 00:58:12 PDT 2026


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

The later calls to getNumElems() will fail for them.

>From 103b8c172326b9fbcaa0b86b082dbe5ac07013eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 3 Sep 2026 09:57:10 +0200
Subject: [PATCH] [clang][bytecode] Check strcmp input for unknown-size arrays

The later calls to getNumElems() will fail for them.
---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp      | 4 +++-
 clang/test/AST/ByteCode/builtin-functions.cpp | 6 ++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 91d6a32c136ac..b78ce614e290d 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -306,7 +306,9 @@ static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
 
   if (!A.isReadablePointerType() || !B.isReadablePointerType())
     return false;
-  if (A.isDummy() || B.isDummy())
+
+  if (A.isDummy() || B.isDummy() || A.isUnknownSizeArray() ||
+      B.isUnknownSizeArray())
     return false;
 
   bool IsWide = ID == Builtin::BIwcscmp || ID == Builtin::BIwcsncmp ||
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 47a1c23d77799..51e1b6cde4779 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -132,6 +132,12 @@ namespace strcmp {
   static_assert(__builtin_strncmp("abaa", "abba", 0) == 0);
   static_assert(__builtin_strncmp(0, 0, 0) == 0);
   static_assert(__builtin_strncmp("abab\0banana", "abab\0canada", 100) == 0);
+
+
+  constexpr char missingInit[] = bar__; // both-error {{use of undeclared identifier 'bar__'}} \
+                                        // ref-note {{declared here}}
+  static_assert(__builtin_strcmp(missingInit, "bar") == 0, ""); // both-error {{not an integral constant expression}} \
+                                                                // ref-note {{initializer of 'missingInit' is unknown}}
 }
 
 namespace WcsCmp {



More information about the cfe-commits mailing list