Skip to content

Commit

Permalink
Fix potential use after in MacOS / iOS impl
Browse files Browse the repository at this point in the history
Per Ryan Lopopolo's review:

> This bit makes me a bit scared with the `Dropping` type and the `CStr`
  being dropped while a borrowed `&str` is taken from name. Is the
  `.map(|name| name.to_owned())` a use after free?
>
> To be sure, I'd probably restructure all of these combinators to use
  short circuit return to make sure the `Dropping` and `CStr` wrappers
  are dropped in the right order.

<strawlab#50 (comment)>
  • Loading branch information
Kijewski committed Aug 14, 2022
1 parent fe425a8 commit b072fca
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/tz_macos.rs
Expand Up @@ -3,20 +3,21 @@ use core_foundation_sys::string::{kCFStringEncodingUTF8, CFStringGetCStringPtr};
use core_foundation_sys::timezone::{CFTimeZoneCopySystem, CFTimeZoneGetName};

pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
unsafe {
Dropping::new(CFTimeZoneCopySystem())
.and_then(|tz| Dropping::new(CFTimeZoneGetName(tz.0)))
.and_then(|name| {
let name = CFStringGetCStringPtr(name.0, kCFStringEncodingUTF8);
if name.is_null() {
None
} else {
Some(name)
pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
unsafe {
if let Some(tz) = Dropping::new(CFTimeZoneCopySystem()) {
if let Some(name) = Dropping::new(CFTimeZoneGetName(tz.0)) {
let name = CFStringGetCStringPtr(name.0, kCFStringEncodingUTF8);
if !name.is_null() {
let name = std::ffi::CStr::from_ptr(name);
if let Ok(name) = name.to_str() {
return Ok(name.to_owned());
}
}
}
})
.and_then(|name| std::ffi::CStr::from_ptr(name).to_str().ok())
.map(|name| name.to_owned())
.ok_or(crate::GetTimezoneError::OsError)
}
}
Err(crate::GetTimezoneError::OsError)
}
}

Expand Down

0 comments on commit b072fca

Please sign in to comment.