[clang] [clang][bytecode] Check live-ness when calling dtors (PR #137645)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 28 07:57:22 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
We can't call a destructor on a dead pointer.
---
Full diff: https://github.com/llvm/llvm-project/pull/137645.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+3)
- (modified) clang/test/AST/ByteCode/records.cpp (+11)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 4d89f23401db5..62b449597bca8 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1375,6 +1375,9 @@ static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
}
bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
+ if (!CheckLive(S, OpPC, Ptr, AK_Destroy))
+ return false;
+
// Can't call a dtor on a global variable.
if (Ptr.block()->isStatic()) {
const SourceInfo &E = S.Current->getSource(OpPC);
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 9abfe6b8a15e7..c2fe3d9007480 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1819,3 +1819,14 @@ namespace GlobalDtor {
a.~A(); // both-note {{cannot modify an object that is visible outside}}
}
}
+
+namespace NullDtor {
+ struct S {};
+ constexpr int foo() { // both-error {{never produces a constant expression}}
+ S *s = nullptr;
+ s->~S(); // both-note 2{{destruction of dereferenced null pointer is not allowed in a constant expression}}
+ return 10;
+ }
+ static_assert(foo() == 10, ""); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/137645
More information about the cfe-commits
mailing list