mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 08:16:15 +02:00
Merge pull request #10865 from Mic92/better-cleanup
libutil: guard Finally against invalid exception throws
This commit is contained in:
commit
dadb6f86cf
1 changed files with 23 additions and 1 deletions
|
@ -2,6 +2,8 @@
|
||||||
///@file
|
///@file
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <cassert>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A trivial class to run a function at the end of a scope.
|
* A trivial class to run a function at the end of a scope.
|
||||||
|
@ -21,5 +23,25 @@ public:
|
||||||
Finally(Finally &&other) : fun(std::move(other.fun)) {
|
Finally(Finally &&other) : fun(std::move(other.fun)) {
|
||||||
other.movedFrom = true;
|
other.movedFrom = true;
|
||||||
}
|
}
|
||||||
~Finally() { if (!movedFrom) fun(); }
|
~Finally() noexcept(false)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (!movedFrom)
|
||||||
|
fun();
|
||||||
|
} catch (...) {
|
||||||
|
// finally may only throw an exception if exception handling is not already
|
||||||
|
// in progress. if handling *is* in progress we have to return cleanly here
|
||||||
|
// but are still prohibited from doing so since eating the exception would,
|
||||||
|
// in almost all cases, mess up error handling even more. the only good way
|
||||||
|
// to handle this is to abort entirely and leave a message, so we'll assert
|
||||||
|
// (and rethrow anyway, just as a defense against possible NASSERT builds.)
|
||||||
|
if (std::uncaught_exceptions()) {
|
||||||
|
assert(false &&
|
||||||
|
"Finally function threw an exception during exception handling. "
|
||||||
|
"this is not what you want, please use some other methods (like "
|
||||||
|
"std::promise or async) instead.");
|
||||||
|
}
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue