diff --git a/src/graph/classdef-graph.hh b/src/graph/classdef-graph.hh index 03046d16c..44a328ad1 100644 --- a/src/graph/classdef-graph.hh +++ b/src/graph/classdef-graph.hh @@ -66,6 +66,7 @@ struct ClassDef : public OT::ClassDef unsigned max_size) { unsigned class_def_prime_id = c.graph.new_node (nullptr, nullptr); + if (class_def_prime_id == (unsigned) -1) return false; auto& class_def_prime_vertex = c.graph.vertices_[class_def_prime_id]; if (!make_class_def (c, glyph_and_class, class_def_prime_id, max_size)) return false; diff --git a/src/graph/coverage-graph.hh b/src/graph/coverage-graph.hh index 457aab771..4cc1de675 100644 --- a/src/graph/coverage-graph.hh +++ b/src/graph/coverage-graph.hh @@ -88,6 +88,8 @@ struct Coverage : public OT::Layout::Common::Coverage unsigned max_size) { unsigned coverage_prime_id = c.graph.new_node (nullptr, nullptr); + if (coverage_prime_id == (unsigned) -1) + return nullptr; auto& coverage_prime_vertex = c.graph.vertices_[coverage_prime_id]; if (!make_coverage (c, glyphs, coverage_prime_id, max_size)) return nullptr; diff --git a/src/graph/graph.hh b/src/graph/graph.hh index d1030dbdb..09ea518a8 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -598,6 +598,8 @@ struct graph_t unsigned parent_id, unsigned child_id) { + if (unlikely (!check_success (parent_id < vertices_.length && child_id < vertices_.length))) + return; auto& v = vertices_[parent_id]; auto* link = v.obj.real_links.push (); link->width = 2; @@ -658,6 +660,8 @@ struct graph_t const vertex_t& next = vertices_[next_id]; for (const auto& link : next.obj.all_links ()) { + if (unlikely (!check_success (link.objidx < vertices_.length))) + continue; removed_edges[link.objidx]++; const auto& v = vertices_[link.objidx]; if (!(v.incoming_edges () - removed_edges[link.objidx])) @@ -1095,11 +1099,17 @@ struct graph_t distance_invalid = true; positions_invalid = true; + if (unlikely (!check_success (old_parent_idx < vertices_.length && + new_parent_idx < vertices_.length))) + return -1; + auto& old_v = vertices_[old_parent_idx]; auto& new_v = vertices_[new_parent_idx]; unsigned child_id = index_for_offset (old_parent_idx, old_offset); + if (unlikely (!check_success (child_id < vertices_.length))) + return -1; auto* new_link = new_v.obj.real_links.push (); new_link->width = O::static_size; @@ -1341,7 +1351,7 @@ struct graph_t DEBUG_MSG (SUBSET_REPACK, nullptr, " Duplicating %u, ..., %u => %u", first_parent, last_parent, child_idx); unsigned clone_idx = duplicate (child_idx); - if (clone_idx == (unsigned) -1) return false; + if (clone_idx == (unsigned) -1) return -1; for (unsigned parent_idx : *parents) { // duplicate shifts the root node idx, so if parent_idx was root update it. @@ -1642,10 +1652,16 @@ struct graph_t for (unsigned p = 0; p < count; p++) { for (auto& l : vertices_.arrayZ[p].obj.real_links) + { + if (unlikely (l.objidx >= count)) { check_success (false); continue; } vertices_[l.objidx].add_parent (p, false); + } for (auto& l : vertices_.arrayZ[p].obj.virtual_links) + { + if (unlikely (l.objidx >= count)) { check_success (false); continue; } vertices_[l.objidx].add_parent (p, true); + } } for (unsigned i = 0; i < count; i++) @@ -1715,6 +1731,9 @@ struct graph_t for (const auto& link : next.obj.all_links ()) { + if (unlikely (!check_success (link.objidx < vertices_.length))) + continue; + if (visited[link.objidx]) continue; auto& child_v = vertices_.arrayZ[link.objidx]; diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 44f777be6..a4099d6bf 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -229,6 +229,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 } void add_virtual_link(gsubgpos_graph_context_t& c, unsigned from, unsigned to) const { + if (from >= c.graph.vertices_.length || to >= c.graph.vertices_.length) return; auto& from_obj = c.graph.vertices_[from].obj; c.graph.vertices_[to].add_parent(from, true); auto& link = *from_obj.virtual_links.push (); @@ -295,7 +296,9 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 // Create a place holder coverage prime id since we need to add virtual links to it while // generating liga and liga sets. Afterwards it will be updated to have the correct coverage. unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage); + if (coverage_id == (unsigned) -1) return -1; unsigned coverage_prime_id = c.graph.duplicate(coverage_id); + if (coverage_prime_id == (unsigned) -1) return -1; auto& coverage_prime_vertex = c.graph.vertices_[coverage_prime_id]; auto* coverage_prime_link = c.graph.vertices_[liga_subst_prime_id].obj.real_links.push (); coverage_prime_link->width = SmallTypes::size; diff --git a/src/graph/markbasepos-graph.hh b/src/graph/markbasepos-graph.hh index e476a90b8..b328b7285 100644 --- a/src/graph/markbasepos-graph.hh +++ b/src/graph/markbasepos-graph.hh @@ -217,6 +217,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2 (); const unsigned base_size = OT::Layout::GPOS_impl::MarkBasePosFormat1_2::min_size + MarkArray::min_size + @@ -261,6 +262,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2 (); split_context_t split_context { c, this, @@ -346,6 +348,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2 (); auto& base_array_v = c.graph.vertices_[base_array_id]; for (const auto& link : base_array_v.obj.real_links) @@ -429,11 +432,12 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2baseCoverage), prime_id, base_coverage_id); auto mark_coverage = sc.c.graph.as_table (this_index, &markCoverage); - if (!mark_coverage) return false; + if (!mark_coverage) return -1; hb_set_t marks = sc.marks_for (start, end); auto new_coverage = + hb_enumerate (mark_coverage.table->iter ()) @@ -456,6 +460,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2markArray), prime_id, new_mark_array); unsigned class_count = classCount; @@ -466,6 +471,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2clone (sc.c, base_array.index, start, end, this->classCount); + if (new_base_array == (unsigned) -1) return -1; graph.add_link (&(prime->baseArray), prime_id, new_base_array); return prime_id; diff --git a/src/graph/pairpos-graph.hh b/src/graph/pairpos-graph.hh index 65b170017..c2b182dcf 100644 --- a/src/graph/pairpos-graph.hh +++ b/src/graph/pairpos-graph.hh @@ -54,6 +54,7 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3 (); const unsigned coverage_size = c.graph.vertices_[coverage_id].table_size (); const unsigned base_size = OT::Layout::GPOS_impl::PairPosFormat1_3::min_size; @@ -173,6 +174,7 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3width = SmallTypes::size; class_def_link->objidx = class_def_2_id; @@ -554,6 +560,8 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4& overflows, for (int i = overflows.length - 1; i >= 0; i--) { const graph::overflow_record_t& r = overflows[i]; - const auto& child = sorted_graph.vertices_[r.child]; - if (child.is_shared ()) + if (sorted_graph.vertices_[r.child].is_shared ()) { // The child object is shared, we may be able to eliminate the overflow // by duplicating it. @@ -315,7 +314,7 @@ bool _process_overflows (const hb_vector_t& overflows, // (eg. all links from the same parent) in this case continue on to other resolution options. } - if (child.is_leaf () && !priority_bumped_parents.has (r.parent)) + if (sorted_graph.vertices_[r.child].is_leaf () && !priority_bumped_parents.has (r.parent)) { // This object is too far from it's parent, attempt to move it closer. // diff --git a/src/test-repacker.cc b/src/test-repacker.cc index bf0c38871..a888f2e40 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -2852,12 +2852,58 @@ test_32bit_roots_traversal () free (buffer); } +static void test_invalid_link_objidx () +{ + size_t buffer_size = 100; + void* buffer = malloc (buffer_size); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_complex_2 (&c); + + graph_t graph (c.object_graph ()); + hb_always_assert (!graph.in_error ()); + + // Poison a link to point to (unsigned) -1 + graph.vertices_[graph.root_idx ()].obj.real_links[0].objidx = (unsigned) -1; + + // Running Dijkstra / shortest distance sorting must not crash or OOB access, + // and must mark the graph in error. + graph.sort_shortest_distance (); + hb_always_assert (graph.in_error ()); + + free (buffer); +} + +static void test_invalid_link_add_and_move () +{ + size_t buffer_size = 100; + void* buffer = malloc (buffer_size); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_complex_2 (&c); + + graph_t graph (c.object_graph ()); + hb_always_assert (!graph.in_error ()); + + // add_link with out-of-bounds child + OT::Offset16 dummy; + graph.add_link (&dummy, 0, (unsigned) -1); + hb_always_assert (graph.in_error ()); + + // move_child with non-existent offset + unsigned res = graph.move_child (0, &dummy, 1, &dummy); + hb_always_assert (res == (unsigned) -1); + hb_always_assert (graph.in_error ()); + + free (buffer); +} + // TODO(garretrieger): update will_overflow tests to check the overflows array. // TODO(garretrieger): add tests for priority raising. int main (int argc, char **argv) { + test_invalid_link_objidx (); + test_invalid_link_add_and_move (); test_serialize (); test_sort_shortest (); test_will_overflow_1 ();