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

Marlus Cadanus da Costa via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 25 06:56:49 PDT 2026


================
@@ -19389,7 +19284,19 @@ bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
         return Error(OOE);
       CurrentType = AT->getElementType();
       CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
-      Result += IdxResult.getSExtValue() * ElementSize;
+      // Reject negative indices, indices too large to fit in int64_t,
+      // and overflow in the offset computation.
+      if (IdxResult.isNegative() || IdxResult.getActiveBits() > 63)
+        return Error(OOE);
----------------
marlus wrote:

Done. Added note_constexpr_offsetof_overflow ("overflow in offsetof") to DiagnosticASTKinds.td and use it in both the ExprConstant.cpp and InterpBuiltin.cpp paths when the multiply or add step of the offset computation overflows int64_t. The test has been updated to expect the new note.

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


More information about the cfe-commits mailing list