From f2b79d995cb415dcc30db2dda5da083b620ad77c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:10:42 -0600 Subject: [PATCH] [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 --- src/OT/Color/COLR/COLR.hh | 20 +++++++++--------- .../colrv1-cliplist-overlapping-records.ttf | Bin 0 -> 35134 bytes 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 test/fuzzing/fonts/colrv1-cliplist-overlapping-records.ttf diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 246d76752..c0533edf7 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -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); diff --git a/test/fuzzing/fonts/colrv1-cliplist-overlapping-records.ttf b/test/fuzzing/fonts/colrv1-cliplist-overlapping-records.ttf new file mode 100644 index 0000000000000000000000000000000000000000..a26230f5b6c96f0c7a0ce9dcc25d80a33fd7b9b3 GIT binary patch literal 35134 zcmeIzF-n6$5C+gMf5gHzqz_gWHVSD{g#RBXA2m1b>0=kO(xSAq{CrLmJYMhBTxh4QWV28q$!4G^8O7X-GpF z(vXHUq#+GyNJARZkcKp*Aq{CrLmJYMhBTxh4QWV28q$!4G^8O7X-GpF(vXHUq#+Gy zNJARZkcKp*Aq{CrLmJYMhBTxh4QWV28q$!4G^8O7X-GpF(vXHUq#+GyNJARZkcKp* zAq{CrLmJYMhBTxh4QWV28q$!4G^8O7X-GpF(vXHUq#+GyNJARZkcR(2!$E2ckHcey SO