1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 06:31:14 +02:00

Don't leak exceptions

This commit is contained in:
Eelco Dolstra 2019-12-13 19:05:09 +01:00
parent e6bd88878e
commit b4edc3ca61
3 changed files with 48 additions and 9 deletions

View file

@ -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);
}