From 84cd925e979f59fded120fcacca1a7ea5608c766 Mon Sep 17 00:00:00 2001 From: Andreas Weinlein Date: Mon, 26 Aug 2024 11:15:37 +0200 Subject: [PATCH] Added NULL checks to DynamicStore --- system-configuration/examples/set_dns.rs | 4 +++- system-configuration/examples/watch_dns.rs | 7 +++++-- system-configuration/src/dynamic_store.rs | 24 ++++++++++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/system-configuration/examples/set_dns.rs b/system-configuration/examples/set_dns.rs index 157063c..6f8a188 100644 --- a/system-configuration/examples/set_dns.rs +++ b/system-configuration/examples/set_dns.rs @@ -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); diff --git a/system-configuration/examples/watch_dns.rs b/system-configuration/examples/watch_dns.rs index d145c2a..b15b730 100644 --- a/system-configuration/examples/watch_dns.rs +++ b/system-configuration/examples/watch_dns.rs @@ -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 = CFArray::from_CFTypes(&[]); let watch_patterns = @@ -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 }); diff --git a/system-configuration/src/dynamic_store.rs b/system-configuration/src/dynamic_store.rs index 444277a..66ecca4 100644 --- a/system-configuration/src/dynamic_store.rs +++ b/system-configuration/src/dynamic_store.rs @@ -102,7 +102,7 @@ impl SCDynamicStoreBuilder { } /// Create the dynamic store session. - pub fn build(mut self) -> SCDynamicStore { + pub fn build(mut self) -> Option { let store_options = self.create_store_options(); if let Some(callback_context) = self.callback_context.take() { SCDynamicStore::create( @@ -161,7 +161,7 @@ impl SCDynamicStore { store_options: &CFDictionary, callout: SCDynamicStoreCallBack, context: *mut SCDynamicStoreContext, - ) -> Self { + ) -> Option { unsafe { let store = SCDynamicStoreCreateWithOptions( kCFAllocatorDefault, @@ -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)) + } } } @@ -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)) } } } @@ -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 { 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)) + } } } }