[clang] 55c70f6 - [clang][bytecode] Check GetPtrBase ops for null pointers (#110673)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 1 08:09:29 PDT 2024


Author: Timm Baeder
Date: 2024-10-01T17:09:26+02:00
New Revision: 55c70f6d893452d3b7b2005bc6b5d208f2e840ba

URL: https://github.com/llvm/llvm-project/commit/55c70f6d893452d3b7b2005bc6b5d208f2e840ba
DIFF: https://github.com/llvm/llvm-project/commit/55c70f6d893452d3b7b2005bc6b5d208f2e840ba.diff

LOG: [clang][bytecode] Check GetPtrBase ops for null pointers (#110673)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/records.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 8a3c6810e0e11b..5c3ee5e689f1c3 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1641,14 +1641,14 @@ inline bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
 
 inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
   const Pointer &Ptr = S.Stk.peek<Pointer>();
+  if (!CheckNull(S, OpPC, Ptr, CSK_Base))
+    return false;
 
   if (!Ptr.isBlockPointer()) {
     S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
     return true;
   }
 
-  if (!CheckNull(S, OpPC, Ptr, CSK_Base))
-    return false;
   if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
     return false;
   const Pointer &Result = Ptr.atField(Off);
@@ -1661,13 +1661,14 @@ inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
 inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off) {
   const Pointer &Ptr = S.Stk.pop<Pointer>();
 
+  if (!CheckNull(S, OpPC, Ptr, CSK_Base))
+    return false;
+
   if (!Ptr.isBlockPointer()) {
     S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
     return true;
   }
 
-  if (!CheckNull(S, OpPC, Ptr, CSK_Base))
-    return false;
   if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
     return false;
   const Pointer &Result = Ptr.atField(Off);

diff  --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 7e3cf5b94518f7..215f26bd5da8ea 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1653,3 +1653,11 @@ namespace ExprWithCleanups {
   constexpr auto F = true ? 1i : 2i;
   static_assert(F == 1i, "");
 }
+
+namespace NullptrUpcast {
+  struct A {};
+  struct B : A { int n; };
+  constexpr B *nb = nullptr;
+  constexpr A &ra = *nb; // both-error {{constant expression}} \
+                         // both-note {{cannot access base class of null pointer}}
+}


        


More information about the cfe-commits mailing list