[clang] [clang][bytecode] Fix incorrect handling of arithmetic on string literals (PR #173212)

Oliver Hunt via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 23 15:38:18 PST 2025


================
@@ -294,12 +294,14 @@ bool Context::evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result) {
     if (!FieldDesc->isPrimitiveArray())
       return false;
 
-    if (Ptr.isDummy() || Ptr.isUnknownSizeArray())
+    if (Ptr.isDummy() || Ptr.isUnknownSizeArray() || Ptr.isPastEnd())
       return false;
 
     unsigned N = Ptr.getNumElems();
     if (Ptr.elemSize() == 1) {
-      Result = strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), N);
+      unsigned Size = N - Ptr.getIndex();
+      Result =
+          strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
       return Result != N;
----------------
ojhunt wrote:

Hmmm, this should be Result != Size - I clearly missed this when moving this portion of the code around and yet all the tests pass. @tbaederr any ideas how I might be able to trigger this?

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


More information about the cfe-commits mailing list