From 83927686d9eafc772c68e24c078a873a2a5ca969 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Mon, 28 Oct 2024 15:23:43 +0100 Subject: [PATCH] Fix wrong drop order for self-referential struct (#5306) Co-authored-by: Laurenz --- crates/typst-library/src/text/font/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/typst-library/src/text/font/mod.rs b/crates/typst-library/src/text/font/mod.rs index 09837312a..afa92e778 100644 --- a/crates/typst-library/src/text/font/mod.rs +++ b/crates/typst-library/src/text/font/mod.rs @@ -29,10 +29,6 @@ pub struct Font(Arc); /// The internal representation of a font. struct Repr { - /// The raw font data, possibly shared with other fonts from the same - /// collection. The vector's allocation must not move, because `ttf` points - /// into it using unsafe code. - data: Bytes, /// The font's index in the buffer. index: u32, /// Metadata about the font. @@ -43,6 +39,14 @@ struct Repr { ttf: ttf_parser::Face<'static>, /// The underlying rustybuzz face. rusty: rustybuzz::Face<'static>, + // NOTE: `ttf` and `rusty` reference `data`, so it's important for `data` + // to be dropped after them or they will be left dangling while they're + // dropped. Fields are dropped in declaration order, so `data` needs to be + // declared after `ttf` and `rusty`. + /// The raw font data, possibly shared with other fonts from the same + /// collection. The vector's allocation must not move, because `ttf` points + /// into it using unsafe code. + data: Bytes, } impl Font {