[clang] [clang][bytecode] Check __builtin_strcmp for fixed-size integer elements (PR #237879)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 2 00:29:40 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/237879
We can't call getElemDataSize() on e.g. 128-bit integers.
>From f204700a1778383207129e91ace728800c3ba1b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 2 Nov 2026 09:24:08 +0100
Subject: [PATCH] [clang][bytecode] Check __builtin_strcmp for fixed-size
integer elements
We can't call getElemDataSize() on e.g. 128-bit integers.
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 22 ++++++++++++++++++-
clang/test/AST/ByteCode/builtin-functions.cpp | 5 +++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index c9f44807c9080..9530d58e7d6a4 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -50,6 +50,23 @@ namespace interp {
return false;
}
+static bool isFixedSizeIntegralType(PrimType T) {
+ switch (T) {
+ case PT_Sint8:
+ case PT_Uint8:
+ case PT_Sint16:
+ case PT_Uint16:
+ case PT_Sint32:
+ case PT_Uint32:
+ case PT_Sint64:
+ case PT_Uint64:
+ case PT_Bool:
+ return true;
+ default:
+ return false;
+ }
+}
+
static void discard(InterpStack &Stk, PrimType T) {
TYPE_SWITCH(T, { Stk.discard<T>(); });
}
@@ -435,6 +452,9 @@ static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
assert(StrPtr.getFieldDesc()->isPrimitiveArray());
PrimType ElemT = StrPtr.getFieldDesc()->getPrimType();
+ if (!isFixedSizeIntegralType(ElemT))
+ return false;
+
unsigned ElemSize = StrPtr.getFieldDesc()->getElemDataSize();
if (ElemSize != 1 && ElemSize != 2 && ElemSize != 4)
return Invalid(S, OpPC);
@@ -442,7 +462,7 @@ static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
if (ID == Builtin::BI__builtin_wcslen || ID == Builtin::BIwcslen) {
const ASTContext &AC = S.getASTContext();
unsigned WCharSize = AC.getTypeSizeInChars(AC.getWCharType()).getQuantity();
- if (StrPtr.getFieldDesc()->getElemDataSize() != WCharSize)
+ if (ElemSize != WCharSize)
return false;
}
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 04e4728da078b..dc6d2274d8293 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -138,6 +138,11 @@ namespace strcmp {
// 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}}
+#ifdef __SIZEOF_INT128__
+ const unsigned __int128 foo128[] = {};
+ constexpr int bar128 = __builtin_strlen((char *)foo128); // both-error {{must be initialized by a constant expression}} \
+ // both-note {{cast that performs the conversions of a reinterpret_cast}}
+#endif
}
namespace WcsCmp {
More information about the cfe-commits
mailing list