Skip to content

Commit

Permalink
Added NULL checks to DynamicStore
Browse files Browse the repository at this point in the history
  • Loading branch information
aw-cf committed Aug 26, 2024
1 parent c7a1626 commit 84cd925
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
4 changes: 3 additions & 1 deletion system-configuration/examples/set_dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use system_configuration::{
// network interface to 8.8.8.8 and 8.8.4.4

fn main() {
let store = SCDynamicStoreBuilder::new("my-test-dyn-store").build();
let store = SCDynamicStoreBuilder::new("my-test-dyn-store")
.build()
.expect("Unable to create DynamicStore");
let primary_service_uuid = get_primary_service_uuid(&store).expect("No PrimaryService active");
println!("PrimaryService UUID: {}", primary_service_uuid);

Expand Down
7 changes: 5 additions & 2 deletions system-configuration/examples/watch_dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ fn main() {

let store = SCDynamicStoreBuilder::new("my-watch-dns-store")
.callback_context(callback_context)
.build();
.build()
.expect("Unable to create DynamicStore");

let watch_keys: CFArray<CFString> = CFArray::from_CFTypes(&[]);
let watch_patterns =
Expand All @@ -34,7 +35,9 @@ fn main() {
panic!("Unable to register notifications");
}

let run_loop_source = store.create_run_loop_source();
let run_loop_source = store
.create_run_loop_source()
.expect("Unable to create run loop source");
let run_loop = CFRunLoop::get_current();
run_loop.add_source(&run_loop_source, unsafe { kCFRunLoopCommonModes });

Expand Down
24 changes: 16 additions & 8 deletions system-configuration/src/dynamic_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<T> SCDynamicStoreBuilder<T> {
}

/// Create the dynamic store session.
pub fn build(mut self) -> SCDynamicStore {
pub fn build(mut self) -> Option<SCDynamicStore> {
let store_options = self.create_store_options();
if let Some(callback_context) = self.callback_context.take() {
SCDynamicStore::create(
Expand Down Expand Up @@ -161,7 +161,7 @@ impl SCDynamicStore {
store_options: &CFDictionary,
callout: SCDynamicStoreCallBack,
context: *mut SCDynamicStoreContext,
) -> Self {
) -> Option<Self> {
unsafe {
let store = SCDynamicStoreCreateWithOptions(
kCFAllocatorDefault,
Expand All @@ -170,7 +170,11 @@ impl SCDynamicStore {
callout,
context,
);
SCDynamicStore::wrap_under_create_rule(store)
if store.is_null() {
None
} else {
Some(SCDynamicStore::wrap_under_create_rule(store))
}
}
}

Expand All @@ -185,10 +189,10 @@ impl SCDynamicStore {
self.as_concrete_TypeRef(),
cf_pattern.as_concrete_TypeRef(),
);
if !array_ref.is_null() {
Some(CFArray::wrap_under_create_rule(array_ref))
} else {
if array_ref.is_null() {
None
} else {
Some(CFArray::wrap_under_create_rule(array_ref))
}
}
}
Expand Down Expand Up @@ -268,14 +272,18 @@ impl SCDynamicStore {
}

/// Creates a run loop source object that can be added to the application's run loop.
pub fn create_run_loop_source(&self) -> CFRunLoopSource {
pub fn create_run_loop_source(&self) -> Option<CFRunLoopSource> {
unsafe {
let run_loop_source_ref = SCDynamicStoreCreateRunLoopSource(
kCFAllocatorDefault,
self.as_concrete_TypeRef(),
0,
);
CFRunLoopSource::wrap_under_create_rule(run_loop_source_ref)
if run_loop_source_ref.is_null() {
None
} else {
Some(CFRunLoopSource::wrap_under_create_rule(run_loop_source_ref))
}
}
}
}
Expand Down

0 comments on commit 84cd925

Please sign in to comment.