Skip to content

Fix header compressed size check by checking against an aligned size - #6

Open
Kanabenki wants to merge 1 commit into
Cimpress-MCP:mainfrom
Kanabenki:fix-size-check
Open

Fix header compressed size check by checking against an aligned size#6
Kanabenki wants to merge 1 commit into
Cimpress-MCP:mainfrom
Kanabenki:fix-size-check

Conversation

@Kanabenki

Copy link
Copy Markdown

Closes #4.

It seems the size check would often fail on some fonts as the size of the compressed section provided by the header might not take into account the 4-byte alignment of the section. The comparison is now done between the actual consumed input size after processing the compressed section (the input buffer is consumed until after the potential padding bytes), and the size reported by the header aligned to the next multiple of 4.

The spec seems to indicate that the compressed section should always be padded to 4 byte alignment if followed by an extended metadata block or a private data block, but I'm not totally sure if it enforces this alignment when the compressed block is the last one. However on the font files I checked, the compressed block was always padded even if nothing followed.

This PR also add some derives on the public error type that might be useful to the crate consumers.

I tested with a few fonts (notably those listed in #4) and this seems to be working properly.

virtualritz added a commit to virtualritz/woff2-rs2 that referenced this pull request Oct 24, 2025
This commit addresses multiple robustness and maintainability issues.

Error Handling Improvements:
- Preserve full error chain in DecodeError instead of flattening to strings.
- Add typed error variants (InvalidHeader, InvalidCollectionHeader, etc.).
- Replace unwrap() with try_from() for safe size conversions.
- Add SizeOverflow error for conversion failures.
- Add OffsetOverflow error for table offset calculations.

Performance Improvements:
- Fix O(n²) complexity in Woff2TableDirectory::write_to_buf.
- Use indexed iteration instead of iterator cloning.

Safety & Validation:
- Add overflow checks in Woff2TableDirectory::from_buf using checked_add.
- Fix TableDirectory::new to handle zero tables gracefully with assertion.
- Enforce WOFF2 spec requirement: loca must immediately follow glyf in collections.
- Add LocaNotAdjacentToGlyf error for spec violations.

API Naming (Rust API Guidelines):
- Remove get_ prefix from getters: get_four_cc → four_cc.
- Rename try_get_four_cc → try_four_cc.
- Rename try_get_base_128 → try_base_128.
- Rename try_get_255_u16 → try_255_u16.
- Rename get_source_range → source_range.
- Rename get_range → range.

Merged Upstream PRs:
- PR #8: Fix calculation of overlap_simple_bit_stream_size.
  Cimpress-MCP/woff2-rs#8
- PR #9: Fix error conversion issues for modern Rust compatibility.
  Cimpress-MCP/woff2-rs#9
- PR #6: Fix header compressed size check with alignment.
  Cimpress-MCP/woff2-rs#6

Documentation:
- Fix doctest to use correct crate name (woff2_patched).
- Update TableDirectory::new documentation with panic conditions.
- Clarify glyf/loca adjacency requirements in comments.

All tests passing (11 unit tests + 1 doctest).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Converting Bootstrap woff2 to ttf leads to error

1 participant