Make World::font implementations safe (#6117)

This commit is contained in:
Myriad-Dreamin 2025-03-31 16:08:55 +08:00 committed by GitHub
parent efdb75558f
commit 758ee78ef5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 6 additions and 4 deletions

View File

@ -210,7 +210,9 @@ impl World for SystemWorld {
} }
fn font(&self, index: usize) -> Option<Font> { fn font(&self, index: usize) -> Option<Font> {
self.fonts[index].get() // comemo's validation may invoke this function with an invalid index. This is
// impossible in typst-cli but possible if a custom tool mutates the fonts.
self.fonts.get(index)?.get()
} }
fn today(&self, offset: Option<i64>) -> Option<Datetime> { fn today(&self, offset: Option<i64>) -> Option<Datetime> {

View File

@ -97,7 +97,7 @@ impl World for TestWorld {
} }
fn font(&self, index: usize) -> Option<Font> { fn font(&self, index: usize) -> Option<Font> {
Some(self.base.fonts[index].clone()) self.base.fonts.get(index).cloned()
} }
fn today(&self, _: Option<i64>) -> Option<Datetime> { fn today(&self, _: Option<i64>) -> Option<Datetime> {

View File

@ -498,7 +498,7 @@ impl World for DocWorld {
} }
fn font(&self, index: usize) -> Option<Font> { fn font(&self, index: usize) -> Option<Font> {
Some(FONTS.1[index].clone()) FONTS.1.get(index).cloned()
} }
fn today(&self, _: Option<i64>) -> Option<Datetime> { fn today(&self, _: Option<i64>) -> Option<Datetime> {

View File

@ -67,7 +67,7 @@ impl World for TestWorld {
} }
fn font(&self, index: usize) -> Option<Font> { fn font(&self, index: usize) -> Option<Font> {
Some(self.base.fonts[index].clone()) self.base.fonts.get(index).cloned()
} }
fn today(&self, _: Option<i64>) -> Option<Datetime> { fn today(&self, _: Option<i64>) -> Option<Datetime> {