mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-29 09:34:05 +01:00
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:
9 files changed
+201
-32
No files matched your search
+42
-14
@@ -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);
|
||||
}
|
||||
@@ -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<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);
|
||||
@@ -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<hb_tag_t, hb_tag_t>);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
+3
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
+18
-6
@@ -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
|
||||
@@ -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<hb_tag_t, hb::unique_ptr<hb_set_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
|
||||
|
||||
|
||||
Reference in new issue
Block a user