[subset] Look up each retained glyph's ClipRecord by bsearch in ClipList::subset

ClipList::subset() walked startGlyphID..endGlyphID of every ClipRecord,
which is only O(num_glyphs) overall when the records are sorted and
disjoint.  ClipList::sanitize() checks neither, so R overlapping records
each spanning all glyphs cost R x 65535 iterations: a 332 KB font with
10000 whole-range records took about 20 s of CPU in hb-subset whatever
glyphs or text the request asked for.

Invert the loop: iterate the retained glyph set once and find each
glyph's record with clips.as_array ().bsearch (gid), exactly how
ClipList::get_extents() resolves a glyph's clip box when rendering, and
the same shape as the COLRv0 path in COLR::subset().  Cost is now
O(|glyphset| log R) regardless of record overlap.  For well-formed
(sorted, disjoint) lists the output is unchanged; for malformed
overlapping/unsorted lists the chosen record can differ (bsearch instead
of last-record-wins), which makes the subset agree with the clip box
HarfBuzz itself renders from the source font.

Adds an extended-format hb-subset-fuzzer input (5000 records covering
all 65535 glyphs) that the subset-fuzzer tests pick up.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MRSCGMJ7.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-10-30 16:10:42 -06:00
1 parent c5dedbef33
commit f2b79d995c
2 files changed
+10 -10

No files matched your search

+10 -10
View File
@@ -2179,17 +2179,17 @@ struct ClipList
hb_map_t new_gid_offset_map;
hb_set_t new_gids;
for (const ClipRecord& record : clips.iter ())
/* Look up each retained glyph the same way get_extents () does, instead of
* walking every gid of every record: records are not required to be
* disjoint, so that can cost num_records * num_glyphs. */
for (hb_codepoint_t gid : glyphset)
{
unsigned start_gid = record.startGlyphID;
unsigned end_gid = record.endGlyphID;
for (unsigned gid = start_gid; gid <= end_gid; gid++)
{
if (!glyphset.has (gid) || !glyph_map.has (gid)) continue;
unsigned new_gid = glyph_map.get (gid);
new_gid_offset_map.set (new_gid, record.clipBox);
new_gids.add (new_gid);
}
const hb_codepoint_t *new_gid;
if (!glyph_map.has (gid, &new_gid)) continue;
const ClipRecord *record = clips.as_array ().bsearch (gid);
if (!record) continue;
new_gid_offset_map.set (*new_gid, record->clipBox);
new_gids.add (*new_gid);
}
unsigned count = serialize_clip_records (c, instancer, new_gids, new_gid_offset_map);