[clang] [clang][bytecode] Add more sanity checks for pointers used in `dynamic_cast` (PR #205070)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 22 02:20:23 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Make sure it's initialized and that it points to a record.
---
Full diff: https://github.com/llvm/llvm-project/pull/205070.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+4-2)
- (modified) clang/test/AST/ByteCode/dynamic-cast.cpp (+16)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index b022d71ae1e49..106ca1b9e789e 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2019,10 +2019,12 @@ bool DynamicCast(InterpState &S, CodePtr OpPC, const Type *DestTypePtr,
return false;
}
- // TODO: Other checks?
- if (!Ptr.isBlockPointer())
+ if (!Ptr.isBlockPointer() || !Ptr.getRecord())
return false;
+ if (!Ptr.isInitialized())
+ return DiagnoseUninitialized(S, OpPC, Ptr, AK_Read);
+
// Our given pointer, limited by the base that's currently being initialized,
// if any.
PtrView LimitedPtr;
diff --git a/clang/test/AST/ByteCode/dynamic-cast.cpp b/clang/test/AST/ByteCode/dynamic-cast.cpp
index b782920eb8763..a40b455cecabf 100644
--- a/clang/test/AST/ByteCode/dynamic-cast.cpp
+++ b/clang/test/AST/ByteCode/dynamic-cast.cpp
@@ -294,3 +294,19 @@ namespace UnrelatedAndRootPtr{
}
static_assert(f());
}
+
+namespace Invalid {
+ struct S { virtual void s(); };
+ struct A : S {};
+ struct B : A {};
+ constexpr __UINTPTR_TYPE__ g = 0;
+ static_assert(&dynamic_cast<A&>((S&)(B&)g) == &(A&)(B&)g); // both-error {{not an integral constant expression}} \
+ // both-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
+
+ struct X : S { : ; }; // both-error {{expected expression}} \
+ // both-error {{a type specifier is required for all declarations}}
+ constexpr X x; // both-error {{must be initialized by a constant expression}} \
+ // both-note {{declared here}}
+ static_assert(&dynamic_cast<S&>((X&)x), ""); // both-error {{not an integral constant expression}} \
+ // both-note {{initializer of 'x' is not a constant expression}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/205070
More information about the cfe-commits
mailing list