Skip to content

Commit

Permalink
[read-fonts] Fix possible crash in PackedPointNumbersIter
Browse files Browse the repository at this point in the history
This would only be encountered if the user manually tried to iterate
packed points with a single 0x0 byte (signifying that there are points
for all deltas) and did not also know how many points there were; but
there's no reason not to be more careful here and at least stop
iterating after u16::MAX items.
  • Loading branch information
cmyr committed Sep 27, 2023
1 parent f8a8ccf commit 45a7753
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion read-fonts/src/tables/variations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl Iterator for PackedPointNumbersIter<'_> {
// if our count is zero, we keep incrementing forever
if self.count == 0 {
let result = self.last_val;
self.last_val += 1;
self.last_val = self.last_val.checked_add(1)?;
return Some(result);
}

Expand Down Expand Up @@ -827,4 +827,13 @@ mod tests {
assert_eq!(points.total_len(), 4);
assert_eq!(data.len(), INPUT.len() - 4);
}

#[test]
fn packed_points_dont_panic() {
// a single '0' byte means that there are deltas for all points
static ALL_POINTS: FontData = FontData::new(&[0]);
let (all_points, _) = PackedPointNumbers::split_off_front(ALL_POINTS);
// in which case the iterator just keeps incrementing until u16::MAX
assert_eq!(all_points.iter().count(), u16::MAX as _);
}
}

0 comments on commit 45a7753

Please sign in to comment.