[clang] [clang][bytecode] Fix reinterpret casts of two non-primitive types (PR #107564)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 6 04:02:07 PDT 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/107564

We don't want to allow e.g. cast from a record to an array or the other way arround.

>From dda02f67ba68beba4564bae52374849724fd19ec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 5 Sep 2024 11:18:02 +0200
Subject: [PATCH] [clang][bytecode] Fix reinterpret casts of two non-primitive
 types

We don't want to allow e.g. cast from a record to an array or the other
way arround.
---
 clang/lib/AST/ByteCode/Compiler.cpp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index a831f196abdcb5..c376dd8325fe2e 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2478,8 +2478,15 @@ bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
     const CXXReinterpretCastExpr *E) {
   const Expr *SubExpr = E->getSubExpr();
 
-  bool TypesMatch = classify(E) == classify(SubExpr);
-  if (!this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/!TypesMatch, E))
+  bool Fatal = false;
+  std::optional<PrimType> FromT = classify(SubExpr);
+  std::optional<PrimType> ToT = classify(E);
+  if (!FromT || !ToT)
+    Fatal = true;
+  else
+    Fatal = (ToT != FromT);
+
+  if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
     return false;
 
   return this->delegate(SubExpr);



More information about the cfe-commits mailing list