Skip to content

Reject fonts with head.unitsPerEm == 0 - #49

Open
1820893135-pixel wants to merge 1 commit into
tomolt:masterfrom
1820893135-pixel:fix/unitsperem-zero
Open

1820893135-pixel wants to merge 1 commit into
tomolt:masterfrom
1820893135-pixel:fix/unitsperem-zero

Conversation

@1820893135-pixel

Copy link
Copy Markdown

Fixes #47.

Root cause

init_font() stores head.unitsPerEm without validating it, and every metric and render path divides by it. With unitsPerEm == 0 those divisions produce inf/NaN, and glyph_bbox() then converts the non-finite results to int, which is undefined behaviour.

Fix

  • Reject the font in init_font() when unitsPerEm is zero.
  • In glyph_bbox(), compute the four scaled coordinates in double, reject non-finite values, and clamp finite out-of-range values to the int range before converting.

No effect on well-formed fonts.

Verification

Build with the command from the report, plus a driver that measures A-Z:

SFT_Font *font = sft_loadfile(argv[1]);
SFT sft = {0};
sft.font = font; sft.xScale = 16.0; sft.yScale = 16.0; sft.flags = SFT_DOWNWARD_Y;
for (SFT_UChar c = 'A'; c <= 'Z'; c++) {
    SFT_Glyph g; SFT_GMetrics m;
    if (sft_lookup(&sft, c, &g) == 0)
        sft_gmetrics(&sft, g, &m);
}
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 -lm

1. Reported input - 320-byte poc-unitsPerEm0.ttf:

base64 -d > poc-unitsPerEm0.ttf <<'EOF'
AAEAAAAHAAAAAAAAY21hcAAAAAAAAAB8AAAALGdseWYAAAAAAAAAqAAAAChoZWFkAAAAAAAAANAAAAA2aGhlYQAAAAAAAAEIAAAAJGhtdHgAAAAAAAABLAAAAARsb2NhAAAAAAAAATAAAAAGbWF4cAAAAAAAAAE4AAAABgAAAAEAAwABAAAADAAEACAAAAAEAAQAAQACAEH//wAAAEH////AAAAAAAAAAAAAAAABAAAAAAAKAAoAAwAAAQEBAQAAAAoAAP/2AAAAAAAKAAAAAAABAAAAAAAAAAAAAF8PPPUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAKAAAAAAAAAAAAAAAAAAAAAAMg/zgAAAPoA+gAAAAAAAAAAAAAAAAAAAAAAAAAAAABAlgAAAAAAAIAEwAAAABQAAACAAA=
EOF
sha256sum poc-unitsPerEm0.ttf  # 9b60a83ec9cd206cf6b919205c278d9b15916a10b53a951edf89b13ac2c5e411

Before:

schrift.c:1005:11: runtime error: -nan is outside the range of representable values of type 'int'
    #0 glyph_bbox    schrift.c:1005
    #1 sft_gmetrics  schrift.c:284

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.ttf and 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).

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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant