harfbuzz / rustybuzz

A complete harfbuzz's shaping algorithm port to Rust
MIT License
551 stars 37 forks source link

Font::from_ptr causes segfaults due to faulty transmute #5

Closed CryZe closed 4 years ago

CryZe commented 4 years ago
    pub(crate) fn from_ptr(font: *const ffi::hb_font_t) -> &'static Font<'static> {
        unsafe { &*(font as *const Font) }
    }

This straight up transmutes a hb_font_t into a Font. However hb_font_t is not a Font, it's stored as a pointer in a Font. This causes a segfault here:

fn font_has_glyph(font: &Font, u: u32) -> bool {
    font.ttf_parser().glyph_index(char::try_from(u).unwrap()).is_some()
}

where it then tries to access the ttf_parser() from the font, which doesn't exist, as it's not a Font, but a transmuted hb_font_t

RazrFalcon commented 4 years ago

Yes, this is a pretty bad one and there no easy way to fix it. We have to wait for a first pure Rust release.

PS: the current state of the library is pre-alpha.

RazrFalcon commented 4 years ago

The repo was reset, so this was kinda fixed.