diff options
| author | Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> | 2025-03-31 16:08:55 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-31 08:08:55 +0000 |
| commit | 758ee78ef57ebbaadacc50817620a540bcf8beeb (patch) | |
| tree | 5afc87df894c3d147971a6c660069176e4ba2d2f | |
| parent | efdb75558f20543af39f75fb88b3bae59b20e2e8 (diff) | |
Make `World::font` implementations safe (#6117)
| -rw-r--r-- | crates/typst-cli/src/world.rs | 4 | ||||
| -rw-r--r-- | crates/typst-ide/src/tests.rs | 2 | ||||
| -rw-r--r-- | docs/src/html.rs | 2 | ||||
| -rw-r--r-- | tests/src/world.rs | 2 |
4 files changed, 6 insertions, 4 deletions
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<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> { 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<Font> { - Some(self.base.fonts[index].clone()) + self.base.fonts.get(index).cloned() } fn today(&self, _: Option<i64>) -> Option<Datetime> { diff --git a/docs/src/html.rs b/docs/src/html.rs index 9077d5c4..9c02f08e 100644 --- a/docs/src/html.rs +++ b/docs/src/html.rs @@ -498,7 +498,7 @@ impl World for DocWorld { } fn font(&self, index: usize) -> Option<Font> { - Some(FONTS.1[index].clone()) + FONTS.1.get(index).cloned() } fn today(&self, _: Option<i64>) -> Option<Datetime> { diff --git a/tests/src/world.rs b/tests/src/world.rs index 9e0e91ad..fe2bd45e 100644 --- a/tests/src/world.rs +++ b/tests/src/world.rs @@ -67,7 +67,7 @@ impl World for TestWorld { } 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> { |
