diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index b6dc5e1fa..1e7687c65 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -1322,7 +1322,8 @@ struct CFFIndex /* serialize CFFIndex header */ if (unlikely (!c->extend_min (this))) return_trace (false); - this->count = hb_len (it); + if (unlikely (!c->check_assign (this->count, hb_len (it), HB_SERIALIZE_ERROR_ARRAY_OVERFLOW))) + return_trace (false); if (!this->count) return_trace (true); if (unlikely (!c->extend (this->offSize))) return_trace (false); this->offSize = off_size; diff --git a/src/test-cff.cc b/src/test-cff.cc index c6c8e24e7..8a4ccb923 100644 --- a/src/test-cff.cc +++ b/src/test-cff.cc @@ -155,9 +155,33 @@ test_fd_select_sentinel () c.end_processing (); } +static void +test_index_count_limits () +{ + char buffer[65550]; + for (unsigned count : {0u, 65535u, 65536u, 65537u}) + { + auto items = hb_range (count) | hb_map ([] (unsigned) { return hb_bytes_t (); }); + hb_serialize_context_t c (buffer, sizeof (buffer)); + auto *index = c.start_serialize (); + bool success = index->serialize_header (&c, +items, 0); + hb_always_assert (success == (count <= 65535u)); + if (success) + hb_always_assert (index->count == count); + else + hb_always_assert (c.in_error ()); + + hb_serialize_context_t c2 (buffer, sizeof (buffer)); + auto *index2 = c2.start_serialize (); + hb_always_assert (index2->serialize_header (&c2, +items, 0)); + hb_always_assert (index2->count == count); + } +} + int main (int argc, char **argv) { + test_index_count_limits (); test_specialized_line_geometry (); test_specialized_fractional_geometry (); test_charset1_splits_long_ranges (); diff --git a/test/api/test-subset-cff1.c b/test/api/test-subset-cff1.c index b6ce63575..f9cb4f04d 100644 --- a/test/api/test-subset-cff1.c +++ b/test/api/test-subset-cff1.c @@ -520,10 +520,26 @@ test_subset_cff1_caller_supplied_subr (void) hb_face_destroy (face); } +static void +test_subset_cff1_index_count_overflow (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/SourceHanSans-Regular.41,4C2E.otf"); + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_glyph_set (input), 1); + /* The output CharStrings count must fit in a CFF1 INDEX. */ + hb_map_set (hb_subset_input_old_to_new_glyph_mapping (input), 1, 70000); + + g_assert_null (hb_subset_or_fail (face, input)); + + hb_subset_input_destroy (input); + hb_face_destroy (face); +} + int main (int argc, char **argv) { hb_test_init (&argc, &argv); + hb_test_add (test_subset_cff1_index_count_overflow); hb_test_add (test_subset_cff1_caller_supplied_subr);