Panic when getting shape of BOM glyph from Windows' Consolas font
When getting shape of Byte Order Mark ('\u{feff}'
) of Windows' Consolas font, a panic occurs:
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', libcore\option.rs:345:21
[...]
11: core::option::Option<(usize, stb_truetype::{{impl}}::glyph_shape_positive_contours::FlagData)*>::unwrap<(usize, stb_truetype::{{impl}}::glyph_shape_positive_contours::FlagData)*>
at C:\projects\rust\src\libcore\macros.rs:20
12: stb_truetype::FontInfo<rusttype::SharedBytes>::glyph_shape_positive_contours<rusttype::SharedBytes>
at C:\Users\Martin\.cargo\registry\src\github.com-1ecc6299db9ec823\stb_truetype-0.2.4\src\lib.rs:911
13: stb_truetype::FontInfo<rusttype::SharedBytes>::get_glyph_shape<rusttype::SharedBytes>
at C:\Users\Martin\.cargo\registry\src\github.com-1ecc6299db9ec823\stb_truetype-0.2.4\src\lib.rs:655
[...]
More context can be found at https://github.com/jwilm/alacritty/issues/1674
I've tried the following patch:
diff --git a/src/lib.rs b/src/lib.rs
index 384ef78..8462d3f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -907,9 +907,9 @@ impl<Data: Deref<Target = [u8]>> FontInfo<Data> {
scx = x;
scy = y;
- let (next_flags, next_x, next_y) = {
- let peek = &iter.peek().unwrap().1;
- (peek.flags, peek.x, peek.y)
+ let (next_flags, next_x, next_y) = match &iter.peek() {
+ Some(peek) => (peek.1.flags, peek.1.x, peek.1.y)
+ None => break
};
if next_flags & 1 == 0 {
This obviously suppressed the panic, but I'm not sure it's the correct fix. If this is caused by invalid data in the font, maybe glyph_shape_positive_contours
should return an Option or Result to indicate this instead of panicking?