Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added null checks to dynamic store APIs #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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