[clang] [Clang] Fix offsetof sign-extending unsigned array indices >= 128 (PR #204139)

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 2 20:55:30 PDT 2026


================
@@ -6673,12 +6673,28 @@ bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
       // When generating bytecode, we put all the index expressions as Sint64 on
       // the stack.
       int64_t Index = ArrayIndices[ArrayIndex];
+      if (Index < 0)
+        return Invalid(S, OpPC);
       const ArrayType *AT = S.getASTContext().getAsArrayType(CurrentType);
       if (!AT)
         return false;
       CurrentType = AT->getElementType();
       CharUnits ElementSize = S.getASTContext().getTypeSizeInChars(CurrentType);
-      Result += Index * ElementSize;
+      int64_t ElemSize = ElementSize.getQuantity();
+      if (Index != 0 && ElemSize > llvm::maxIntN(64) / Index) {
----------------
shafik wrote:

This need some parentheses, I get order of operations and all but really this is asking for a bug when someone and changes this and gets it wrong.

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


More information about the cfe-commits mailing list