diff options
| author | Timm Baeder <tbaeder@redhat.com> | 2025-04-28 17:47:30 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-28 17:47:30 +0200 |
| commit | f8724bd873878df81720bdec8b7a9a24f2a40a03 (patch) | |
| tree | ba72c97a66760495cd7c87d14638041a82fed8e7 | |
| parent | 41ab76bf0aafb35d6d023429560b44637dfb8de6 (diff) | |
[clang][bytecode] Check live-ness when calling dtors (#137645)
We can't call a destructor on a dead pointer.
| -rw-r--r-- | clang/lib/AST/ByteCode/Interp.cpp | 3 | ||||
| -rw-r--r-- | clang/test/AST/ByteCode/records.cpp | 11 |
2 files changed, 14 insertions, 0 deletions
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index 4d89f23401db..62b449597bca 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 9abfe6b8a15e..c2fe3d900748 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}} +} |
