Merge pull request #6503 from harfbuzz/scanner/subsetmisc

[subset/face-builder] Fix parser, leak, and DoS issues found by OSS Scanner
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-30 17:45:06 -06:00
commit 786fddc088
9 files changed
+201 -32

No files matched your search

+71
View File
@@ -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);
+2
View File
@@ -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);
+60
View File
@@ -27,6 +27,8 @@
#include "hb-test.h"
#include "hb-subset-test.h"
#include <math.h>
/* 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);