Skip to content

Mark schedule_with_runloop/unschedule_from_runloop as unsafe #47

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

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Changes from 2 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -24,6 +24,9 @@ Line wrap the file at 100 chars. Th
## [Unreleased]
### Changed
- Bump minimum supported Rust version (MSRV) to 1.64.0.
- Breaking: Mark `SCNetworkReachability::schedule_with_runloop` and `unschedule_from_runloop` as
`unsafe`. They accept a raw pointer that it dereferences. Figuring out a safe API around this is
left as an exercise for the future.

### Fixed
- Fix memory leak in `SCNetworkReachability::set_callback`.
79 changes: 48 additions & 31 deletions system-configuration/src/network_reachability.rs
Original file line number Diff line number Diff line change
@@ -186,7 +186,12 @@ impl SCNetworkReachability {
/// See [`SCNetworkReachabilityScheduleFromRunLoop`] for details.
///
/// [`SCNetworkReachabilityScheduleFromRunLoop`]: https://developer.apple.com/documentation/systemconfiguration/1514894-scnetworkreachabilityschedulewit?language=objc
pub fn schedule_with_runloop(
///
/// # Safety
///
/// The `run_loop_mode` must not be NULL and must be a pointer to a valid run loop mode.
/// Use `core_foundation::runloop::kCFRunLoopCommonModes` if you are unsure.
pub unsafe fn schedule_with_runloop(
&self,
run_loop: &CFRunLoop,
run_loop_mode: CFStringRef,
@@ -210,7 +215,12 @@ impl SCNetworkReachability {
/// See [`SCNetworkReachabilityUnscheduleFromRunLoop`] for details.
///
/// [`SCNetworkReachabilityUnscheduleFromRunLoop`]: https://developer.apple.com/documentation/systemconfiguration/1514899-scnetworkreachabilityunschedulef?language=objc
pub fn unschedule_from_runloop(
///
/// # Safety
///
/// The `run_loop_mode` must not be NULL and must be a pointer to a valid run loop mode.
/// Use `core_foundation::runloop::kCFRunLoopCommonModes` if you are unsure.
pub unsafe fn unschedule_from_runloop(
&self,
run_loop: &CFRunLoop,
run_loop_mode: CFStringRef,
@@ -383,14 +393,15 @@ mod test {
addr
);
reachability.set_callback(|_| {}).unwrap();
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), unsafe { kCFRunLoopCommonModes })
.unwrap();
reachability
.unschedule_from_runloop(&CFRunLoop::get_current(), unsafe {
kCFRunLoopCommonModes
})
.unwrap();
// SAFETY: We use the Apple provided run_loop_mode kCFRunLoopCommonModes
unsafe {
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), kCFRunLoopCommonModes)
.unwrap();
reachability
.unschedule_from_runloop(&CFRunLoop::get_current(), kCFRunLoopCommonModes)
.unwrap();
}
}
}

@@ -414,14 +425,15 @@ mod test {
remote
);
reachability.set_callback(|_| {}).unwrap();
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), unsafe { kCFRunLoopCommonModes })
.unwrap();
reachability
.unschedule_from_runloop(&CFRunLoop::get_current(), unsafe {
kCFRunLoopCommonModes
})
.unwrap();
// SAFETY: We use the Apple provided run_loop_mode kCFRunLoopCommonModes
unsafe {
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), kCFRunLoopCommonModes)
.unwrap();
reachability
.unschedule_from_runloop(&CFRunLoop::get_current(), kCFRunLoopCommonModes)
.unwrap();
}
}
}

@@ -435,16 +447,18 @@ mod test {
match SCNetworkReachability::from_host(&input) {
Some(mut reachability) => {
reachability.set_callback(|_| {}).unwrap();
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), unsafe {
kCFRunLoopCommonModes
})
.unwrap();
reachability
.unschedule_from_runloop(&CFRunLoop::get_current(), unsafe {
kCFRunLoopCommonModes
})
.unwrap();
// SAFETY: We use the Apple provided run_loop_mode kCFRunLoopCommonModes
unsafe {
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), kCFRunLoopCommonModes)
.unwrap();
reachability
.unschedule_from_runloop(
&CFRunLoop::get_current(),
kCFRunLoopCommonModes,
)
.unwrap();
}
}
None => {
panic!(
@@ -471,9 +485,12 @@ mod test {
let mut reachability =
SCNetworkReachability::from("0.0.0.0:0".parse::<SocketAddr>().unwrap());
reachability.set_callback(|_| {}).unwrap();
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), unsafe { kCFRunLoopCommonModes })
.unwrap();
// SAFETY: We use the Apple provided run_loop_mode kCFRunLoopCommonModes
unsafe {
reachability
.schedule_with_runloop(&CFRunLoop::get_current(), kCFRunLoopCommonModes)
.unwrap();
}
reachability.set_callback(|_| {}).unwrap();
let _ = tx.send(reachability);
CFRunLoop::run_current();