Panic: u16 overflow in name.rs when a name record's offset + length exceeds 65535

Open Beginner friendly
#30 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Read src/name.rs, especially subset_table around the two additions identified in the issue. Run the supplied reproduction or cargo test to observe the overflow, then check that malformed offsets return an error rather than panic and that accumulated string offsets do not wrap.

Written by the indexing model from the issue text.

Description

subset panics with attempt to add with overflow when a name record's offset + length exceeds 65535, in any build with overflow checks (debug builds, cargo test, fuzzing):

thread 'main' panicked at subsetter-0.2.6/src/name.rs:48:20:
attempt to add with overflow

subset_table in src/name.rs adds record.string_offset + record.length as u16 (line 48), and cur_storage_offset += record.length (line 53) can overflow the same way when the kept strings add up to more than 64 KiB. In release builds the first sum wraps, storage.get(..) returns None and the call ends in Err(SubsetError); the second can wrap silently and write wrong string offsets.

To reproduce with subsetter 0.2.6 (default-features = false) and the font below (696 bytes, a minimal TrueType font whose name table has a single record: platform 3, encoding 1, language 0x409, name ID 1, length 4, offset 0xFFFE):

fn main() {
    let data = std::fs::read("name-overflow.ttf").unwrap();
    let remapper = subsetter::GlyphRemapper::new();
    println!("{:?}", subsetter::subset(&data, 0, &remapper).map(|font| font.len()));
}

cargo run panics as above; cargo run --release prints Err(SubsetError).

name-overflow.ttf, base64 (decode with base64 -d > name-overflow.ttf)
AAEAAAAKAIAAAwAgT1MvMmKwXBUAAAEoAAAAYGNtYXABSgEcAAABnAAAAExnbHlmC4EjVwAAAgAA
AACAaGVhZMSNyOUAAACsAAAANmhoZWEF1AGWAAAA5AAAACRobXR4CMoAlgAAAYgAAAAUbG9jYQAA
AXAAAAHoAAAAGG1heHAACwANAAABCAAAACBuYW1lAFQEUgAAAoAAAAAWcG9zdP+fADIAAAKYAAAA
IAABAAAAAQAAIE34C18PPPUACwPoAAAAAHwlncAAAAAA5vHm7QAAAAACWANSAAAACAACAAEAAAAA
AAEAAAMg/zgAWgJYAAAAAAJYAAEAAAAAAAAAAAAAAAAAAAAFAAEAAAAFAAQAAQAGAAIAAgAAAAAA
AAAAAAAAAAACAAEABAHCAZAABQAAAooCWAAAAEsCigJYAAABXgAyAPoAAAAAAAAAAAAAAAAAAAAD
AAAAAAAAAAAAAAAAT1dSVABAACAAwQMg/zgAWgOEAPoAAAABAAAAAAH0ArwAAAAgAAAB9AAyAPoA
AAJYAAABLABkAlgAAAAAAAIAAAADAAAAFAADAAEAAAAUAAQAOAAAAAoACAACAAIAIABBALQAwf//
AAAAIABBALQAwf///+H/wf9P/0MAAQAAAAAAAAAAAAAAAAAAAAAAJAAAACQAAABEAAAAZAAAAIAA
AQAyAAABwgK8AAMAAAEBAQEAMgAAAZAAAAAAArwAAP1EAAAAAQAAAAACWAK8AAIAAAEBAQAAASwB
LAAAArz9RAAAAAABAGQCWAD6Au4AAgAAAQEBAGQAZAAyAlgAlv/OAAAA//8AAAAAAlgDUgAjAAIA
AAAAAAMAAwBkAGQAAAAAAAEAEgADAAEECQABAAT//gBBAEIAAAADAAAAAAAA/5wAMgAAAAAAAAAA
AAAAAAAAAAAAAAAA

Doing the arithmetic in usize or with checked_add, and returning MalformedFont when it does not fit, would make this an error. Found by fuzzing subsetter through the font crate of officewright; until it is fixed we call subset inside catch_unwind.

Dominant language
Rust
Stars
61
Forks
16
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from typst/subsetter

All issues in typst/subsetter

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.