[face-builder] Keep table tags sorted at add_table() time

_hb_face_builder_get_table_tags() copied every key out of the hash map
and qsort()ed it on each call, so a caller paging through the list (the
subsetter uses 32-entry pages in hb_subset_plan_execute_or_fail() and
_is_table_present()) paid N/32 sorts of N tags per enumeration.  With
the 65535-table face that hb_subset_preprocess() hands back this was
~25 s of CPU per hb_subset_or_fail() call.

Keep an hb_vector_t<hb_tag_t> next to the map and maintain it in
hb_face_builder_add_table(), the only place keys are inserted: a binary
search finds the slot, the vector is pre-allocated before the map is
touched so an allocation failure leaves both structures unchanged, and
the slot is opened with the same resize+memmove idiom hb-bit-set.hh
uses.  The getter becomes a sub_array() copy, so enumeration is O(page)
and the returned order is unchanged.  _hb_face_builder_data_reference_blob()
keeps its own sort, which orders by blob size, not by tag.

A regression test adds tags out of order, one of them twice, and checks
that paging with a 4-entry array returns each tag once in ascending
order.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-10-30 15:30:37 -06:00
1 parent dae0a46b4d
commit d52c540c7a
2 files changed
+75 -14

No files matched your search

+27 -14
View File
@@ -48,6 +48,7 @@ struct face_table_info_t
struct hb_face_builder_data_t
{
hb_hashmap_t<hb_tag_t, face_table_info_t> tables;
hb_vector_t<hb_tag_t> sorted_tags; /* Keys of tables, kept sorted. */
};
static int compare_entries (const void* pa, const void* pb)
@@ -74,6 +75,7 @@ _hb_face_builder_data_create ()
return nullptr;
data->tables.init ();
data->sorted_tags.init ();
return data;
}
@@ -87,6 +89,7 @@ _hb_face_builder_data_destroy (void *user_data)
hb_blob_destroy (info.data);
data->tables.fini ();
data->sorted_tags.fini ();
hb_free (data);
}
@@ -182,18 +185,8 @@ _hb_face_builder_get_table_tags (const hb_face_t *face HB_UNUSED,
return population;
}
// Sort the tags.
hb_vector_t<hb_tag_t> sorted_tags;
data->tables.keys () | hb_sink (sorted_tags);
if (unlikely (sorted_tags.in_error ()))
{
// Not much to do...
}
sorted_tags.qsort ([] (const hb_tag_t &a, const hb_tag_t &b) {
return (a > b) - (a < b);
});
auto array = sorted_tags.as_array ().sub_array (start_offset, table_count);
// Tags are kept sorted by hb_face_builder_add_table().
auto array = data->sorted_tags.as_array ().sub_array (start_offset, table_count);
if (table_tags)
{
auto out = hb_array (table_tags, *table_count);
@@ -259,9 +252,20 @@ hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;
// Keep the tags sorted here, such that get_table_tags() does not
// have to sort them on every call.
auto &sorted_tags = data->sorted_tags;
unsigned i;
bool is_new = !hb_bsearch_impl (&i, tag,
sorted_tags.arrayZ, sorted_tags.length,
sizeof (hb_tag_t),
_hb_cmp_operator<hb_tag_t, hb_tag_t>);
/* numTables in the font file's table directory is 16bit. */
if (unlikely (data->tables.get_population () >= 0xFFFFu &&
!data->tables.has (tag)))
if (unlikely (is_new && data->tables.get_population () >= 0xFFFFu))
return false;
if (is_new && unlikely (!sorted_tags.alloc (sorted_tags.length + 1)))
return false;
hb_blob_t* previous = data->tables.get (tag).data;
@@ -271,6 +275,15 @@ hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
return false;
}
if (is_new)
{
sorted_tags.resize (sorted_tags.length + 1); // Cannot fail; allocated above.
memmove (sorted_tags.arrayZ + i + 1,
sorted_tags.arrayZ + i,
(sorted_tags.length - 1 - i) * sorted_tags.item_size);
sorted_tags.arrayZ[i] = tag;
}
hb_blob_destroy (previous);
return true;
}
+48
View File
@@ -120,6 +120,53 @@ test_get_table_tags_builder_limit (void)
hb_face_destroy (face);
}
static void
test_get_table_tags_builder_unsorted (void)
{
/* Tags added out of order, one of them twice; must come back
* sorted, each once, with any page size. */
static const hb_tag_t added[] = {
HB_TAG ('m','a','x','p'),
HB_TAG ('G','S','U','B'),
HB_TAG ('h','e','a','d'),
HB_TAG ('z','z','z','z'),
HB_TAG ('h','e','a','d'),
HB_TAG ('c','m','a','p'),
HB_TAG ('G','D','E','F'),
};
static const hb_tag_t expected[] = {
HB_TAG ('G','D','E','F'),
HB_TAG ('G','S','U','B'),
HB_TAG ('c','m','a','p'),
HB_TAG ('h','e','a','d'),
HB_TAG ('m','a','x','p'),
HB_TAG ('z','z','z','z'),
};
const unsigned num_expected = sizeof (expected) / sizeof (expected[0]);
hb_face_t *face = hb_face_builder_create ();
hb_blob_t *blob = hb_blob_create ("test", 4, HB_MEMORY_MODE_READONLY, NULL, NULL);
for (unsigned i = 0; i < sizeof (added) / sizeof (added[0]); i++)
g_assert_true (hb_face_builder_add_table (face, added[i], blob));
hb_blob_destroy (blob);
g_assert_cmpuint (num_expected, ==, hb_face_get_table_tags (face, 0, NULL, NULL));
hb_tag_t tags[4];
unsigned offset = 0, count = sizeof (tags) / sizeof (tags[0]);
while (((void) hb_face_get_table_tags (face, offset, &count, tags), count))
{
g_assert_cmpuint (offset + count, <=, num_expected);
for (unsigned i = 0; i < count; i++)
g_assert_cmpuint (tags[i], ==, expected[offset + i]);
offset += count;
count = sizeof (tags) / sizeof (tags[0]);
}
g_assert_cmpuint (offset, ==, num_expected);
hb_face_destroy (face);
}
#ifdef HAVE_FREETYPE
static void
test_get_table_tags_ft (void)
@@ -254,6 +301,7 @@ main (int argc, char **argv)
hb_test_add (test_get_table_tags_default);
hb_test_add (test_get_table_tags_builder);
hb_test_add (test_get_table_tags_builder_limit);
hb_test_add (test_get_table_tags_builder_unsorted);
#ifdef HAVE_FREETYPE
hb_test_add (test_get_table_tags_ft);
hb_test_add (test_get_table_tags_ft_stream);