Skip to content

Commit

Permalink
Merge pull request #22 from mach-kernel/120423/xpc-object-default
Browse files Browse the repository at this point in the history
xpc-sys: Don't xpc_release NULLs
  • Loading branch information
mach-kernel authored Dec 21, 2023
2 parents d99a5ba + 6a71d69 commit 3c2ccf3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion xpc-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "xpc-sys"
description = "Conveniently call routines with wrappers for xpc_pipe_routine() and go from Rust types to XPC objects and back!"
version = "0.4.2"
version = "0.5.0"
authors = ["David Stancu <[email protected]>"]
license = "MIT"
edition = "2018"
Expand Down
15 changes: 8 additions & 7 deletions xpc-sys/src/objects/xpc_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,14 @@ impl XPCObject {
}
}

impl Default for XPCObject {
fn default() -> Self {
Self(null_mut(), XPCType(null_mut()))
}
}

impl fmt::Display for XPCObject {
/// Use xpc_copy_description to show as a string, for
/// _xpc_type_dictionary contents are shown!
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let XPCObject(ptr, _) = self;

if *ptr == null_mut() {
write!(f, "XPCObject is NULL")
write!(f, "{:?} xpc_object_t is NULL", self)
} else {
let xpc_desc = unsafe { xpc_copy_description(*ptr) };
let cstr = unsafe { CStr::from_ptr(xpc_desc) };
Expand Down Expand Up @@ -220,6 +214,12 @@ impl Drop for XPCObject {
/// https://developer.apple.com/documentation/xpc/1505851-xpc_release
fn drop(&mut self) {
let XPCObject(ptr, _) = &self;

if *ptr == null_mut() {
log::info!("XPCObject xpc_object_t is NULL, not calling xpc_release()");
return
}

log::info!(
"XPCObject drop ({:p}, {}, {})",
*ptr,
Expand All @@ -229,6 +229,7 @@ impl Drop for XPCObject {
.map(|(r, xr)| format!("refs {} xrefs {}", r, xr))
.unwrap_or("refs ???".to_string()),
);

unsafe { xpc_release(*ptr) }
}
}
Expand Down
3 changes: 2 additions & 1 deletion xpc-sys/src/objects/xpc_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
_xpc_type_array, _xpc_type_bool, _xpc_type_dictionary, _xpc_type_double, _xpc_type_fd,
_xpc_type_int64, _xpc_type_mach_recv, _xpc_type_mach_send, _xpc_type_s, _xpc_type_shmem,
_xpc_type_string, _xpc_type_uint64, xpc_get_type, xpc_object_t, xpc_type_get_name, xpc_type_t,
_xpc_type_string, _xpc_type_uint64, xpc_get_type, xpc_object_t, xpc_type_get_name, xpc_type_t, _xpc_type_null,
};

use crate::objects::xpc_error::XPCError;
Expand Down Expand Up @@ -93,6 +93,7 @@ lazy_static! {
unsafe { (&_xpc_type_mach_recv as *const _xpc_type_s).into() };
pub static ref Fd: XPCType = unsafe { (&_xpc_type_fd as *const _xpc_type_s).into() };
pub static ref Shmem: XPCType = unsafe { (&_xpc_type_shmem as *const _xpc_type_s).into() };
pub static ref Null: XPCType = unsafe { (&_xpc_type_null as *const _xpc_type_s).into() };
}

/// Runtime type check for XPC object.
Expand Down

0 comments on commit 3c2ccf3

Please sign in to comment.