https://github.com/MScarta updated https://github.com/llvm/llvm-project/pull/241354
>From fe9780f42d8f1ef4ec1c28e15f73269bf103a066 Mon Sep 17 00:00:00 2001 From: Michael Scartaccini <[email protected]> Date: Wed, 11 Nov 2026 23:26:57 +0800 Subject: [PATCH 1/2] [clang] Fix strlen constant evaluation for wide string literals with an offset The string literal fast path in EvaluateBuiltinStrLen passed a byte offset to StringLiteral::findZeroCodeUnit(), which expects an index in code units. For a wide string literal with a non-zero offset this gave the wrong length. For example, with a 4-byte wchar_t, __builtin_wcslen(L"abc" + 1) evaluated to 12 instead of 2. This was introduced by #218601 (a2ac4f6af958) and has not been in a release. The fix converts the offset to code units before calling the helper, only takes the fast path when the offset is on a code unit boundary, and no longer falls back to the literal's byte size. It also fixes two other faults from the same change: * The fast path was taken when the literal was read through a pointer to a different character type, so __builtin_strlen((const char *)L"abc") could be folded to 3. The check that the character types match is restored. * When the string contents were requested, a wide literal was sliced using a code unit count as a byte count. Wide literals now take the slow path in that case, so both paths produce the same string. The new constant interpreter is not affected by the offset bug, but it still folds the cast case above. That is an older fault that is also in LLVM 21, so it is left for a separate change and marked in the test. AI use: an AI tool found the bug. The fix and the tests were drafted with AI assistance. I reviewed every line, understand the change, and ran the tests. Assisted-by: Claude --- clang/lib/AST/ExprConstant.cpp | 25 +++++++++++++++---------- clang/test/SemaCXX/constexpr-string.cpp | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 57d5bb475d0f9b..d7d2b1dba0e8bb 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -23785,25 +23785,30 @@ EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info, if (!EvaluatePointer(E, String, Info)) return std::nullopt; + QualType CharTy = E->getType()->getPointeeType(); + // Fast path: if it's a string literal, search the string value. if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( String.getLValueBase().dyn_cast<const Expr *>())) { StringRef Str = S->getBytes(); int64_t Off = String.Offset.getQuantity(); - if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size()) { - UnsignedOrNone ZeroIndex = S->findZeroCodeUnit(Off); - if (StringResult) { - if (ZeroIndex) - Str = Str.substr(Off, *ZeroIndex); - *StringResult = Str; + unsigned Width = S->getCharByteWidth(); + QualType LitCharTy = + S->getType()->castAsArrayTypeUnsafe()->getElementType(); + if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && Off % Width == 0 && + Info.Ctx.hasSameUnqualifiedType(CharTy, LitCharTy) && + // When the text is wanted, wide strings go to the slow path, which + // builds it one char per character. Raw bytes here would not match. + (Width == 1 || !StringResult)) { + if (UnsignedOrNone Len = S->findZeroCodeUnit(Off / Width)) { + if (StringResult) + *StringResult = Str.substr(Off, *Len); + return *Len; } - - return ZeroIndex.value_or(Str.size()); } - // For an invalid index, fall through to the offset handling below. + // Anything else falls through to the slow path. } - QualType CharTy = E->getType()->getPointeeType(); // Slow path: scan the bytes of the string looking for the terminating 0. for (uint64_t Strlen = 0; /**/; ++Strlen) { APValue Char; diff --git a/clang/test/SemaCXX/constexpr-string.cpp b/clang/test/SemaCXX/constexpr-string.cpp index 96303ba1ef8015..ee008fd596949e 100644 --- a/clang/test/SemaCXX/constexpr-string.cpp +++ b/clang/test/SemaCXX/constexpr-string.cpp @@ -7,8 +7,8 @@ // RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-signed-char // RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-wchar -DNO_PREDEFINED_WCHAR_T -// RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-signed-char -fexperimental-new-constant-interpreter -// RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-wchar -DNO_PREDEFINED_WCHAR_T -fexperimental-new-constant-interpreter +// RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify=expected,new-interp -pedantic -Wno-vla-extension -fno-signed-char -fexperimental-new-constant-interpreter +// RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify=expected,new-interp -pedantic -Wno-vla-extension -fno-wchar -DNO_PREDEFINED_WCHAR_T -fexperimental-new-constant-interpreter # 9 "/usr/include/string.h" 1 3 4 // expected-warning {{this style of line directive is a GNU extension}} extern "C" { @@ -63,6 +63,18 @@ namespace Strlen { int arr[3]; // expected-note 2{{here}} int k = arr[strlen("hello")]; // expected-warning {{array index 5}} int wk = arr[wcslen(L"hello")]; // expected-warning {{array index 5}} + + // __builtin_wcslen on a wide string literal with a non-zero offset. + static_assert(__builtin_wcslen(&L"abc"[1]) == 2, ""); + static_assert(__builtin_wcslen(&L"abcdef"[1]) == 5, ""); + static_assert(__builtin_wcslen(&L"ab\0cd"[1]) == 1, ""); + static_assert(__builtin_wcslen(&L"ab\0cd"[3]) == 2, ""); + static_assert(__builtin_wcslen(&L"abc"[3]) == 0, ""); + static_assert(__builtin_strlen(&"abc"[1]) == 2, ""); + + // A wide literal read as char must not be folded. + // FIXME: The new constant interpreter still folds it. + static_assert(!__builtin_constant_p(__builtin_strlen((const char *)L"abc")), ""); // new-interp-error {{static assertion failed}} } namespace StrcmpEtc { >From b8a7606319d95baeeb5dcb03d0a860ea09e08646 Mon Sep 17 00:00:00 2001 From: Michael Scartaccini <[email protected]> Date: Thu, 12 Nov 2026 10:00:00 +0800 Subject: [PATCH 2/2] Compare character sizes instead of types in the string literal fast path --- clang/lib/AST/ExprConstant.cpp | 5 ++--- clang/test/SemaCXX/constexpr-string.cpp | 6 +++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index d7d2b1dba0e8bb..064fd9bf43a356 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -23793,10 +23793,9 @@ EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info, StringRef Str = S->getBytes(); int64_t Off = String.Offset.getQuantity(); unsigned Width = S->getCharByteWidth(); - QualType LitCharTy = - S->getType()->castAsArrayTypeUnsafe()->getElementType(); if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && Off % Width == 0 && - Info.Ctx.hasSameUnqualifiedType(CharTy, LitCharTy) && + !CharTy->isIncompleteType() && + Info.Ctx.getTypeSize(CharTy) == Width * 8 && // When the text is wanted, wide strings go to the slow path, which // builds it one char per character. Raw bytes here would not match. (Width == 1 || !StringResult)) { diff --git a/clang/test/SemaCXX/constexpr-string.cpp b/clang/test/SemaCXX/constexpr-string.cpp index ee008fd596949e..e3dd96e71f90c9 100644 --- a/clang/test/SemaCXX/constexpr-string.cpp +++ b/clang/test/SemaCXX/constexpr-string.cpp @@ -67,7 +67,7 @@ namespace Strlen { // __builtin_wcslen on a wide string literal with a non-zero offset. static_assert(__builtin_wcslen(&L"abc"[1]) == 2, ""); static_assert(__builtin_wcslen(&L"abcdef"[1]) == 5, ""); - static_assert(__builtin_wcslen(&L"ab\0cd"[1]) == 1, ""); + static_assert(__builtin_wcslen(&L"abc\0defgh"[1]) == 2, ""); static_assert(__builtin_wcslen(&L"ab\0cd"[3]) == 2, ""); static_assert(__builtin_wcslen(&L"abc"[3]) == 0, ""); static_assert(__builtin_strlen(&"abc"[1]) == 2, ""); @@ -75,6 +75,10 @@ namespace Strlen { // A wide literal read as char must not be folded. // FIXME: The new constant interpreter still folds it. static_assert(!__builtin_constant_p(__builtin_strlen((const char *)L"abc")), ""); // new-interp-error {{static assertion failed}} + + // A literal read as a different character type of the same size is folded. + static_assert(__builtin_constant_p(__builtin_strlen((const char *)u8"abc")), ""); + static_assert(__builtin_constant_p(__builtin_wcslen((const wchar_t *)U"abc")), ""); } namespace StrcmpEtc { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
