diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 354270dc7..92afe433e 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -149,7 +149,9 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 // all links TRY_ASSIGN (auto index_to_id, ligature_index_to_object_id (liga_set)); - for (unsigned j = 0; j < liga_set.table->ligature.len; j++) + // Split points and liga_counts () must count the same offset slots, + // including null offsets. + for (unsigned j = 0; j < liga_set.table->ligature.len; j++, ligature_index++) { const unsigned liga_id = index_to_id[j]; if (liga_id == HB_GRAPH_INVALID) continue; // no outgoing link, ignore @@ -168,8 +170,6 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 OT::Layout::GSUB_impl::LigatureSet::min_size + // for liga set subtable liga_size; // for liga sub table } - - ligature_index++; } } @@ -340,6 +340,9 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 // a new liga set sub table and move the intersecting ligas to it. unsigned start_index = hb_max(start, current_start) - count; unsigned end_index = hb_min(end, current_end) - count; + // Narrowing to the remaining links may leave an inverted range. + if (unlikely (end_index < start_index)) + return Err(INVALID_ARGUMENT); unsigned liga_count = end_index - start_index; TRY_ASSIGN (auto result, new_liga_set(c, liga_count)); liga_set_prime_id = result.first; diff --git a/src/test-repacker.cc b/src/test-repacker.cc index 854b76a01..6371bc2be 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -1853,6 +1853,83 @@ populate_serializer_with_large_liga_overlapping_clone_result (hb_serialize_conte c->end_serialize(); } +static void +populate_serializer_with_large_liga_null_offsets (hb_serialize_context_t* c, bool split) +{ + std::string large_string(100000, 'a'); + c->start_serialize (); + + constexpr unsigned liga_size = 30000; + const char null_offsets[6] = {0}; + + unsigned liga[3]; + unsigned liga_subst[2]; + unsigned liga_set[2]; + unsigned liga_subst_count = split ? 2 : 1; + unsigned coverage; + + if (split) { + // LigaSubst 2 + coverage = add_coverage(1, 1, c); + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[2] = c->pop_pack (false); + + add_liga_set_header(1, c); + add_virtual_offset(coverage, c); + add_offset(liga[2], c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 1, c); + add_offset(liga_set[0], c); + liga_subst[1] = c->pop_pack(false); + } + + // LigaSubst 1 + coverage = add_coverage(0, 1, c); + for (int i = split ? 1 : 2; i >= 0; i--) { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i] = c->pop_pack (false); + } + + add_liga_set_header(split ? 1 : 2, c); + add_virtual_offset(coverage, c); + add_offset(liga[1], c); + if (!split) + add_offset(liga[2], c); + liga_set[1] = c->pop_pack(false); + + // One liga followed by three null offsets. + add_liga_set_header(4, c); + add_virtual_offset(coverage, c); + add_offset(liga[0], c); + extend(null_offsets, 6, c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 2, c); + add_offset(liga_set[0], c); + add_offset(liga_set[1], c); + liga_subst[0] = c->pop_pack(false); + + for (int l = liga_subst_count - 1; l >= 0; l--) { + liga_subst[l] = add_extension(liga_subst[l], 4, c); + } + + start_lookup (7, liga_subst_count, c); + for (unsigned l = 0; l < liga_subst_count; l++) { + add_offset(liga_subst[l], c); + } + + unsigned lookup = finish_lookup (c); + + unsigned lookup_list = add_lookup_list (&lookup, 1, c); + + add_gsubgpos_header (lookup_list, c); + + c->end_serialize(); +} + template