[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 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 authored and GitHub committed 2026-11-10 16:29:56 -06:00
1 parent 26a6729680
commit 7f65c5d820
3 files changed
+43 -28

No files matched your search

+17
View File
@@ -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<const OT::Index>
_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
+6
View File
@@ -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<const OT::Index>
_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
+20 -28
View File
@@ -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;
}
}