IndexSubtableArray::find_table() finds a glyph's bitmap by walking
the strike's whole IndexSubtableRecord array. The record count is a
32-bit field bounded only by the table length, and the walk is
charged to no work limit, so two callers run it once per glyph and
turn font size times text/glyph count into CPU time:
- hb-ot-font, on every extents/PNG/paint query, which stalls
hb_shape() (vertical text without VORG/vmtx measures each glyph by
its extents) and the extents/paint/PNG getters;
- the subsetter, for every output glyph of every strike in
IndexSubtableArray::build_lookup().
With a ~320 KB font one vertical hb_shape() of 2000 letters took ~31 s
against ~0.25 s for the same font with the record count set to 1, and
a ~303 KB font kept hb-subset --gids='*' busy for ~100 s.
build_lookup() now maps each retained glyph to the first record that
covers it in one pass over the records (a copy of the glyphset with
del_range() after each record keeps the first-covering-record rule),
instead of searching the array per glyph. Output is byte-identical.
The accelerator builds a bounded per-face index: for the first strike
of each ppem it records the disjoint (ppem, glyph range) -> record
index spans in a sorted vector that find_table() binary searches,
preserving the first-covering-record rule exactly. The index is
capped at HB_CBDT_MAX_INDEX_ENTRIES (a new limit in hb-limits.hh): a
strike whose records would overflow it, or that hits an allocation
failure, falls back to the original linear walk via walk_ppems, so
the index never exceeds the cap no matter how many records the
sanitizer accepted. BitmapSizeTable::find_table() is kept as that
fallback.
Known limitation: the cap is one shared budget, filled in sizeTables
order. A single ~512 KB filler strike of ~65535 one-glyph records
exhausts it, so every strike after it in the array keeps the full
per-glyph walk; a font under 3x the reproducer's size can still force
that walk for those later strikes. This is acceptable for a
non-security performance bug and keeps lookup results exact for every
font. Per-strike lazy indexing (build a strike's index on first use,
under its own cap) is the real follow-up and is left out here to keep
the change small.
Lookups are unchanged. The reproducer drops from ~31 s to ~0.39 s
with identical shaped output; the 303 KB subsetter case drops to
~0.07 s; the 197 MiB worst case (255 strikes at distinct ppems
sharing one 65535-record array) now peaks at ~18 MiB. Adds
test_hb_ot_color_png_cbdt_records (pins the first-covering-record
rule through the index) and test_subset_cbdt_many_records (a
65535-glyph, 64-strike, keep-everything subset that must finish
quickly). The full test suite passes; ASan/UBSan clean.
Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-YJXG98O5.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
IndexSubtableRecord::add_new_subtable() bounds-checks each retained
glyph's image block against the source CBDT table, but nothing
requires the blocks of different glyphs (or strikes) to be distinct
and nothing limits the total copied. A format 3 offsetArray of
0, 65535, 0, 65535, ... makes every even glyph copy the same 64 KB,
so a ~186 KB font makes one hb_subset_or_fail() peak at ~3.8 GB and
emit a ~1.97 GB font. The existing source_blob->length * 256 cap
covers only the serializer buffer holding the new CBLC, not the CBDT
side buffers.
Charge every block copied out of the source CBDT to a byte budget
that is proportional to the source table and shared across strikes,
and fail the strike when it runs out; the existing subset_size_table()
path then reverts that strike and continues. The budget lives in
cbdt_dedup_context_t, which CBLC::subset already shares across every
strike, and is set to cbdt_length * HB_SUBSET_CBDT_MAX_COPY_FACTOR, a
new limit (64) in hb-limits.hh. The charge is taken before the copy
into the per-strike buffer, so strikes that end up sharing data are
bounded as well, and it covers index formats 1/2/3 and overlapping
records, not only non-monotonic format 3 arrays. The budget defaults
to unlimited, so the direct users of the struct (test-cbdt-cache) are
unaffected; a well-formed font copies each byte at most once per
strike, so only a font whose strikes reference the table more than 64
times over loses bitmaps.
This is distinct from the integer-overflow hardening of
_copy_data_to_cbdt() in PR #6485: that bounds the final append to
cbdt_prime, this bounds the overlap amplification before the copy.
Adds test_subset_cbdt_overlapping_images, which builds the format 3
shape above and asserts the output CBDT stays within the factor
(524292 > 65536 on the unpatched tree). The api and subset suites
pass and the reproducer drops from 3767 MiB to 17 MiB.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-C39OXNU0.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>