Pārlūkot izejas kodu

Exceptionpropagation from the deleter prevented. As the deleter is

called from within the dtor, this may otherwise cause UB (fixes #7).
main
offa pirms 7 gadiem
vecāks
revīzija
a8cd4a3852
2 mainītis faili ar 18 papildinājumiem un 1 dzēšanām
  1. +11
    -1
      include/scope_guard.h
  2. +7
    -0
      test/ScopeGuardTest.cpp

+ 11
- 1
include/scope_guard.h Parādīt failu

@@ -28,7 +28,7 @@ namespace sr
{
if( m_execute_on_destruction == true )
{
m_deleter();
callDeleterSafe();
}
}

@@ -45,6 +45,16 @@ namespace sr

private:

void callDeleterSafe() noexcept
{
try
{
m_deleter();
}
catch( ... ) { /* Empty */ }
}


Deleter m_deleter;
bool m_execute_on_destruction;
};

+ 7
- 0
test/ScopeGuardTest.cpp Parādīt failu

@@ -64,3 +64,10 @@ TEST_CASE("move transfers state if released", "[ScopeGuard]")
REQUIRE(executed == false);
}

TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")
{
REQUIRE_NOTHROW([] {
auto guard = sr::scope_guard([] { throw "Don't propagate this!"; });
static_cast<void>(guard);
}());
}

Notiek ielāde…
Atcelt
Saglabāt