From ee1d591e1a6ffcc792a1431edb458d1440dbf24d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 29 Aug 2024 15:52:16 +0200 Subject: [PATCH] fix: Update for more recent Nix API (cherry picked from commit 55355f03ab86dfa15972681d9a51d333739f6d10) --- rust/nix-expr/src/value.rs | 4 ++-- rust/nix-util/src/context.rs | 10 +++++----- rust/nix-util/src/string_return.rs | 9 +++++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/rust/nix-expr/src/value.rs b/rust/nix-expr/src/value.rs index 4fb9b62..77ef048 100644 --- a/rust/nix-expr/src/value.rs +++ b/rust/nix-expr/src/value.rs @@ -67,7 +67,7 @@ impl Drop for Value { fn drop(&mut self) { unsafe { // ignoring error because the only failure mode is leaking memory - raw::gc_decref(null_mut(), self.inner.as_ptr()); + raw::value_decref(null_mut(), self.inner.as_ptr()); } } } @@ -77,7 +77,7 @@ impl Clone for Value { // this is very unlikely to error, and it is not recoverable // Maybe try without, and try again with context to report details? unsafe { - check_call!(raw::gc_incref(&mut Context::new(), self.inner.as_ptr())).unwrap(); + check_call!(raw::value_incref(&mut Context::new(), self.inner.as_ptr())).unwrap(); } // can't return an error here, but we don't want to ignore the error either as it means we could use-after-free Value { inner: self.inner } diff --git a/rust/nix-util/src/context.rs b/rust/nix-util/src/context.rs index f5db84d..55f0ab4 100644 --- a/rust/nix-util/src/context.rs +++ b/rust/nix-util/src/context.rs @@ -33,7 +33,7 @@ impl Context { /// We recommend to use `check_call!` if possible. pub fn check_err(&self) -> Result<()> { let err = unsafe { raw::err_code(self.inner.as_ptr()) }; - if err != raw::NIX_OK.try_into().unwrap() { + if err != raw::err_NIX_OK.try_into().unwrap() { // msgp is a borrowed pointer (pointing into the context), so we don't need to free it let msgp = unsafe { raw::err_msg(null_mut(), self.inner.as_ptr(), null_mut()) }; // Turn the i8 pointer into a Rust string by copying @@ -47,7 +47,7 @@ impl Context { unsafe { raw::set_err_msg( self.inner.as_ptr(), - raw::NIX_OK.try_into().unwrap(), + raw::err_NIX_OK.try_into().unwrap(), b"\0".as_ptr() as *const i8, ); } @@ -66,7 +66,7 @@ impl Context { f: F, ) -> Result> { let t = f(self.ptr()); - if unsafe { raw::err_code(self.inner.as_ptr()) == raw::NIX_ERR_KEY } { + if unsafe { raw::err_code(self.inner.as_ptr()) == raw::err_NIX_ERR_KEY } { self.clear(); return Ok(None); } @@ -109,7 +109,7 @@ macro_rules! check_call_opt_key { { let ctx : &mut $crate::context::Context = $ctx; let ret = $($f)::*(ctx.ptr(), $($arg,)*); - if unsafe { raw::err_code(ctx.ptr()) == raw::NIX_ERR_KEY } { + if unsafe { raw::err_code(ctx.ptr()) == raw::err_NIX_ERR_KEY } { ctx.clear(); return Ok(None); } @@ -140,7 +140,7 @@ mod tests { unsafe { raw::set_err_msg( ctx_ptr, - raw::NIX_ERR_UNKNOWN.try_into().unwrap(), + raw::err_NIX_ERR_UNKNOWN.try_into().unwrap(), b"dummy error message\0".as_ptr() as *const i8, ); } diff --git a/rust/nix-util/src/string_return.rs b/rust/nix-util/src/string_return.rs index 004f1ae..f06fa32 100644 --- a/rust/nix-util/src/string_return.rs +++ b/rust/nix-util/src/string_return.rs @@ -11,6 +11,15 @@ pub unsafe extern "C" fn callback_get_result_string( user_data: *mut std::os::raw::c_void, ) { let ret = user_data as *mut Result; + + if start == std::ptr::null() { + if n != 0 { + panic!("callback_get_result_string: start is null but n is not zero"); + } + *ret = Ok(String::new()); + return; + } + let slice = std::slice::from_raw_parts(start as *const u8, n as usize); if !(*ret).is_err() {