[clang] [clang] Fix strlen constant evaluation for wide string literals with an offset (PR #241354)
Shafik Yaghmour via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 19:34:20 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();
+ // 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.
+ if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && Off % Width == 0 &&
----------------
shafik wrote:
So the fact that we had to cast both `Off` and `Str.size()` to `uint64_t` should have been a clue something was very wrong here.
First of all `Off` should just be `size_t` since that is what `getQuantity()` gives us then we can remove the casts and the check for `>= 0` is nonsense.
I realize this is existing code but really you need to understand the code you are modifying.
https://github.com/llvm/llvm-project/pull/241354
More information about the cfe-commits
mailing list