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

Avoid various overflows in CFF #1196

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion read-fonts/src/tables/postscript/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Stack {
.iter_mut()
.zip(&mut self.value_is_fixed)
{
sum += if *is_fixed {
let fixed_value = if *is_fixed {
// FreeType reads delta values using cff_parse_num which
// which truncates the fractional parts of 16.16 values
// See delta parsing:
Expand All @@ -224,6 +224,12 @@ impl Stack {
} else {
Fixed::from_i32(*value)
};
// See <https://github.com/googlefonts/fontations/issues/1193>
// The "DIN Alternate" font contains incorrect blue values
// that cause an overflow in this computation. FreeType does
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: Would you have a link to the equivalent logic in FreeType similar to this delta accumulation here?

Copy link
Member Author

Choose a reason for hiding this comment

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

The equivalent logic is here: https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/cff/cffparse.c#L1431 along with the "fixed" case below. I'll add this link to the code.

// not use checked arithmetic so we need to explicitly use
// wrapping behavior to produce matching outlines.
sum = sum.wrapping_add(fixed_value);
*value = sum.to_bits();
*is_fixed = true;
}
Expand Down
11 changes: 7 additions & 4 deletions skrifa/src/outline/cff/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,17 @@ impl HintState {
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psblues.c#L465>
fn capture(&self, bottom_edge: &mut Hint, top_edge: &mut Hint) -> bool {
// We use some wrapping arithmetic on this value below to avoid panics
// on overflow and match FreeType's behavior
// See <https://github.com/googlefonts/fontations/issues/1193>
let fuzz = self.blue_fuzz;
let mut captured = false;
let mut adjustment = Fixed::ZERO;
for zone in self.zones() {
if zone.is_bottom
&& bottom_edge.is_bottom()
&& (zone.cs_bottom_edge - fuzz) <= bottom_edge.cs_coord
&& bottom_edge.cs_coord <= (zone.cs_top_edge + fuzz)
&& zone.cs_bottom_edge.wrapping_sub(fuzz) <= bottom_edge.cs_coord
&& bottom_edge.cs_coord <= zone.cs_top_edge.wrapping_add(fuzz)
drott marked this conversation as resolved.
Show resolved Hide resolved
{
// Bottom edge captured by bottom zone.
adjustment = if self.suppress_overshoot {
Expand All @@ -294,8 +297,8 @@ impl HintState {
}
if !zone.is_bottom
&& top_edge.is_top()
&& (zone.cs_bottom_edge - fuzz) <= top_edge.cs_coord
&& top_edge.cs_coord <= (zone.cs_top_edge + fuzz)
&& zone.cs_bottom_edge.wrapping_sub(fuzz) <= top_edge.cs_coord
&& top_edge.cs_coord <= zone.cs_top_edge.wrapping_add(fuzz)
{
// Top edge captured by top zone.
adjustment = if self.suppress_overshoot {
drott marked this conversation as resolved.
Show resolved Hide resolved
Expand Down