mirror of
https://github.com/NixOS/nix
synced 2025-06-30 03:23:16 +02:00
Don't leak exceptions
This commit is contained in:
parent
e6bd88878e
commit
b4edc3ca61
3 changed files with 48 additions and 9 deletions
|
@ -152,28 +152,47 @@ struct Source
|
|||
template<typename T>
|
||||
struct Result
|
||||
{
|
||||
unsigned int tag;
|
||||
enum { Ok = 0, Err = 1, Uninit = 2 } tag;
|
||||
|
||||
union {
|
||||
T data;
|
||||
std::exception_ptr * exc;
|
||||
};
|
||||
|
||||
Result() : tag(Uninit) { }; // FIXME: remove
|
||||
|
||||
Result(const Result &) = delete;
|
||||
|
||||
Result(Result && other)
|
||||
: tag(other.tag)
|
||||
{
|
||||
other.tag = Uninit;
|
||||
if (tag == Ok)
|
||||
data = std::move(other.data);
|
||||
else if (tag == Err)
|
||||
exc = other.exc;
|
||||
}
|
||||
|
||||
~Result()
|
||||
{
|
||||
if (tag == 0)
|
||||
if (tag == Ok)
|
||||
data.~T();
|
||||
else if (tag == 1)
|
||||
// FIXME: don't leak exc
|
||||
else if (tag == Err)
|
||||
free(exc);
|
||||
else if (tag == Uninit)
|
||||
;
|
||||
else
|
||||
abort();
|
||||
}
|
||||
|
||||
/* Rethrow the wrapped exception or return the wrapped value. */
|
||||
T unwrap()
|
||||
{
|
||||
if (tag == 0)
|
||||
if (tag == Ok) {
|
||||
tag = Uninit;
|
||||
return std::move(data);
|
||||
else if (tag == 1)
|
||||
}
|
||||
else if (tag == Err)
|
||||
std::rethrow_exception(*exc);
|
||||
else
|
||||
abort();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue