From 7f65c5d8204f7730bcbb508d508121aae6de177d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 10 Nov 2026 16:29:56 -0600 Subject: [PATCH] [ot-map] Resolve a feature once in add_lookups() instead of once per 32 lookups (#6626) hb_ot_map_builder_t::add_lookups() paged through a feature's lookup indices 32 at a time with hb_ot_layout_feature_with_variations_get_lookups(), which re-resolves the feature on every call. Under a FeatureVariations record that resolution is a linear search of the substitution records, so a feature with many lookups and many variation records cost time quadratic in the number of lookups, stalling hb_shape()'s plan build. Add _hb_ot_layout_feature_with_variations_get_lookups(), which resolves the feature once and returns its whole lookup-index array, and have add_lookups() iterate that array directly. The public API is unchanged; the resulting map is identical. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-M3XZD80O. Co-authored-by: Claude Opus 4.8 --- src/hb-ot-layout.cc | 17 ++++++++++++++++ src/hb-ot-layout.hh | 6 ++++++ src/hb-ot-map.cc | 48 +++++++++++++++++++-------------------------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 2eefdf77d..156c6c293 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -1519,6 +1519,23 @@ hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face, return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes); } +/* Like hb_ot_layout_feature_with_variations_get_lookups(), but resolves the + * feature only once and returns all of its lookup indices. Resolving a feature + * under a FeatureVariations record is a linear search of the record's + * FeatureTableSubstitution, so callers must not page through the lookups. */ +hb_array_t +_hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face, + hb_tag_t table_tag, + unsigned int feature_index, + unsigned int variations_index) +{ + const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag); + + const OT::Feature &f = g.get_feature_variation (feature_index, variations_index); + + return f.lookupIndex.as_array (); +} + /* * OT::GSUB diff --git a/src/hb-ot-layout.hh b/src/hb-ot-layout.hh index 38ad88b3e..b72b629ba 100644 --- a/src/hb-ot-layout.hh +++ b/src/hb-ot-layout.hh @@ -76,6 +76,12 @@ hb_ot_layout_lookup_would_substitute_with_buffer (hb_face_t *face, bool zero_context, hb_buffer_t *buffer); +HB_INTERNAL hb_array_t +_hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face, + hb_tag_t table_tag, + unsigned int feature_index, + unsigned int variations_index); + /* * GDEF diff --git a/src/hb-ot-map.cc b/src/hb-ot-map.cc index a1f5c9e5a..c0f518cab 100644 --- a/src/hb-ot-map.cc +++ b/src/hb-ot-map.cc @@ -136,8 +136,6 @@ hb_ot_map_builder_t::add_lookups (hb_ot_map_t &m, bool per_syllable, hb_tag_t feature_tag) { - unsigned int lookup_indices[32]; - unsigned int offset, len; unsigned int table_lookup_count; table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]); @@ -166,32 +164,26 @@ hb_ot_map_builder_t::add_lookups (hb_ot_map_t &m, return; } - offset = 0; - do { - len = ARRAY_LENGTH (lookup_indices); - hb_ot_layout_feature_with_variations_get_lookups (face, - table_tags[table_index], - feature_index, - key.variations_index[table_index], - offset, &len, - lookup_indices); - - for (unsigned int i = 0; i < len; i++) - { - if (lookup_indices[i] >= table_lookup_count) - continue; - hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push (); - lookup->mask = mask; - lookup->index = lookup_indices[i]; - lookup->auto_zwnj = auto_zwnj; - lookup->auto_zwj = auto_zwj; - lookup->random = random; - lookup->per_syllable = per_syllable; - lookup->feature_tag = feature_tag; - } - - offset += len; - } while (len == ARRAY_LENGTH (lookup_indices)); + /* Resolve the feature once: under a FeatureVariations record that is a + * linear search of the substitution records, so don't repeat it per batch + * of lookup indices. */ + auto lookup_indices = _hb_ot_layout_feature_with_variations_get_lookups (face, + table_tags[table_index], + feature_index, + key.variations_index[table_index]); + for (unsigned int lookup_index : lookup_indices) + { + if (lookup_index >= table_lookup_count) + continue; + hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push (); + lookup->mask = mask; + lookup->index = lookup_index; + lookup->auto_zwnj = auto_zwnj; + lookup->auto_zwj = auto_zwj; + lookup->random = random; + lookup->per_syllable = per_syllable; + lookup->feature_tag = feature_tag; + } }