From 758ee78ef57ebbaadacc50817620a540bcf8beeb Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Mon, 31 Mar 2025 16:08:55 +0800 Subject: Make `World::font` implementations safe (#6117) --- crates/typst-cli/src/world.rs | 4 +++- crates/typst-ide/src/tests.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs index 12e80d27..2da03d4d 100644 --- a/crates/typst-cli/src/world.rs +++ b/crates/typst-cli/src/world.rs @@ -210,7 +210,9 @@ impl World for SystemWorld { } fn font(&self, index: usize) -> Option { - 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) -> Option { diff --git a/crates/typst-ide/src/tests.rs b/crates/typst-ide/src/tests.rs index 6678ab84..c6d733ca 100644 --- a/crates/typst-ide/src/tests.rs +++ b/crates/typst-ide/src/tests.rs @@ -97,7 +97,7 @@ impl World for TestWorld { } fn font(&self, index: usize) -> Option { - Some(self.base.fonts[index].clone()) + self.base.fonts.get(index).cloned() } fn today(&self, _: Option) -> Option { -- cgit v1.2.3