diff --git a/src/hb-face-builder.cc b/src/hb-face-builder.cc index cb55e4b8f..7e0c1fb72 100644 --- a/src/hb-face-builder.cc +++ b/src/hb-face-builder.cc @@ -48,6 +48,7 @@ struct face_table_info_t struct hb_face_builder_data_t { hb_hashmap_t tables; + hb_vector_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); } @@ -99,7 +102,16 @@ _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data) unsigned int face_length = table_count * 16 + 12; for (auto info : data->tables.values()) - face_length += hb_ceil_to_4 (hb_blob_get_length (info.data)); + if (unlikely (hb_unsigned_add_overflows (face_length, + hb_ceil_to_4 (hb_blob_get_length (info.data)), + &face_length))) + return nullptr; + + /* hb_blob_create() refuses such a length; do not assemble the font + * only to throw it away. Tables referenced from a font file can + * overlap in it, so the sum is not bounded by the size of that file. */ + if (unlikely (face_length >= 1u << 31)) + return nullptr; char *buf = (char *) hb_malloc (face_length); if (unlikely (!buf)) @@ -173,18 +185,8 @@ _hb_face_builder_get_table_tags (const hb_face_t *face HB_UNUSED, return population; } - // Sort the tags. - hb_vector_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); @@ -234,7 +236,8 @@ hb_face_builder_create () * @blob: The blob containing the table data to add * * Add table for @tag with data provided by @blob to the face. @face must - * be created using hb_face_builder_create(). + * be created using hb_face_builder_create(). A face can hold at most + * 65535 tables; adding more fails. * * Since: 1.9.0 **/ @@ -249,6 +252,22 @@ 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); + + /* numTables in the font file's table directory is 16bit. */ + 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; if (!data->tables.set (tag, face_table_info_t {hb_blob_reference (blob), (unsigned) -1})) { @@ -256,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; } diff --git a/src/hb-face.cc b/src/hb-face.cc index ee633a624..badf5d9c3 100644 --- a/src/hb-face.cc +++ b/src/hb-face.cc @@ -745,7 +745,9 @@ hb_face_reference_blob (hb_face_t *face) } } - blob = hb_face_reference_blob (builder); + // Serialize the builder directly; going through hb_face_reference_blob() + // again would recurse if the builder cannot produce a blob either. + blob = builder->reference_table (HB_TAG_NONE); hb_face_destroy (builder); } } diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index c13fab3bd..32142607b 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -6092,16 +6092,10 @@ struct FeatureVariations (this+record.substitutions).collect_feature_indices (feature_indices); } - bool has_lookup_variations (unsigned feature_index) const + void collect_lookup_variation_feature_indices (hb_set_t *feature_indices) const { for (const LookupVariationRecord &record : get_lookup_variation_records ()) - { - if (record.featureIndex == feature_index) - return true; - if (record.featureIndex > feature_index) - break; - } - return false; + feature_indices->add (record.featureIndex); } bool subset (hb_subset_context_t *c, diff --git a/src/hb-subset-input.cc b/src/hb-subset-input.cc index 00d538117..7b4d768ee 100644 --- a/src/hb-subset-input.cc +++ b/src/hb-subset-input.cc @@ -627,11 +627,11 @@ hb_subset_axis_range_from_string (const char *str, int len, len = strlen (str); const char *end = str + len; - const char* part = strpbrk (str, ":"); + const char* part = (const char *) memchr (str, ':', end - str); if (!part) { // Single value. - if (strcmp (str, "drop") == 0) + if (len == 4 && 0 == hb_memcmp (str, "drop", 4)) { *axis_min_value = NAN; *axis_def_value = NAN; @@ -652,13 +652,13 @@ hb_subset_axis_range_from_string (const char *str, int len, int count = 0; for (int i = 0; i < 3; i++) { count++; - if (!*str || part == str) + if (str == end || part == str) { values[i] = NAN; if (part == NULL) break; str = part + 1; - part = strpbrk (str, ":"); + part = (const char *) memchr (str, ':', end - str); continue; } @@ -669,7 +669,7 @@ hb_subset_axis_range_from_string (const char *str, int len, if (part == NULL) break; str = part + 1; - part = strpbrk (str, ":"); + part = (const char *) memchr (str, ':', end - str); } if (count == 2) @@ -1248,7 +1248,19 @@ hb_subset_input_override_name_table (hb_subset_input_t *input, hb_memcpy (override_name, name_str, str_len); name_bytes = hb_bytes_t (override_name, str_len); } - input->name_table_overrides.set (hb_ot_name_record_ids_t (platform_id, encoding_id, language_id, name_id), name_bytes); + + hb_ot_name_record_ids_t record_ids (platform_id, encoding_id, language_id, name_id); + + /* Overriding the same record again replaces the previous string; free it. */ + hb_bytes_t *old_bytes; + if (input->name_table_overrides.has (record_ids, &old_bytes)) + old_bytes->fini (); + + if (unlikely (!input->name_table_overrides.set (record_ids, name_bytes))) + { + name_bytes.fini (); + return false; + } return true; } #endif diff --git a/src/hb-subset-plan-layout.cc b/src/hb-subset-plan-layout.cc index a2a963335..223c90e9e 100644 --- a/src/hb-subset-plan-layout.cc +++ b/src/hb-subset-plan-layout.cc @@ -295,6 +295,8 @@ _GSUBGPOS_find_duplicate_features (const OT::GSUBGPOS &g, hb_set_t variable_features; if (!all_axes_pinned) g.get_feature_variations ().collect_feature_indices (&variable_features); + /* Lookup variations are instanced, not dropped, when all axes are pinned. */ + g.get_feature_variations ().collect_lookup_variation_feature_indices (&variable_features); if (unlikely (variable_features.in_error ())) return; #endif hb_hashmap_t> unique_features; @@ -320,9 +322,7 @@ _GSUBGPOS_find_duplicate_features (const OT::GSUBGPOS &g, { #ifndef HB_NO_VAR /* Matching base lookups do not imply matching alternate behavior. */ - if (variable_features.has (i) || variable_features.has (other_f_index) || - g.get_feature_variations ().has_lookup_variations (i) || - g.get_feature_variations ().has_lookup_variations (other_f_index)) + if (variable_features.has (i) || variable_features.has (other_f_index)) continue; #endif diff --git a/test/api/test-get-table-tags.c b/test/api/test-get-table-tags.c index e941d588a..2fb1f2fd2 100644 --- a/test/api/test-get-table-tags.c +++ b/test/api/test-get-table-tags.c @@ -98,6 +98,75 @@ test_get_table_tags_builder (void) hb_face_destroy (face); } +static void +test_get_table_tags_builder_limit (void) +{ + /* A font file's table directory cannot hold more than 65535 tables. */ + 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 = 1; i <= 0xFFFFu; i++) + g_assert_true (hb_face_builder_add_table (face, i, blob)); + g_assert_false (hb_face_builder_add_table (face, 0x10000u, blob)); + /* Replacing a table is still fine. */ + g_assert_true (hb_face_builder_add_table (face, 1, blob)); + g_assert_cmpuint (0xFFFFu, ==, hb_face_get_table_tags (face, 0, NULL, NULL)); + + hb_blob_t *file = hb_face_reference_blob (face); + g_assert_cmpuint (12 + 0xFFFFu * (16 + 4), ==, hb_blob_get_length (file)); + + hb_blob_destroy (file); + hb_blob_destroy (blob); + 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) @@ -231,6 +300,8 @@ 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); diff --git a/test/api/test-subset-nameids.c b/test/api/test-subset-nameids.c index 7fd4f0fd3..b2ef698a5 100644 --- a/test/api/test-subset-nameids.c +++ b/test/api/test-subset-nameids.c @@ -86,6 +86,8 @@ test_subset_name_overrides (void) hb_set_add_range (name_ids, 0, 13); hb_subset_input_t *subset_input = hb_subset_test_create_input_from_nameids (name_ids); + /* Override the same record twice; only the last string must survive (and the first must not leak). */ + hb_subset_input_override_name_table (subset_input, 1, 1, 0, 0, "Roboto Replaced", -1); hb_subset_input_override_name_table (subset_input, 1, 1, 0, 0, str1, -1); hb_subset_input_override_name_table (subset_input, 1, 3, 1, 0x409, str1_3, -1); hb_subset_input_override_name_table (subset_input, 2, 1, 0, 0, str2, 4); diff --git a/test/api/test-subset.c b/test/api/test-subset.c index 7267741f4..ea1f5c3eb 100644 --- a/test/api/test-subset.c +++ b/test/api/test-subset.c @@ -27,6 +27,8 @@ #include "hb-test.h" #include "hb-subset-test.h" +#include + /* Unit tests for hb-subset-glyf.h */ static void @@ -649,6 +651,63 @@ test_subset_create_for_tables_face_MAXP (void) } #endif +static void +test_subset_axis_range_from_string (void) +{ + float min, max, def; + + /* An explicit len must bound the parse: bytes after len are not part of + * the string and must be neither read nor interpreted. */ + g_assert_true (hb_subset_axis_range_from_string ("100:200:", 3, &min, &max, &def)); + g_assert_cmpfloat (min, ==, 100.f); + g_assert_cmpfloat (def, ==, 100.f); + g_assert_cmpfloat (max, ==, 100.f); + + g_assert_true (hb_subset_axis_range_from_string ("100:200:", 7, &min, &max, &def)); + g_assert_cmpfloat (min, ==, 100.f); + g_assert_true (isnan (def)); + g_assert_cmpfloat (max, ==, 200.f); + + g_assert_true (hb_subset_axis_range_from_string ("100:200,wdth=75:100", 7, &min, &max, &def)); + g_assert_cmpfloat (min, ==, 100.f); + g_assert_true (isnan (def)); + g_assert_cmpfloat (max, ==, 200.f); + + g_assert_true (hb_subset_axis_range_from_string ("100:300:200,", 11, &min, &max, &def)); + g_assert_cmpfloat (min, ==, 100.f); + g_assert_cmpfloat (def, ==, 300.f); + g_assert_cmpfloat (max, ==, 200.f); + + g_assert_true (hb_subset_axis_range_from_string (":300:,", 5, &min, &max, &def)); + g_assert_true (isnan (min)); + g_assert_cmpfloat (def, ==, 300.f); + g_assert_true (isnan (max)); + + g_assert_true (hb_subset_axis_range_from_string ("drop,wdth=75", 4, &min, &max, &def)); + g_assert_true (isnan (min)); + g_assert_true (isnan (def)); + g_assert_true (isnan (max)); + + g_assert_true (hb_subset_axis_range_from_string ("dropped", 4, &min, &max, &def)); + g_assert_true (isnan (min)); + g_assert_false (hb_subset_axis_range_from_string ("dropped", -1, &min, &max, &def)); + g_assert_false (hb_subset_axis_range_from_string ("drop", 3, &min, &max, &def)); + g_assert_false (hb_subset_axis_range_from_string ("", 0, &min, &max, &def)); + + /* NUL-terminated input behaves as before. */ + g_assert_true (hb_subset_axis_range_from_string ("100:200", -1, &min, &max, &def)); + g_assert_cmpfloat (min, ==, 100.f); + g_assert_true (isnan (def)); + g_assert_cmpfloat (max, ==, 200.f); + + g_assert_true (hb_subset_axis_range_from_string ("drop", -1, &min, &max, &def)); + g_assert_true (isnan (min)); + g_assert_true (isnan (def)); + g_assert_true (isnan (max)); + + g_assert_false (hb_subset_axis_range_from_string ("abc", -1, &min, &max, &def)); +} + #ifdef HB_EXPERIMENTAL_API const uint8_t CFF2[226] = { // From https://learn.microsoft.com/en-us/typography/opentype/spec/cff2 @@ -1043,6 +1102,7 @@ main (int argc, char **argv) #ifndef HB_NO_BEYOND_64K hb_test_add (test_subset_create_for_tables_face_MAXP); #endif + hb_test_add (test_subset_axis_range_from_string); #ifdef HB_EXPERIMENTAL_API hb_test_add (test_subset_input_to_string); diff --git a/test/fuzzing/fonts/gsub-lookup-variation-feature-dedup-timeout.ttf b/test/fuzzing/fonts/gsub-lookup-variation-feature-dedup-timeout.ttf new file mode 100644 index 000000000..9cc9a96f1 Binary files /dev/null and b/test/fuzzing/fonts/gsub-lookup-variation-feature-dedup-timeout.ttf differ