[clang] [clang][Interp] Bail out from type-punning casts (PR #163809)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 16 23:23:30 PDT 2025


================
@@ -5511,6 +5515,46 @@ bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
   return true;
 }
 
+template <class Emitter>
+bool Compiler<Emitter>::isPunningDereference(const Expr *E)
+{
+  E = E->IgnoreParenImpCasts();
+
+  const auto *UO = dyn_cast<UnaryOperator>(E);
+  if (!UO || UO->getOpcode() != UO_Deref)
+    return false;
+
+  const Expr *Base = UO->getSubExpr()->IgnoreParenImpCasts();
+  const auto *Cast = dyn_cast<CastExpr>(Base);
+  if (!Cast)
+    return false;
+
+  // Only consider reinterpret-ish casts
+  switch (Cast->getCastKind()) {
+    case CK_BitCast:
+    case CK_PointerToIntegral:
+    case CK_IntegralToPointer:
+    case CK_AddressSpaceConversion:
+      break;
+    default:
+      return false; // CK_NoOp etc. are fine
+  }
----------------
term-est wrote:

It's tentative. I think we need to check for actual UBness of the cast, as there are cases where some casts are legal and should be allowed in CE

For a POC to see if this fixes the issue I linked, I just put everything that remotely resembles a fishy cast

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


More information about the cfe-commits mailing list