Tests: Stop invoking UB in AK::NeverDestroyed
's tests
Instead of attempting a stack use-after-free by reading an out-of-scope object's data member, let's keep a flag that checks if the destructor had been called in the outer scope. Fixes #64
This commit is contained in:
parent
8641e78cdc
commit
376b956214
Notes:
sideshowbarker
2024-07-17 05:02:35 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/LadybirdBrowser/ladybird/commit/376b956214 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/67 Issue: https://github.com/LadybirdBrowser/ladybird/issues/64 Reviewed-by: https://github.com/ADKaster ✅
1 changed files with 16 additions and 9 deletions
|
@ -44,20 +44,27 @@ TEST_CASE(should_construct_by_move)
|
|||
EXPECT_EQ(1, n->num_moves);
|
||||
}
|
||||
|
||||
NO_SANITIZE_ADDRESS static void should_not_destroy()
|
||||
{
|
||||
Counter* c = nullptr;
|
||||
struct DestructorChecker {
|
||||
DestructorChecker(bool& destroyed)
|
||||
: m_destroyed(destroyed)
|
||||
{
|
||||
AK::NeverDestroyed<Counter> n {};
|
||||
// note: explicit stack-use-after-scope
|
||||
c = &n.get();
|
||||
}
|
||||
EXPECT_EQ(0, c->num_destroys);
|
||||
}
|
||||
|
||||
~DestructorChecker()
|
||||
{
|
||||
m_destroyed = true;
|
||||
}
|
||||
|
||||
bool& m_destroyed;
|
||||
};
|
||||
|
||||
TEST_CASE(should_not_destroy)
|
||||
{
|
||||
should_not_destroy();
|
||||
bool destroyed = false;
|
||||
{
|
||||
AK::NeverDestroyed<DestructorChecker> n(destroyed);
|
||||
}
|
||||
EXPECT(!destroyed);
|
||||
}
|
||||
|
||||
TEST_CASE(should_provide_dereference_operator)
|
||||
|
|
Loading…
Add table
Reference in a new issue