Skip to content

Avoid u16 underflow in xmtx advance for fonts with no long metrics - #136

Open
youdie006 wants to merge 1 commit into
dfrg:mainfrom
youdie006:fix/xmtx-advance-zero-metrics-underflow
Open

Avoid u16 underflow in xmtx advance for fonts with no long metrics#136
youdie006 wants to merge 1 commit into
dfrg:mainfrom
youdie006:fix/xmtx-advance-zero-metrics-underflow

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #133.

xmtx::advance() computes the offset of the last long metric with (long_metric_count - 1) * 4 (src/internal/xmtx.rs:14).

A font whose numberOfHMetrics is 0 makes long_metric_count == 0, so long_metric_count - 1 underflows the u16: it panics in debug builds (attempt to subtract with overflow at xmtx.rs:14) and wraps to 65535 in release builds (an out-of-range read that unwrap_or(0) then masks). The minimal 12-byte font in #133 is accepted by FontRef::from_index, so GlyphMetrics::advance_width reaches this path.

sb() is not affected — its else branch subtracts long_metric_count from glyph_id (which is >= long_metric_count there), so it cannot underflow.

Fix: use long_metric_count.saturating_sub(1), so a zero metric count clamps to offset 0 (which reads no metric and returns 0) instead of underflowing.

Test: added a unit test in xmtx.rs calling advance(&[], 0, 0, _). Verified red before the fix (panics with attempt to subtract with overflow) and green after. cargo test (1 + 17 passed), cargo fmt --check, and cargo clippy (no new warnings) pass locally.


Disclosure: developed with the assistance of Claude Code (AI); reviewed and verified by me.

advance() computes the offset of the last long metric as
(long_metric_count - 1) * 4. A font with numberOfHMetrics == 0 makes
long_metric_count 0, so the subtraction underflows: it panics in debug
builds (attempt to subtract with overflow) and wraps to 65535 in
release builds, both from a font that FontRef accepts.

Use saturating_sub(1) so a zero metric count clamps to offset 0 (which
reads no metric and returns 0) instead of underflowing.
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.

GlyphMetrics::advance_width panics on a minimal font accepted by FontRef::from_index

1 participant