error checking tweaks

(cherry picked from commit 6ac38519b710e69a0c30eb0fe8fc5fa712168cb8)
This commit is contained in:
Taeer Bar-Yam 2024-05-09 12:41:34 -04:00 committed by Robert Hensing
parent c2159c8834
commit 48af0f9e41
5 changed files with 14 additions and 8 deletions

View file

@ -52,8 +52,9 @@ impl EvalState {
store.raw_ptr(),
)
};
context.check_err()?;
if eval_state.is_null() {
bail!("nix_state_create returned a null pointer");
panic!("nix_state_create returned a null pointer without an error");
}
Ok(EvalState {
eval_state: NonNull::new(eval_state).unwrap(),
@ -152,7 +153,7 @@ where
if unsafe { raw::GC_thread_is_registered() } != 0 {
return Ok(f());
} else {
gc_register_my_thread().unwrap();
gc_register_my_thread()?;
let r = f();
unsafe {
raw::GC_unregister_my_thread();

View file

@ -1,6 +1,6 @@
use nix_c_raw as raw;
use nix_util::context::Context;
use std::ptr::NonNull;
use std::ptr::{null_mut, NonNull};
// TODO: test: cloning a thunk does not duplicate the evaluation.
@ -35,6 +35,7 @@ impl ValueType {
raw::ValueType_NIX_TYPE_PATH => ValueType::Path,
raw::ValueType_NIX_TYPE_STRING => ValueType::String,
raw::ValueType_NIX_TYPE_THUNK => ValueType::Thunk,
// This would happen if a new type of value is added in Nix.
_ => ValueType::Unknown,
}
}
@ -56,17 +57,17 @@ impl Value {
}
impl Drop for Value {
fn drop(&mut self) {
let context = Context::new();
unsafe {
raw::gc_decref(context.ptr(), self.inner.as_ptr());
// ignoring error because the only failure mode is leaking memory
raw::gc_decref(null_mut(), self.inner.as_ptr());
}
// ignore error from context, because drop should not panic
}
}
impl Clone for Value {
fn clone(&self) -> Self {
let context = Context::new();
unsafe { raw::gc_incref(context.ptr(), self.inner.as_ptr()) };
// can't return an error here, but we don't want to ignore the error either as it means we could use-after-free
context.check_err().unwrap();
Value { inner: self.inner }
}