[clang] [clang] Fix strlen constant evaluation for wide string literals with an offset (PR #241354)
Michael Scartaccini via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 18:58:42 PST 2026
================
@@ -23785,25 +23785,29 @@ 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();
+ if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && Off % Width == 0 &&
+ !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.
----------------
MScarta wrote:
Done.
https://github.com/llvm/llvm-project/pull/241354
More information about the cfe-commits
mailing list