Skip to content

Commit 84cd925

Browse files
committed
Added NULL checks to DynamicStore
1 parent c7a1626 commit 84cd925

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

system-configuration/examples/set_dns.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use system_configuration::{
1414
// network interface to 8.8.8.8 and 8.8.4.4
1515

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

system-configuration/examples/watch_dns.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ fn main() {
2222

2323
let store = SCDynamicStoreBuilder::new("my-watch-dns-store")
2424
.callback_context(callback_context)
25-
.build();
25+
.build()
26+
.expect("Unable to create DynamicStore");
2627

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

37-
let run_loop_source = store.create_run_loop_source();
38+
let run_loop_source = store
39+
.create_run_loop_source()
40+
.expect("Unable to create run loop source");
3841
let run_loop = CFRunLoop::get_current();
3942
run_loop.add_source(&run_loop_source, unsafe { kCFRunLoopCommonModes });
4043

system-configuration/src/dynamic_store.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<T> SCDynamicStoreBuilder<T> {
102102
}
103103

104104
/// Create the dynamic store session.
105-
pub fn build(mut self) -> SCDynamicStore {
105+
pub fn build(mut self) -> Option<SCDynamicStore> {
106106
let store_options = self.create_store_options();
107107
if let Some(callback_context) = self.callback_context.take() {
108108
SCDynamicStore::create(
@@ -161,7 +161,7 @@ impl SCDynamicStore {
161161
store_options: &CFDictionary,
162162
callout: SCDynamicStoreCallBack,
163163
context: *mut SCDynamicStoreContext,
164-
) -> Self {
164+
) -> Option<Self> {
165165
unsafe {
166166
let store = SCDynamicStoreCreateWithOptions(
167167
kCFAllocatorDefault,
@@ -170,7 +170,11 @@ impl SCDynamicStore {
170170
callout,
171171
context,
172172
);
173-
SCDynamicStore::wrap_under_create_rule(store)
173+
if store.is_null() {
174+
None
175+
} else {
176+
Some(SCDynamicStore::wrap_under_create_rule(store))
177+
}
174178
}
175179
}
176180

@@ -185,10 +189,10 @@ impl SCDynamicStore {
185189
self.as_concrete_TypeRef(),
186190
cf_pattern.as_concrete_TypeRef(),
187191
);
188-
if !array_ref.is_null() {
189-
Some(CFArray::wrap_under_create_rule(array_ref))
190-
} else {
192+
if array_ref.is_null() {
191193
None
194+
} else {
195+
Some(CFArray::wrap_under_create_rule(array_ref))
192196
}
193197
}
194198
}
@@ -268,14 +272,18 @@ impl SCDynamicStore {
268272
}
269273

270274
/// Creates a run loop source object that can be added to the application's run loop.
271-
pub fn create_run_loop_source(&self) -> CFRunLoopSource {
275+
pub fn create_run_loop_source(&self) -> Option<CFRunLoopSource> {
272276
unsafe {
273277
let run_loop_source_ref = SCDynamicStoreCreateRunLoopSource(
274278
kCFAllocatorDefault,
275279
self.as_concrete_TypeRef(),
276280
0,
277281
);
278-
CFRunLoopSource::wrap_under_create_rule(run_loop_source_ref)
282+
if run_loop_source_ref.is_null() {
283+
None
284+
} else {
285+
Some(CFRunLoopSource::wrap_under_create_rule(run_loop_source_ref))
286+
}
279287
}
280288
}
281289
}

0 commit comments

Comments
 (0)