[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 &&
+        !CharTy->isIncompleteType() &&
+        Info.Ctx.getTypeSize(CharTy) == Width * 8 &&
----------------
shafik wrote:

`8` is a magic number please name it or find a named way to get this value that makes it clear what it is. `1` below is also a magic number.

https://github.com/llvm/llvm-project/pull/241354


More information about the cfe-commits mailing list