From f840e4d018dae3256bb94b3cdbdd0d1a47e41b6a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:00:28 -0600 Subject: [PATCH 1/8] [subset] Clamp PaintColrLayers layer range to LayerList in COLRv1 closure PaintColrLayers::closurev1() recorded firstLayerIndex .. firstLayerIndex + numLayers - 1 as retained layers without checking the range against LayerList.len (PaintColrLayers::sanitize is a plain check_struct), and _colr_closure() then creates one colrv1_layers map entry per retained index. A crafted font whose leaf paints point past the end of the LayerList could therefore add up to 255 entries each for layers that do not exist, driving subset-plan memory to roughly 1200x the font size (585 MiB for a 512 KB font). Clamp the range to the LayerList at the one place the retained-layer set is built, before add_layer_indices() and for the dispatch loop alike; this also removes the firstLayerIndex + numLayers wrap-around in the loop bound. Readers (paint_glyph, dependv1) never record indices and already tolerate out-of-range ones via LayerList::get_paint() returning Null, so they are left alone. A PaintColrLayers whose range lies past the source LayerList now has no mapping and subset writes 0xFFFFFFFF; both that and a renumbered index past the new LayerList are out of range and draw nothing. Adds a regression test that builds such a font inline and pins the output; it fails without the fix. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ROQFK0RA. Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/colrv1-closure.hh | 8 +++- test/api/test-subset-colr.c | 61 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/OT/Color/COLR/colrv1-closure.hh b/src/OT/Color/COLR/colrv1-closure.hh index f94ce070c..24ef3bda5 100644 --- a/src/OT/Color/COLR/colrv1-closure.hh +++ b/src/OT/Color/COLR/colrv1-closure.hh @@ -38,9 +38,13 @@ namespace OT { HB_INTERNAL void PaintColrLayers::closurev1 (hb_colrv1_closure_context_t* c) const { - c->add_layer_indices (firstLayerIndex, numLayers); const LayerList &paint_offset_lists = c->get_colr_table ()->get_layerList (); - for (unsigned i = firstLayerIndex; i < firstLayerIndex + numLayers; i++) + /* Only retain layers that exist in the LayerList; firstLayerIndex is not + * checked against it at sanitize time. */ + unsigned first = hb_min ((unsigned) firstLayerIndex, (unsigned) paint_offset_lists.len); + unsigned count = hb_min ((unsigned) numLayers, paint_offset_lists.len - first); + c->add_layer_indices (first, count); + for (unsigned i = first; i < first + count; i++) { const Paint &paint = paint_offset_lists.get_paint (i); paint.dispatch (c); diff --git a/test/api/test-subset-colr.c b/test/api/test-subset-colr.c index feb9767f2..b22c28ccb 100644 --- a/test/api/test-subset-colr.c +++ b/test/api/test-subset-colr.c @@ -587,6 +587,66 @@ test_subset_colr_var_overflow (void) hb_face_destroy (face); } +/* A PaintColrLayers whose layer range lies past the end of the LayerList + * must not make the subsetter retain (and allocate mappings for) layers + * that do not exist. */ +static void +test_subset_colr_layers_out_of_range (void) +{ + const uint8_t maxp_data[6] = { + 0x00, 0x00, 0x50, 0x00, + 0x00, 0x02 /* numGlyphs */ + }; + const uint8_t colr_data[73] = { + /* header: version 1, no COLRv0 records, BaseGlyphList @34, LayerList @50 */ + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x32, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + /* BaseGlyphList: glyph 1 -> PaintColrLayers (numLayers 2, firstLayerIndex 0) */ + 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0A, + 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, + /* LayerList: PaintSolid, then PaintColrLayers (numLayers 255, firstLayerIndex 0xFFFFFF00) */ + 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x11, + 0x02, 0x00, 0x00, 0x40, 0x00, + 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + }; + + hb_blob_t *colr = hb_blob_create ((const char *) colr_data, sizeof (colr_data), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_blob_t *maxp = hb_blob_create ((const char *) maxp_data, sizeof (maxp_data), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_t *builder = hb_face_builder_create (); + hb_face_builder_add_table (builder, HB_TAG ('C','O','L','R'), colr); + hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), maxp); + hb_blob_t *face_blob = hb_face_reference_blob (builder); + hb_face_t *face = hb_face_create (face_blob, 0); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_glyph_set (input), 1); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + + hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('C','O','L','R')); + unsigned length; + const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length); + g_assert_cmpuint (length, >=, 34); + unsigned list = read_uint_be (data + 18, 4); + g_assert_cmpuint (list + 12, <=, length); + /* Only the two layers that exist are kept. */ + g_assert_cmpuint (read_uint_be (data + list, 4), ==, 2); + unsigned paint = list + read_uint_be (data + list + 8, 4); + g_assert_cmpuint (paint + 6, <=, length); + g_assert_cmpuint (data[paint], ==, 1); + g_assert_cmpuint (data[paint + 1], ==, 255); + /* The out-of-range range was not retained, so it has no mapping. */ + g_assert_cmpuint (read_uint_be (data + paint + 2, 4), ==, 0xFFFFFFFFu); + hb_blob_destroy (blob); + + hb_face_destroy (subset); + hb_face_destroy (face); + hb_blob_destroy (face_blob); + hb_face_destroy (builder); + hb_blob_destroy (maxp); + hb_blob_destroy (colr); +} + int main (int argc, char **argv) { @@ -603,6 +663,7 @@ main (int argc, char **argv) hb_test_add (test_subset_colr_affine_values); hb_test_add (test_subset_colr_sentinel_map_without_variations); hb_test_add (test_subset_colr_var_overflow); + hb_test_add (test_subset_colr_layers_out_of_range); return hb_test_run(); } From c5dedbef338fd9300a38603b24a6137c397c70ec Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:04:35 -0600 Subject: [PATCH 2/8] [subset] Bound shared COLRv0 layer expansion and make V0 closures linear COLR::subset() gives every retained COLRv0 base glyph a private copy of its layer range, so N base glyphs sharing L layers cost N x L layer copies with two hash lookups each, and nothing checked the total against the 16-bit numLayers header field. serialize_V0() walks the lazy iterator twice and _hb_subset_table_try() retries while doubling the buffer, so a ~200 KB font with all records naming the same range kept hb-subset busy for about two minutes before failing. Cap the summed layer count with a cheap walk over base_it before layer_it is built, and drop COLR (return_trace (false)) when the total exceeds 0xFFFF, the same way this function already handles V0 records it cannot subset; this also removes the 16-bit wrap in serialize_V0(). Any total above 65535 could only have produced a failed subset or a wrapped, invalid numLayers, so no valid output is lost. The two V0 closure helpers had the same N x L shape during planning; collect the layer indices of all retained base glyphs into one set first and visit each layer once. closure_glyphs() now takes the glyph set in one call (the accelerator wrapper changes signature with it; no public API change), and _colr_closure() passes the whole set instead of looping. Adds test_subset_colr_shared_layers (256 base glyphs all claiming the same 256 layers): retaining 255 gives a 65280-layer table, retaining all 256 gives an empty COLR. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-4T7NHCTK. Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/COLR.hh | 56 +++++++++++++----- src/hb-subset-plan.cc | 3 +- test/api/fonts/colr-shared-layers.ttf | Bin 0 -> 10460 bytes .../fonts/generate-colr-shared-layers-font.py | 34 +++++++++++ test/api/test-subset-colr.c | 45 ++++++++++++++ 5 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 test/api/fonts/colr-shared-layers.ttf create mode 100644 test/api/fonts/generate-colr-shared-layers-font.py diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 106143d38..246d76752 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -2591,9 +2591,9 @@ struct COLR colr->depend (depend_data); } - void closure_glyphs (hb_codepoint_t glyph, + void closure_glyphs (const hb_set_t *glyphs, hb_set_t *related_ids /* OUT */) const - { colr->closure_glyphs (glyph, related_ids); } + { colr->closure_glyphs (glyphs, related_ids); } void closure_V0palette_indices (const hb_set_t *glyphs, hb_set_t *palettes /* OUT */) const @@ -2681,16 +2681,33 @@ struct COLR } } - void closure_glyphs (hb_codepoint_t glyph, + /* Base glyphs may share layers. The V0 closures collect the layer + * indices of all base glyphs first, so each layer is visited once. */ + void collect_V0layer_indices (const BaseGlyphRecord &record, + hb_set_t *layer_indices /* OUT */) const + { + unsigned count = (this+layersZ).as_array (numLayers).sub_array (record.firstLayerIdx, + record.numLayers).length; + if (!count) return; + layer_indices->add_range (record.firstLayerIdx, record.firstLayerIdx + count - 1); + } + + void closure_glyphs (const hb_set_t *glyphs, hb_set_t *related_ids /* OUT */) const { - const BaseGlyphRecord *record = get_base_glyph_record (glyph); - if (!record) return; + if (!numBaseGlyphs || !numLayers) return; + hb_array_t all_layers = (this+layersZ).as_array (numLayers); - auto glyph_layers = (this+layersZ).as_array (numLayers).sub_array (record->firstLayerIdx, - record->numLayers); - if (!glyph_layers.length) return; - related_ids->add_array (&glyph_layers[0].glyphId, glyph_layers.length, LayerRecord::min_size); + hb_set_t layer_indices; + for (hb_codepoint_t glyph : *glyphs) + { + const BaseGlyphRecord *record = get_base_glyph_record (glyph); + if (!record) continue; + collect_V0layer_indices (*record, &layer_indices); + } + + for (unsigned idx : layer_indices) + related_ids->add (all_layers[idx].glyphId); } void closure_V0palette_indices (const hb_set_t *glyphs, @@ -2700,14 +2717,15 @@ struct COLR hb_array_t baseGlyphs = (this+baseGlyphsZ).as_array (numBaseGlyphs); hb_array_t all_layers = (this+layersZ).as_array (numLayers); - for (const BaseGlyphRecord record : baseGlyphs) + hb_set_t layer_indices; + for (const BaseGlyphRecord &record : baseGlyphs) { if (!glyphs->has (record.glyphId)) continue; - hb_array_t glyph_layers = all_layers.sub_array (record.firstLayerIdx, - record.numLayers); - for (const LayerRecord layer : glyph_layers) - palettes->add (layer.colorIdx); + collect_V0layer_indices (record, &layer_indices); } + + for (unsigned idx : layer_indices) + palettes->add (all_layers[idx].colorIdx); } void closure_forV1 (hb_set_t *glyphset, @@ -2980,6 +2998,16 @@ struct COLR | hb_map_retains_sorting (hb_second) ; + /* Base glyphs sharing layers each get their own copy in the output. + * Drop the table instead of expanding more layers than numLayers holds. */ + unsigned num_layers = 0; + for (const BaseGlyphRecord record : + base_it) + { + num_layers += record.numLayers; + if (unlikely (num_layers > 0xFFFFu)) + return_trace (false); + } + auto layer_it = + hb_range (c->plan->num_output_glyphs ()) | hb_map (reverse_glyph_map) diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 9d62f4a2c..1f664d181 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -118,8 +118,7 @@ static void _colr_closure (hb_subset_plan_t* plan, hb_set_t palette_indices, layer_indices; // Collect all glyphs referenced by COLRv0 hb_set_t glyphset_colrv0; - for (hb_codepoint_t gid : *glyphs_colred) - colr.closure_glyphs (gid, &glyphset_colrv0); + colr.closure_glyphs (glyphs_colred, &glyphset_colrv0); glyphs_colred->union_ (glyphset_colrv0); diff --git a/test/api/fonts/colr-shared-layers.ttf b/test/api/fonts/colr-shared-layers.ttf new file mode 100644 index 0000000000000000000000000000000000000000..484f8a0758143c8fd9a93884e20d04582b260cbd GIT binary patch literal 10460 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 literal 0 HcmV?d00001 diff --git a/test/api/fonts/generate-colr-shared-layers-font.py b/test/api/fonts/generate-colr-shared-layers-font.py new file mode 100644 index 000000000..3fa303129 --- /dev/null +++ b/test/api/fonts/generate-colr-shared-layers-font.py @@ -0,0 +1,34 @@ +"""Generate a COLRv0 font whose base glyphs all share the same layer range.""" + +from pathlib import Path +from fontTools.fontBuilder import FontBuilder +from fontTools.pens.ttGlyphPen import TTGlyphPen +from fontTools.ttLib.tables.DefaultTable import DefaultTable +import struct + +# Every base glyph claims all the layers, so retaining 256 of them expands +# to 65536 layer records, one more than the COLR header can count. +count = 256 +bases = [f"base{i}" for i in range(count)] +layers = [f"layer{i}" for i in range(count)] +names = [".notdef"] + bases + layers +fb = FontBuilder(1000, isTTF=True) +fb.setupGlyphOrder(names) +fb.setupCharacterMap({0xE000 + i: name for i, name in enumerate(bases)}) +fb.setupGlyf({name: TTGlyphPen(None).glyph() for name in names}) +fb.setupHorizontalMetrics({name: (1000, 0) for name in names}) +fb.setupHorizontalHeader(ascent=800, descent=-200) +fb.setupNameTable({"familyName": "COLR Shared Layers", "styleName": "Regular"}) +fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200) +fb.setupPost() +fb.setupCPAL([[(0, 0, 0, 1), (1, 1, 1, 1)]]) + +# fontTools writes each base glyph its own layers; build the table by hand. +header = struct.pack(">HHIIH", 0, count, 14, 14 + count * 6, count) +base_records = b"".join(struct.pack(">HHH", 1 + i, 0, count) for i in range(count)) +layer_records = b"".join(struct.pack(">HH", 1 + count + i, i % 2) for i in range(count)) +colr = DefaultTable("COLR") +colr.data = header + base_records + layer_records +fb.font["COLR"] = colr + +fb.save(Path(__file__).with_name("colr-shared-layers.ttf")) diff --git a/test/api/test-subset-colr.c b/test/api/test-subset-colr.c index b22c28ccb..5c8f291db 100644 --- a/test/api/test-subset-colr.c +++ b/test/api/test-subset-colr.c @@ -647,6 +647,50 @@ test_subset_colr_layers_out_of_range (void) hb_blob_destroy (colr); } +static void +test_subset_colr_shared_layers (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/colr-shared-layers.ttf"); + /* Every base glyph claims all 256 layers. Retaining 255 of them expands + * to 65280 layer records; one more does not fit numLayers. */ + for (unsigned count = 255; count <= 256; count++) + { + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_unicode_set (input), 0xE000, 0xE000 + count - 1); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + + hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('C','O','L','R')); + unsigned length; + const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length); + if (count == 256) + { + g_assert_cmpuint (length, ==, 0); + hb_blob_destroy (blob); + hb_face_destroy (subset); + continue; + } + g_assert_cmpuint (length, ==, 14 + count * 6 + count * 256 * 4); + g_assert_cmpuint (read_uint_be (data + 2, 2), ==, count); + g_assert_cmpuint (read_uint_be (data + 12, 2), ==, count * 256); + unsigned bases = read_uint_be (data + 4, 4), layers = read_uint_be (data + 8, 4); + for (unsigned i = 0; i < count; i++) + { + g_assert_cmpuint (read_uint_be (data + bases + i * 6, 2), ==, i + 1); + g_assert_cmpuint (read_uint_be (data + bases + i * 6 + 2, 2), ==, i * 256); + g_assert_cmpuint (read_uint_be (data + bases + i * 6 + 4, 2), ==, 256); + for (unsigned j = 0; j < 256; j++) + { + unsigned layer = layers + (i * 256 + j) * 4; + g_assert_cmpuint (read_uint_be (data + layer, 2), ==, count + 1 + j); + g_assert_cmpuint (read_uint_be (data + layer + 2, 2), ==, j % 2); + } + } + hb_blob_destroy (blob); + hb_face_destroy (subset); + } + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -664,6 +708,7 @@ main (int argc, char **argv) hb_test_add (test_subset_colr_sentinel_map_without_variations); hb_test_add (test_subset_colr_var_overflow); hb_test_add (test_subset_colr_layers_out_of_range); + hb_test_add (test_subset_colr_shared_layers); return hb_test_run(); } From f2b79d995cb415dcc30db2dda5da083b620ad77c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:10:42 -0600 Subject: [PATCH 3/8] [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 Date: Fri, 30 Oct 2026 16:10:55 -0600 Subject: [PATCH 4/8] [subset] Compute COLR constant_after_pinning once per plan PaintRadialGradient::constant_after_pinning() answers a question about the source ItemVariationStore and the subset plan only, yet it rescanned every variation region (building a hashmap per region, up to 32767) on every paint visit. Shared paints are visited once per reference by OffsetTo::serialize_subset(), so during partial instancing the cost grew as visits x regionCount: 40 s to 6 minutes for a 192 KB font with a PaintComposite chain and many regions. Cache the answer in a tri-state plan field (-1 until computed, like the lazily-set has_gdef_varstore flag) so the scan runs at most once per plan; the plan is valid for one source face and one set of axis locations, so this is sound. Move the cheap varIdxBase != NO_VARIATION test ahead of the scan so non-variable format 6 gradients never trigger it. The allocation-failure path of get_var_region() still sets the serializer error and returns uncached, as before. Output is byte-identical; the attack font drops from ~41 s to ~0.03 s. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-910I4C0Y. Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/COLR.hh | 11 +++++++---- src/hb-subset-plan.cc | 1 + src/hb-subset-plan.hh | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index c0533edf7..a2b630840 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -890,6 +890,10 @@ struct PaintRadialGradient const ItemVarStoreInstancer &instancer) const { if (c->plan->has_avar2) return false; + /* The answer depends on the table and the plan only, but shared paints + * are visited once per reference, so compute it once per plan. */ + int &constant = c->plan->colrv1_constant_after_pinning; + if (constant >= 0) return constant; const auto ®ions = instancer.varStore->get_region_list (); for (unsigned i = 0; i < regions.regionCount; i++) { @@ -904,10 +908,10 @@ struct PaintRadialGradient Triple *limit; if (!c->plan->user_axes_location.has (axis.first, &limit) || limit->minimum != limit->maximum) - return false; + return (constant = false); } } - return true; + return (constant = true); } void dependv1 (hb_colrv1_depend_context_t* c) const {} @@ -1052,9 +1056,8 @@ struct PaintRadialGradient uint32_t varIdxBase) const { TRACE_SUBSET (this); - if (instancer && !c->plan->pinned_at_default && + if (instancer && !c->plan->pinned_at_default && varIdxBase != VarIdx::NO_VARIATION && (c->plan->all_axes_pinned || constant_after_pinning (c, instancer)) && - varIdxBase != VarIdx::NO_VARIATION && (radius0 + (double) roundf (instancer (varIdxBase, 2)) < 0 || radius1 + (double) roundf (instancer (varIdxBase, 5)) < 0)) return_trace (subset_nonnegative (c, instancer, varIdxBase)); diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 1f664d181..7dd6908ca 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -695,6 +695,7 @@ hb_subset_plan_t::hb_subset_plan_t (hb_face_t *face, all_axes_pinned = false; pinned_at_default = true; has_gdef_varstore = false; + colrv1_constant_after_pinning = -1; has_avar2 = false; #ifdef HB_EXPERIMENTAL_API diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index 9b20b3385..3d122731a 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -169,6 +169,9 @@ struct hb_subset_plan_t // whether GDEF ItemVariationStore is retained mutable bool has_gdef_varstore; + // whether every COLR variation region uses pinned axes only; -1 until computed + int colrv1_constant_after_pinning; + // whether we're doing avar2 partial instancing bool has_avar2; From 292942ceca63c18c52cbf567cc0710f50678a49d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:14:51 -0600 Subject: [PATCH 5/8] [COLR] Charge gradient color stops to the paint session work budget The COLRv1 paint walk bounds paint-table visits (HB_MAX_NESTING_LEVEL, HB_MAX_GRAPH_EDGE_COUNT) but handed out ColorLine stops uncharged, so a single 65535-stop ColorLine shared across up to 16384 gradient edges could deliver ~10^9 stops from one hb_font_paint_glyph() call, keeping the rendering thread busy for minutes per glyph in every backend. Give hb_paint_context_t the session's live budget via funcs->get_budget_remaining_ptr(), with the same local HB_BUDGET_GLYPH fallback hb_draw_session_t uses, charge HB_BUDGET_16 per stop delivered in ColorLine::get_color_stops() (the one place every consumer fetches stops, so vector, raster, cairo and client paint functions are all covered), and make recurse() skip further paints once the budget is negative, as it already does for an oversized graph. The backend's one budget is only spent, never re-armed, so this does not reintroduce the cross-subsystem budget multiplication removed in #6147. hb-ft-colr.hh has the same shape and gets the identical change. With the default 2^24 budget about a million stops per session are delivered before the rest of the walk is skipped best-effort. A separate quadratic cost inside cairo's own gradient pattern building is not addressed here. Adds a regression test that fails without the fix. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-F2NWHRVP. Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/COLR.hh | 14 ++++++-- src/hb-ft-colr.hh | 13 +++++-- test/api/test-paint.c | 73 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index a2b630840..55c45a5d3 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -85,6 +85,8 @@ public: hb_decycler_t layers_decycler; int depth_left = HB_MAX_NESTING_LEVEL; int edge_count = HB_MAX_GRAPH_EDGE_COUNT; + int64_t local_budget = HB_BUDGET_GLYPH; + int64_t *budget; hb_paint_context_t (const void *base_, hb_paint_funcs_t *funcs_, @@ -104,8 +106,12 @@ public: #endif ), foreground (foreground_), - instancer (instancer_) + instancer (instancer_), + budget (funcs->get_budget_remaining_ptr (data)) { + if (!budget) + budget = &local_budget; + if (font->is_synthetic) { font = hb_font_create_sub_font (font); @@ -606,6 +612,10 @@ struct ColorLine for (i = 0; i < *count && start + i < len; i++) stops[start + i].get_color_stop (c, &color_stops[i], instancer); *count = i; + + /* A gradient is one graph edge however long its color line is, and + * color lines can be shared. recurse() stops once this runs out. */ + hb_budget_spend (*c->budget, HB_BUDGET_16, i); } return len; @@ -3266,7 +3276,7 @@ struct COLR_accelerator_t : COLR::accelerator_t { void hb_paint_context_t::recurse (const Paint &paint) { - if (unlikely (depth_left <= 0 || edge_count <= 0)) return; + if (unlikely (depth_left <= 0 || edge_count <= 0 || *budget < 0)) return; depth_left--; edge_count--; paint.dispatch (this); diff --git a/src/hb-ft-colr.hh b/src/hb-ft-colr.hh index ef7862a2b..0cf43a960 100644 --- a/src/hb-ft-colr.hh +++ b/src/hb-ft-colr.hh @@ -113,8 +113,12 @@ struct hb_ft_paint_context_t hb_color_t foreground) : ft_font (ft_font_), font (font_), funcs (paint_funcs), data (paint_data), - palette (palette), palette_index (palette_index), foreground (foreground) + palette (palette), palette_index (palette_index), foreground (foreground), + budget (funcs->get_budget_remaining_ptr (data)) { + if (!budget) + budget = &local_budget; + if (font->is_synthetic) { font = hb_font_create_sub_font (font); @@ -132,7 +136,7 @@ struct hb_ft_paint_context_t void recurse (FT_OpaquePaint paint) { - if (unlikely (depth_left <= 0 || edge_count <= 0)) return; + if (unlikely (depth_left <= 0 || edge_count <= 0 || *budget < 0)) return; depth_left--; edge_count--; _hb_ft_paint (this, paint); @@ -150,6 +154,8 @@ struct hb_ft_paint_context_t hb_decycler_t layers_decycler; int depth_left = HB_MAX_NESTING_LEVEL; int edge_count = HB_MAX_GRAPH_EDGE_COUNT; + int64_t local_budget = HB_BUDGET_GLYPH; + int64_t *budget; }; static void @@ -255,6 +261,9 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line, *count = wrote; // reset the iterator for next time cl->color_stop_iterator = iter; + + // recurse() stops once this runs out + hb_budget_spend (*c->budget, HB_BUDGET_16, wrote); } return cl->color_stop_iterator.num_color_stops; diff --git a/test/api/test-paint.c b/test/api/test-paint.c index d7985959d..ee4891bfe 100644 --- a/test/api/test-paint.c +++ b/test/api/test-paint.c @@ -1149,6 +1149,78 @@ test_hb_paint_budget (void) hb_paint_funcs_destroy (funcs); } +static unsigned color_stops_fetched; + +static void +fetch_color_stops (hb_paint_funcs_t *funcs HB_UNUSED, + void *paint_data HB_UNUSED, + hb_color_line_t *color_line, + float x0 HB_UNUSED, float y0 HB_UNUSED, + float x1 HB_UNUSED, float y1 HB_UNUSED, + float x2 HB_UNUSED, float y2 HB_UNUSED, + void *user_data HB_UNUSED) +{ + hb_color_stop_t stops[100]; + unsigned count = G_N_ELEMENTS (stops); + g_assert_cmpuint (hb_color_line_get_color_stops (color_line, 0, &count, stops), ==, 100); + g_assert_cmpuint (count, ==, 100); + color_stops_fetched += count; +} + +static void +test_hb_paint_color_stops_budget (void) +{ + /* A COLRv1 glyph whose four layers all are the same linear gradient + * with 100 color stops. Fetching the stops must consume budget. */ + char colr_data[34 + 16 + 20 + 16 + 3 + 100 * 6 + 21] = {0}; + colr_data[1] = 1; /* version */ + colr_data[17] = 34; /* baseGlyphList */ + colr_data[21] = 50; /* layerList */ + colr_data[24] = 0x02; colr_data[25] = (char) 0xB1; /* clipList */ + colr_data[34 + 3] = 1; /* BaseGlyphList: glyph 0 */ + colr_data[34 + 9] = 10; + colr_data[34 + 10] = 1; /* PaintColrLayers */ + colr_data[34 + 11] = 4; + colr_data[50 + 3] = 4; /* LayerList */ + for (unsigned i = 0; i < 4; i++) + colr_data[50 + 4 + i * 4 + 3] = 20; + colr_data[50 + 20] = 4; /* PaintLinearGradient */ + colr_data[50 + 23] = 16; + colr_data[50 + 36 + 2] = 100; /* ColorLine */ + colr_data[689] = 1; /* ClipList: glyph 0 */ + colr_data[689 + 4] = 1; + colr_data[689 + 11] = 12; + colr_data[689 + 12] = 1; /* ClipBox */ + + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "COLR", colr_data); + hb_face_set_glyph_count (face, 1); + hb_font_t *font = hb_font_create (face); + + paint_budget_data_t data = {HB_BUDGET_DEFAULT, 0}; + hb_paint_funcs_t *funcs = hb_paint_funcs_create (); + hb_paint_funcs_set_linear_gradient_func (funcs, fetch_color_stops, NULL, NULL); + hb_paint_funcs_set_set_budget_func (funcs, paint_set_budget, NULL, NULL); + hb_paint_funcs_set_get_budget_remaining_func (funcs, paint_get_budget_remaining, NULL, NULL); + + color_stops_fetched = 0; + g_assert_true (hb_paint_set_budget (funcs, &data, 100000)); + hb_font_paint_glyph (font, 0, funcs, &data, 0, HB_COLOR (0, 0, 0, 255)); + g_assert_cmpuint (color_stops_fetched, ==, 400); + g_assert_cmpint (data.live, <=, 100000 - 400); + g_assert_cmpint (data.live, >=, 0); + + color_stops_fetched = 0; + g_assert_true (hb_paint_set_budget (funcs, &data, 100)); + hb_font_paint_glyph (font, 0, funcs, &data, 0, HB_COLOR (0, 0, 0, 255)); + g_assert_cmpuint (color_stops_fetched, ==, 100); + g_assert_cmpint (data.live, <, 0); + + hb_paint_funcs_destroy (funcs); + hb_font_destroy (font); + hb_face_destroy (face); +} + int main (int argc, char **argv) @@ -1164,6 +1236,7 @@ main (int argc, char **argv) hb_test_add (test_fill_glyph_nil_decomposes); hb_test_add (test_color_glyph_user_data); hb_test_add (test_hb_paint_budget); + hb_test_add (test_hb_paint_color_stops_budget); for (unsigned int i = 0; i < G_N_ELEMENTS (paint_tests); i++) { From b732218126f693f28a4adf4782c2e109753d58f7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:15:07 -0600 Subject: [PATCH 6/8] =?UTF-8?q?[paint]=20Clip=20sweep-gradient=20tiles=20t?= =?UTF-8?q?o=200..2=CF=80=20in=20hb=5Fpaint=5Fsweep=5Fgradient=5Ftiles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hb_paint_sweep_gradient_tiles() documents that it iterates the 0..2π sweep, and all three consumers (hb-cairo mesh patches, the SVG and PDF writers) size their per-tile loops by tile width on that assumption. Several emit sites (the start==end PAD case, the first PAD stop past 0, and repeat/reflect segments straddling 0 or 2π) passed the angles straight through, and COLRv1 variation deltas can place them hundreds of thousands of radians outside the circle, so one tile could expand to millions of patches: a 504-byte variable font drawn off its default allocated ~1.6 GiB through hb-cairo. #6157 bounded the number of sweep tiles; this bounds the width of each one. Route every emit through a small clipping lambda inside the tiler that clamps the tile to [0, 2π], lerps the end colours at the clip points, and drops tiles entirely outside, so the contract holds at the single place it is established rather than in each backend. HB_PAINT_MAX_SWEEP_TILES and the repeat-loop bounds are untouched; external callers need no change. Adds a regression test calling the public function with far-out angles for PAD/REPEAT/REFLECT; it fails without the fix. Follow-up to #6157. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GCNW3SND. Co-Authored-By: Claude Opus 4.8 --- src/hb-paint.cc | 40 +++++++++++++++++------- test/api/test-paint.c | 72 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/hb-paint.cc b/src/hb-paint.cc index 0224a1c0f..3899274ca 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -1080,6 +1080,7 @@ hb_paint_normalize_color_line (hb_color_stop_t *stops, * * Iterates the full 0..2π sweep produced by a color-stop list, * invoking @emit_patch once per (start, end) angular segment. + * Segments are clipped to 0..2π, wherever the angles lie. * Handles #HB_PAINT_EXTEND_PAD, #HB_PAINT_EXTEND_REPEAT, and * #HB_PAINT_EXTEND_REFLECT. Stops must be pre-sorted by * offset; use hb_paint_normalize_color_line() first if they @@ -1098,14 +1099,31 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops, { if (!n_stops) return; + /* Variation deltas can put the angles any distance outside the + * circle. Emit only the part of each tile that is within 0..2π; + * callers do work proportional to the width of the tile. */ + auto emit = [&] (float a0, hb_color_t c0, float a1, hb_color_t c1) + { + float t0 = hb_max (a0, 0.f); + float t1 = hb_min (a1, HB_2_PI); + if (t0 > t1) return; + hb_color_t d0 = c0, d1 = c1; + if (c0 != c1) + { + if (t0 != a0) d0 = hb_color_lerp (c0, c1, (t0 - a0) / (a1 - a0)); + if (t1 != a1) d1 = hb_color_lerp (c0, c1, (t1 - a0) / (a1 - a0)); + } + emit_patch (t0, d0, t1, d1, user_data); + }; + if (start_angle == end_angle) { if (extend == HB_PAINT_EXTEND_PAD) { if (start_angle > 0.f) - emit_patch (0.f, stops[0].color, start_angle, stops[0].color, user_data); + emit (0.f, stops[0].color, start_angle, stops[0].color); if (end_angle < HB_2_PI) - emit_patch (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color, user_data); + emit (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color); } return; } @@ -1168,26 +1186,26 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops, if (pos == n_stops) { color0 = colors[n_stops - 1]; - emit_patch (0.f, color0, HB_2_PI, color0, user_data); + emit (0.f, color0, HB_2_PI, color0); goto done; } - emit_patch (0.f, color0, angles[pos], colors[pos], user_data); + emit (0.f, color0, angles[pos], colors[pos]); for (pos++; pos < n_stops; pos++) { if (angles[pos] <= HB_2_PI) - emit_patch (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos], user_data); + emit (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); else { float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); hb_color_t color1 = hb_color_lerp (colors[pos - 1], colors[pos], f); - emit_patch (angles[pos - 1], colors[pos - 1], HB_2_PI, color1, user_data); + emit (angles[pos - 1], colors[pos - 1], HB_2_PI, color1); break; } } if (pos == n_stops) { color0 = colors[n_stops - 1]; - emit_patch (angles[n_stops - 1], color0, HB_2_PI, color0, user_data); + emit (angles[n_stops - 1], color0, HB_2_PI, color0); goto done; } } @@ -1217,7 +1235,7 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops, hb_clamp_to ((double) g + 0.5), hb_clamp_to ((double) r + 0.5), hb_clamp_to ((double) a + 0.5)); - emit_patch (0.f, avg, HB_2_PI, avg, user_data); + emit (0.f, avg, HB_2_PI, avg); goto done; } @@ -1265,17 +1283,17 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops, { float f = (0.f - a0_l) / (a1_l - a0_l); hb_color_t c = hb_color_lerp (col0, col1, f); - emit_patch (0.f, c, a1_l, col1, user_data); + emit (0.f, c, a1_l, col1); } else if (a1_l >= HB_2_PI) { float f = (HB_2_PI - a0_l) / (a1_l - a0_l); hb_color_t c = hb_color_lerp (col0, col1, f); - emit_patch (a0_l, col0, HB_2_PI, c, user_data); + emit (a0_l, col0, HB_2_PI, c); goto done; } else - emit_patch (a0_l, col0, a1_l, col1, user_data); + emit (a0_l, col0, a1_l, col1); } } } diff --git a/test/api/test-paint.c b/test/api/test-paint.c index ee4891bfe..04548faf8 100644 --- a/test/api/test-paint.c +++ b/test/api/test-paint.c @@ -1221,6 +1221,77 @@ test_hb_paint_color_stops_budget (void) hb_face_destroy (face); } +typedef struct { + unsigned tiles; + float covered; + hb_color_t c0, c1; +} sweep_tiles_data_t; + +static void +check_sweep_tile (float a0, hb_color_t c0, + float a1, hb_color_t c1, + void *user_data) +{ + sweep_tiles_data_t *data = (sweep_tiles_data_t *) user_data; + + g_assert_cmpfloat (a0, >=, 0.f); + g_assert_cmpfloat (a0, <=, a1); + g_assert_cmpfloat (a1, <=, (float) (2 * G_PI)); + data->tiles++; + data->covered += a1 - a0; + data->c0 = c0; + data->c1 = c1; +} + +static void +test_hb_paint_sweep_gradient_tiles_clipped (void) +{ + /* Variation deltas can put the angles far outside the circle. The + * tiles must still cover 0..2π, and nothing else. */ + static const struct { + hb_paint_extend_t extend; + unsigned n_stops; + float start_angle, end_angle; + } tests[] = { + { HB_PAINT_EXTEND_PAD, 1, 1e6f, 1e6f }, + { HB_PAINT_EXTEND_PAD, 1, -1e6f, -1e6f }, + { HB_PAINT_EXTEND_PAD, 1, 1e6f, 2e6f }, + { HB_PAINT_EXTEND_PAD, 2, 1e6f, 2e6f }, + { HB_PAINT_EXTEND_PAD, 2, -2e6f, -1e6f }, + { HB_PAINT_EXTEND_PAD, 2, -1e6f, 1e6f }, + { HB_PAINT_EXTEND_PAD, 2, 1e6f, -1e6f }, + { HB_PAINT_EXTEND_PAD, 2, 1.f, 1e6f }, + { HB_PAINT_EXTEND_REPEAT, 2, -1e6f, 1e6f }, + { HB_PAINT_EXTEND_REPEAT, 2, 1e6f, 3e6f }, + { HB_PAINT_EXTEND_REPEAT, 2, 1e20f, 3e20f }, + { HB_PAINT_EXTEND_REFLECT, 2, -1e6f, 1e6f }, + { HB_PAINT_EXTEND_REFLECT, 2, -3e6f, -1e6f }, + }; + + for (unsigned i = 0; i < G_N_ELEMENTS (tests); i++) + { + hb_color_stop_t stops[2] = { + { 0.f, FALSE, HB_COLOR (0, 0, 0, 255) }, + { 1.f, FALSE, HB_COLOR (255, 255, 255, 255) }, + }; + sweep_tiles_data_t data = {0, 0.f, 0, 0}; + + hb_paint_sweep_gradient_tiles (stops, tests[i].n_stops, tests[i].extend, + tests[i].start_angle, tests[i].end_angle, + check_sweep_tile, &data); + + g_assert_cmpuint (data.tiles, >=, 1); + g_assert_cmpuint (data.tiles, <=, 3); + g_assert_cmpfloat (data.covered, >, (float) (2 * G_PI) - 1e-3f); + g_assert_cmpfloat (data.covered, <, (float) (2 * G_PI) + 1e-3f); + if (tests[i].n_stops == 1) + { + g_assert_cmphex (data.c0, ==, stops[0].color); + g_assert_cmphex (data.c1, ==, stops[0].color); + } + } +} + int main (int argc, char **argv) @@ -1237,6 +1308,7 @@ main (int argc, char **argv) hb_test_add (test_color_glyph_user_data); hb_test_add (test_hb_paint_budget); hb_test_add (test_hb_paint_color_stops_budget); + hb_test_add (test_hb_paint_sweep_gradient_tiles_clipped); for (unsigned int i = 0; i < G_N_ELEMENTS (paint_tests); i++) { From b83dcdbcdeabf35f4688b7e1ecd5bfbdc117193b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:22:37 -0600 Subject: [PATCH 7/8] [depend] Bound COLR dependency extraction with a shared op budget COLR::depend() walked the COLRv1 paint graph from scratch for every BaseGlyphPaintRecord (clearing the visited set each time) with no total work limit, so N base glyphs sharing one paint graph cost N times the graph, quadratic in table size (204 s for a 200 KB font); the COLRv0 loop has the same base-glyphs x layers shape. The API is built only with -Dexperimental_api=true. hb_colrv1_depend_context_t, which already owns the per-walk depth and visited-set guards, now also carries max_ops sized like the sanitizer's (table length x HB_SANITIZE_MAX_OPS_FACTOR, clamped to the MIN/MAX), charged one unit per Paint::dispatch(), per COLRv0 layer record, and per PaintColrLayers layer run. Charging the layer run in dependv1() matters because a layer resolving to the null or an unknown-format paint is dropped by Paint::dispatch() before it reaches the context, so without it a PaintColrLayers could still spin its 255-iteration loop uncharged. Exhausting the budget calls depend_data->fail() (the existing error idiom) so hb_subset_depend_from_face_or_fail() returns NULL rather than a graph with silently missing edges, and the base-glyph loop stops once the builder has failed. The reproducer font is now rejected in a fraction of a second; the control still succeeds, and legitimate fonts keep 64 ops per table byte of headroom. Adds test_depend_colr_budget. Follow-up to #6113. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TO8ND880. Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/COLR.hh | 34 +++++++++++-- src/OT/Color/COLR/colrv1-depend.hh | 4 ++ test/api/test-ot-depend.c | 81 ++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 55c45a5d3..5c522f1f1 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -242,6 +242,9 @@ struct hb_colrv1_depend_context_t : template return_t dispatch (const T &obj) { + if (unlikely (!spend_ops ())) + return hb_empty_t (); + if (unlikely (nesting_level_left == 0)) return hb_empty_t (); @@ -267,6 +270,20 @@ struct hb_colrv1_depend_context_t : return false; } + /* Layers and paints shared between base glyphs are walked again for each + * of them, which sanitize does not charge for. Bound the total work and + * fail, rather than return a graph with edges missing. */ + bool spend_ops (unsigned count = 1) + { + max_ops -= (int) count; + if (unlikely (max_ops < 0)) + { + max_ops = -1; + return depend_data->fail (); + } + return true; + } + const COLR* get_colr_table () const { return colr; } @@ -274,14 +291,19 @@ struct hb_colrv1_depend_context_t : const COLR *colr; hb_depend_data_builder_t *depend_data; hb_set_t visited_paint; + int max_ops; unsigned nesting_level_left; hb_codepoint_t source_gid; hb_colrv1_depend_context_t (const COLR *colr_, + unsigned colr_length, hb_depend_data_builder_t *depend_data_, unsigned nesting_level_left_ = HB_MAX_NESTING_LEVEL) : colr (colr_), depend_data (depend_data_), + max_ops ((int) hb_clamp ((uint64_t) colr_length * HB_SANITIZE_MAX_OPS_FACTOR, + (uint64_t) HB_SANITIZE_MAX_OPS_MIN, + (uint64_t) HB_SANITIZE_MAX_OPS_MAX)), nesting_level_left (nesting_level_left_), source_gid (0) {} @@ -2601,7 +2623,7 @@ struct COLR void depend (hb_depend_data_builder_t *depend_data) const { if (!has_data ()) return; - colr->depend (depend_data); + colr->depend (depend_data, colr.get_length ()); } void closure_glyphs (const hb_set_t *glyphs, @@ -2665,14 +2687,18 @@ struct COLR mutable hb_atomic_t cached_scratch; }; - void depend (hb_depend_data_builder_t *depend_data) const + void depend (hb_depend_data_builder_t *depend_data, unsigned table_length) const { + hb_colrv1_depend_context_t c (this, table_length, depend_data); + // v0 hb_array_t baseGlyphs = (this+baseGlyphsZ).as_array (numBaseGlyphs); for (const BaseGlyphRecord &record : baseGlyphs) { auto glyph_layers = (this+layersZ).as_array (numLayers).sub_array (record.firstLayerIdx, record.numLayers); + if (unlikely (!c.spend_ops (glyph_layers.length))) + return; for (const LayerRecord layer : glyph_layers) depend_data->add_depend(record.glyphId, HB_OT_TAG_COLR, layer.glyphId); } @@ -2681,11 +2707,13 @@ struct COLR return; // v1 - hb_colrv1_depend_context_t c (this, depend_data); const BaseGlyphList &baseglyph_paintrecords = this+baseGlyphList; for (const BaseGlyphPaintRecord &baseglyph_paintrecord: baseglyph_paintrecords.iter ()) { + if (unlikely (!depend_data->successful)) + return; + c.source_gid = baseglyph_paintrecord.glyphId; c.visited_paint.clear (); diff --git a/src/OT/Color/COLR/colrv1-depend.hh b/src/OT/Color/COLR/colrv1-depend.hh index 22184c1b4..4193a3f49 100644 --- a/src/OT/Color/COLR/colrv1-depend.hh +++ b/src/OT/Color/COLR/colrv1-depend.hh @@ -39,6 +39,10 @@ namespace OT { HB_INTERNAL void PaintColrLayers::dependv1 (hb_colrv1_depend_context_t* c) const { const LayerList &paint_offset_lists = c->get_colr_table ()->get_layerList (); + /* Charge the whole layer run: a layer resolving to the null or an + * unknown-format paint is dropped by Paint::dispatch() before it reaches + * the context, so without this the loop can still spin uncharged. */ + if (unlikely (!c->spend_ops (numLayers))) return; for (unsigned i = firstLayerIndex; i < firstLayerIndex + numLayers; i++) { const Paint &paint = paint_offset_lists.get_paint (i); diff --git a/test/api/test-ot-depend.c b/test/api/test-ot-depend.c index 9340e0c0a..5ef9ae03d 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -353,6 +353,86 @@ test_depend_colr (void) hb_face_destroy (face); } +static hb_bool_t +colr_depend_succeeds (const unsigned char *colr, unsigned int length, unsigned int num_glyphs) +{ + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create_or_fail ((const char *) colr, length, HB_MEMORY_MODE_DUPLICATE, NULL, NULL); + hb_face_builder_add_table (face, HB_TAG ('C','O','L','R'), blob); + hb_blob_destroy (blob); + hb_face_set_glyph_count (face, num_glyphs); + + hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face); + hb_bool_t ret = depend != NULL; + + hb_subset_depend_destroy (depend); + hb_face_destroy (face); + return ret; +} + +/* Test that COLR work repeated for every base glyph is bounded */ +static void +test_depend_colr_budget (void) +{ + { + /* COLRv0: every base glyph uses all of the layers. */ + enum { NUM_GLYPHS = 2048, LAYERS = 14 + NUM_GLYPHS * 6 }; + static unsigned char colr[LAYERS + NUM_GLYPHS * 4]; + colr[7] = 14; + colr[10] = LAYERS >> 8; + colr[11] = LAYERS & 0xFF; + colr[12] = NUM_GLYPHS >> 8; + for (unsigned i = 0; i < NUM_GLYPHS; i++) + { + unsigned char *record = colr + 14 + i * 6; + record[0] = i >> 8; + record[1] = i & 0xFF; + record[4] = NUM_GLYPHS >> 8; + } + + colr[3] = 1; + g_assert_true (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS)); + colr[2] = NUM_GLYPHS >> 8; + colr[3] = 0; + g_assert_false (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS)); + } + + { + /* COLRv1: 255 PaintColrLayers that each list all 255 of them as layers, + * and every base glyph uses the first one. */ + enum { NUM_GLYPHS = 64, NUM_PAINTS = 255, + BASE_GLYPHS = 34, + LAYERS = BASE_GLYPHS + 4 + NUM_GLYPHS * 6, + PAINTS = LAYERS + 4 + NUM_PAINTS * 4 }; + static unsigned char colr[PAINTS + NUM_PAINTS * 6]; + colr[1] = 1; + colr[17] = BASE_GLYPHS; + colr[20] = LAYERS >> 8; + colr[21] = LAYERS & 0xFF; + for (unsigned i = 0; i < NUM_GLYPHS; i++) + { + unsigned char *record = colr + BASE_GLYPHS + 4 + i * 6; + record[1] = i; + record[4] = (PAINTS - BASE_GLYPHS) >> 8; + record[5] = (PAINTS - BASE_GLYPHS) & 0xFF; + } + colr[LAYERS + 3] = NUM_PAINTS; + for (unsigned i = 0; i < NUM_PAINTS; i++) + { + unsigned offset = PAINTS - LAYERS + i * 6; + colr[LAYERS + 4 + i * 4 + 2] = offset >> 8; + colr[LAYERS + 4 + i * 4 + 3] = offset & 0xFF; + colr[PAINTS + i * 6] = 1; + colr[PAINTS + i * 6 + 1] = NUM_PAINTS; + } + + colr[BASE_GLYPHS + 3] = 1; + g_assert_true (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS)); + colr[BASE_GLYPHS + 3] = NUM_GLYPHS; + g_assert_false (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS)); + } +} + /* Test MATH (mathematical variants) dependency extraction */ static void test_depend_math (void) @@ -631,6 +711,7 @@ main (int argc, char **argv) hb_test_add (test_depend_glyf); hb_test_add (test_depend_cff); hb_test_add (test_depend_colr); + hb_test_add (test_depend_colr_budget); hb_test_add (test_depend_math); hb_test_add (test_depend_varc); hb_test_add (test_depend_gsub_formats); From 4d2b4125b4d46a49335f143a7db0f9e741714910 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 31 Oct 2026 10:57:39 -0600 Subject: [PATCH 8/8] [ft] Drop the dead color-stop iterator resets Since the color line is copied locally ("Keep iteration local"), the iteration advances cl->color_stop_iterator on that local copy, which is discarded when the function returns. The two "reset the iterator for next time" writes (cl->color_stop_iterator = iter) therefore have no effect: there is no persistent iterator state to restore, and positioning across calls is driven entirely by the start argument. Remove both dead writes and the now-unused saved iterator. No behavior change; the return value (num_color_stops) is unaffected by iteration. Full test suite passes (277/277). Co-Authored-By: Claude Opus 4.8 --- src/hb-ft-colr.hh | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/hb-ft-colr.hh b/src/hb-ft-colr.hh index 0cf43a960..a73efd9ca 100644 --- a/src/hb-ft-colr.hh +++ b/src/hb-ft-colr.hh @@ -216,7 +216,6 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line, hb_lock_t lock (c->ft_font->lock); FT_ColorStop stop; unsigned wrote = 0; - FT_ColorStopIterator iter = cl->color_stop_iterator; if (start >= cl->color_stop_iterator.num_color_stops) { @@ -233,9 +232,6 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line, // FT_Get_Colorline_Stops can in some cases return 0, and // does not advance the iterator. So stop iteration to prevent // infinite loop here. - // - // reset the iterator for next time - cl->color_stop_iterator = iter; return cl->color_stop_iterator.num_color_stops; } } @@ -259,8 +255,6 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line, } *count = wrote; - // reset the iterator for next time - cl->color_stop_iterator = iter; // recurse() stops once this runs out hb_budget_spend (*c->budget, HB_BUDGET_16, wrote);