Skip to content

Commit

Permalink
Fix subtraction overflow when comparing weights
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Jan 18, 2024
1 parent 8457e68 commit 6aadfad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/font/fallback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<'a> Iterator for FontFallbackIter<'a> {
let font_match_keys_iter = |is_mono| {
self.font_match_keys
.iter()
.filter(move |m_key| m_key.weight_offset == 0 || is_mono)
.filter(move |m_key| m_key.weight_offset == Some(0) || is_mono)
};

while self.default_i < self.default_families.len() {
Expand All @@ -157,7 +157,7 @@ impl<'a> Iterator for FontFallbackIter<'a> {
if let Some(font) = self.font_system.get_font(m_key.id) {
if !is_mono {
return Some(font);
} else if m_key.weight_offset == 0 {
} else if m_key.weight_offset == Some(0) {
// Default font
let fallback_info = MonospaceFallbackInfo {
weight_offset: None,
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'a> Iterator for FontFallbackIter<'a> {
.count();

let fallback_info = MonospaceFallbackInfo {
weight_offset: Some(m_key.weight_offset),
weight_offset: m_key.weight_offset,
script_non_matches: Some(script_non_matches),
id: m_key.id,
};
Expand Down
4 changes: 2 additions & 2 deletions src/font/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use rustybuzz;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FontMatchKey {
pub(crate) weight_offset: u16,
pub(crate) weight_offset: Option<u16>,
pub(crate) id: fontdb::ID,
}

Expand Down Expand Up @@ -144,7 +144,7 @@ impl FontSystem {
.faces()
.filter(|face| attrs.matches(face))
.map(|face| FontMatchKey {
weight_offset: attrs.weight.0 - face.weight.0,
weight_offset: attrs.weight.0.checked_sub(face.weight.0),
id: face.id,
})
.collect::<Vec<_>>();
Expand Down

4 comments on commit 6aadfad

@MoSal
Copy link
Contributor

@MoSal MoSal commented on 6aadfad Jan 18, 2024

Choose a reason for hiding this comment

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

Good catch. But note that overflow would have never occurred with the current code because of the <= restriction from attribute matches.

@jackpot51
Copy link
Member Author

Choose a reason for hiding this comment

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

It did occur, with emoji fonts.

@MoSal
Copy link
Contributor

@MoSal MoSal commented on 6aadfad Jan 18, 2024

Choose a reason for hiding this comment

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

It did occur, with emoji fonts.

Ouch. Apologies for missing that.

@jackpot51
Copy link
Member Author

Choose a reason for hiding this comment

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

No problem!

Please sign in to comment.