[clang] [clang][bytecode] Avoid crash in constexpr wcslen on invalid argument… (PR #177891)
Ayush Kumar Gaur via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 26 02:25:50 PST 2026
https://github.com/Ayush3941 updated https://github.com/llvm/llvm-project/pull/177891
>From 7679af9795c8a9769e4e2a298ca4ed9db5183ee8 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Sun, 25 Jan 2026 22:00:57 -0500
Subject: [PATCH] [clang][bytecode] Avoid crash in constexpr wcslen on invalid
argument type
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index d668fa118d3b9..e2517730e81ca 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -367,10 +367,18 @@ static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
unsigned ElemSize = StrPtr.getFieldDesc()->getElemSize();
if (ID == Builtin::BI__builtin_wcslen || ID == Builtin::BIwcslen) {
- [[maybe_unused]] const ASTContext &AC = S.getASTContext();
- assert(ElemSize == AC.getTypeSizeInChars(AC.getWCharType()).getQuantity());
+ const ASTContext &AC = S.getASTContext();
+ unsigned WCharSize = AC.getTypeSizeInChars(AC.getWCharType()).getQuantity();
+ if (ElemSize != WCharSize) {
+ if (S.diagnosing()) {
+ // a dedicated diagnostic
+ diagnoseNonConstexprBuiltin(S, OpPC, ID);
+ }
+ return false;
+ }
}
+
size_t Len = 0;
for (size_t I = StrPtr.getIndex();; ++I, ++Len) {
const Pointer &ElemPtr = StrPtr.atIndex(I);
More information about the cfe-commits
mailing list