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

Use core-foundation-sys instead of core-foundation #50

Merged
merged 1 commit into from Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -18,7 +18,7 @@ fallback = []
android_system_properties = "0.1.4"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation = "0.9"
core-foundation-sys = "0.8.3"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.9", features = ["activation", "combaseapi", "objbase", "roapi", "winerror", "winstring"] }
Expand Down
42 changes: 39 additions & 3 deletions src/tz_macos.rs
@@ -1,5 +1,41 @@
use core_foundation_sys::base::{CFRelease, CFTypeRef};
use core_foundation_sys::string::{kCFStringEncodingUTF8, CFStringGetCStringPtr};
use core_foundation_sys::timezone::{CFTimeZoneCopySystem, CFTimeZoneGetName};

pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
Ok(core_foundation::timezone::CFTimeZone::system()
.name()
.to_string())
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)
}
})
.and_then(|name| std::ffi::CStr::from_ptr(name).to_str().ok())
.map(|name| name.to_owned())
Comment on lines +17 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I am not exactly sure. An implementation that's guaranteed to be sane should be:

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() {
                    if let Ok(name) = std::ffi::CStr::from_ptr(name).to_str() {
                        return Ok(name.to_owned());
                    }
                }
            }
        }
    }
    Err(crate::GetTimezoneError::OsError)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be double sure I'd promote the std::ffi::CStr::from_ptr(name) to a named binding before the if let instead of a temporary in the if let.

.ok_or(crate::GetTimezoneError::OsError)
}
}

struct Dropping<T>(*const T);

impl<T> Drop for Dropping<T> {
#[inline]
fn drop(&mut self) {
unsafe { CFRelease(self.0 as CFTypeRef) };
}
}

impl<T> Dropping<T> {
#[inline]
unsafe fn new(v: *const T) -> Option<Self> {
if v.is_null() {
None
} else {
Some(Self(v))
}
}
}