Repository navigation
Reject fonts with head.unitsPerEm == 0 - #49
Open
1820893135-pixel wants to merge 1 commit into
Open
1820893135-pixel wants to merge 1 commit into
1820893135-pixel wants to merge 1 commit into
Conversation
unitsPerEm is a 16-bit field read from the font and used as a divisor in sft_lmetrics(), sft_gmetrics(), sft_kerning() and sft_render(). When it is zero those divisions produce inf/NaN, and glyph_bbox() converts the non-finite results to int, which is undefined behaviour. Reject the font in init_font() when the field is zero. Also make the conversion in glyph_bbox() total: reject non-finite coordinates and clamp finite ones to the int range instead of casting out-of-range values. No effect on well-formed fonts.
This was referenced Oct 24, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #47.
Root cause
init_font()storeshead.unitsPerEmwithout validating it, and every metric and render path divides by it. WithunitsPerEm == 0those divisions produce inf/NaN, andglyph_bbox()then converts the non-finite results toint, which is undefined behaviour.Fix
init_font()whenunitsPerEmis zero.glyph_bbox(), compute the four scaled coordinates indouble, reject non-finite values, and clamp finite out-of-range values to theintrange before converting.No effect on well-formed fonts.
Verification
Build with the command from the report, plus a driver that measures
A-Z:clang -O1 -g -std=c99 -pedantic -D_POSIX_C_SOURCE=1 \ -fsanitize=address,undefined -fno-sanitize=signed-integer-overflow \ -I. driver.c schrift.c -o repro_fcast -lm1. Reported input - 320-byte
poc-unitsPerEm0.ttf:Before:
After:
sft_loadfile()returns NULL (the font is rejected), the driver prints[repro] cannot load ...and exits 2; no sanitizer report.2. No regression - rendered
resources/Ubuntu-Regular.ttf,resources/FiraGO-Regular.ttfand DejaVu Sans at 8..40 px for every ASCII glyph:sft_lmetrics(),sft_gmetrics(),sft_kerning()values and the FNV hash of every rendered bitmap are byte-identical to the unmodified library (4275 output lines).