From d8347f079ef2739620a533a2c91bc489f0f89b28 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 29 Oct 2026 21:00:03 -0600 Subject: [PATCH] [repacker] Count null offsets when splitting ligatures (#6476) Ligature split points skipped null offsets while the ranges used to clone and shrink subtables counted every slot. Advance the split index for null offsets too, so both stages refer to the same ligatures. Also reject an inverted range after narrowing it to the remaining links, before calculating the number of ligatures to allocate. Add a regression that compares the split graph with the expected subtables when one ligature set has trailing null offsets. Testing: meson test -C build (292 passed; check-symbols skipped on macOS), plus the repacker test and shared-ligature font check with ASan and UBSan. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-9C9WB0KM. Assisted-by: OpenAI Codex --- src/graph/ligature-graph.hh | 9 ++-- src/test-repacker.cc | 101 ++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) 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