Skip to content

Commit f8724bd

Browse files
authored
[clang][bytecode] Check live-ness when calling dtors (#137645)
We can't call a destructor on a dead pointer.
1 parent 41ab76b commit f8724bd

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,9 @@ static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
13751375
}
13761376

13771377
bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
1378+
if (!CheckLive(S, OpPC, Ptr, AK_Destroy))
1379+
return false;
1380+
13781381
// Can't call a dtor on a global variable.
13791382
if (Ptr.block()->isStatic()) {
13801383
const SourceInfo &E = S.Current->getSource(OpPC);

clang/test/AST/ByteCode/records.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,3 +1819,14 @@ namespace GlobalDtor {
18191819
a.~A(); // both-note {{cannot modify an object that is visible outside}}
18201820
}
18211821
}
1822+
1823+
namespace NullDtor {
1824+
struct S {};
1825+
constexpr int foo() { // both-error {{never produces a constant expression}}
1826+
S *s = nullptr;
1827+
s->~S(); // both-note 2{{destruction of dereferenced null pointer is not allowed in a constant expression}}
1828+
return 10;
1829+
}
1830+
static_assert(foo() == 10, ""); // both-error {{not an integral constant expression}} \
1831+
// both-note {{in call to}}
1832+
}

0 commit comments

Comments
 (0)