Skip to content

Fix unwrap panic in extract_number_for_key in impl TraitAccessorPrivate for CTFontTraits on macOS Ventura. #735

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 2 commits into from
Jul 13, 2025
Merged
Changes from all 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
19 changes: 18 additions & 1 deletion core-text/src/font_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use core_foundation::url::{CFURLRef, CFURL};
use core_foundation::{declare_TCFType, impl_CFTypeDescription, impl_TCFType};
use core_graphics::base::CGFloat;

use core_foundation::boolean::CFBoolean;
use std::path::PathBuf;

/*
Expand Down Expand Up @@ -144,7 +145,23 @@ trait TraitAccessorPrivate {
impl TraitAccessorPrivate for CTFontTraits {
fn extract_number_for_key(&self, key: CFStringRef) -> CFNumber {
let cftype = self.get(key);
cftype.downcast::<CFNumber>().unwrap()
let number = cftype.downcast::<CFNumber>();
match number {
Some(number) => number,
None => {
// The value was not able to be converted to a CFNumber, this violates the Core
// Foundation's docs (see https://developer.apple.com/documentation/coretext/kctfontsymbolictrait)
// but can occur in practice with certain fonts in MacOS 13 (Ventura). When this
// does occur in Ventura, the value returned is always a CFBoolean, so we attempt to
// convert into a boolean and create a number from there.
let value_as_bool = bool::from(
cftype
.downcast::<CFBoolean>()
.expect("Should be able to convert value into CFBoolean"),
);
CFNumber::from(value_as_bool as i32)
}
}
}
}

Expand Down