diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2019-12-13 19:05:09 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2019-12-13 19:05:26 +0100 |
commit | b4edc3ca61c1f1a98052ec8ffc41d1b533b08fd7 (patch) | |
tree | 6382fdb8b4258a7d965a8352248a4929b6a72b31 /nix-rust/src/error.rs | |
parent | e6bd88878e8fc08652b77f0444adcd74321dc6b6 (diff) |
Don't leak exceptions
Diffstat (limited to 'nix-rust/src/error.rs')
-rw-r--r-- | nix-rust/src/error.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/nix-rust/src/error.rs b/nix-rust/src/error.rs index 586a8f53d..85ac926e9 100644 --- a/nix-rust/src/error.rs +++ b/nix-rust/src/error.rs @@ -76,7 +76,7 @@ impl From<Error> for CppException { fn from(err: Error) -> Self { match err { Error::Foreign(ex) => ex, - _ => unsafe { make_error(&err.to_string()) }, + _ => CppException::new(&err.to_string()), } } } @@ -85,7 +85,23 @@ impl From<Error> for CppException { #[derive(Debug)] pub struct CppException(*const libc::c_void); // == std::exception_ptr* +impl CppException { + fn new(s: &str) -> Self { + Self(unsafe { make_error(s) }) + } +} + +impl Drop for CppException { + fn drop(&mut self) { + unsafe { + destroy_error(self.0); + } + } +} + extern "C" { #[allow(improper_ctypes)] // YOLO - fn make_error(s: &str) -> CppException; + fn make_error(s: &str) -> *const libc::c_void; + + fn destroy_error(exc: *const libc::c_void); } |