Add missing error handling for fallible operations in the repacker.

This commit is contained in:
Garret Rieger committed 2026-08-27 14:22:26 -06:00
1 parent eca91585cc
commit 7eac03d53c
8 files changed
+91 -5

No files matched your search

+1
View File
@@ -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;
+2
View File
@@ -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;
+20 -1
View File
@@ -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];
+3
View File
@@ -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;
+7 -1
View File
@@ -217,6 +217,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2<S
hb_set_t visited;
const unsigned base_coverage_id = c.graph.index_for_offset (this_index, &baseCoverage);
if (base_coverage_id == (unsigned) -1) return hb_vector_t<unsigned> ();
const unsigned base_size =
OT::Layout::GPOS_impl::MarkBasePosFormat1_2<SmallTypes>::min_size +
MarkArray::min_size +
@@ -261,6 +262,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2<S
const unsigned mark_array_id = c.graph.index_for_offset (this_index, &markArray);
if (mark_array_id == (unsigned) -1) return hb_vector_t<unsigned> ();
split_context_t split_context {
c,
this,
@@ -346,6 +348,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2<S
unsigned base_array_id =
c.graph.index_for_offset (this_index, &baseArray);
if (base_array_id == (unsigned) -1) return hb_vector_t<class_info_t> ();
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_2<S
unsigned base_coverage_id =
graph.index_for_offset (sc.this_index, &baseCoverage);
if (base_coverage_id == (unsigned) -1) return -1;
graph.add_link (&(prime->baseCoverage), prime_id, base_coverage_id);
auto mark_coverage = sc.c.graph.as_table<Coverage> (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_2<S
sc.mark_array_links,
marks,
start);
if (new_mark_array == (unsigned) -1) return -1;
graph.add_link (&(prime->markArray), prime_id, new_mark_array);
unsigned class_count = classCount;
@@ -466,6 +471,7 @@ struct MarkBasePosFormat1 : public OT::Layout::GPOS_impl::MarkBasePosFormat1_2<S
base_array.table->clone (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;
+10
View File
@@ -54,6 +54,7 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3<SmallType
hb_set_t visited;
const unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage);
if (coverage_id == (unsigned) -1) return hb_vector_t<unsigned> ();
const unsigned coverage_size = c.graph.vertices_[coverage_id].table_size ();
const unsigned base_size = OT::Layout::GPOS_impl::PairPosFormat1_3<SmallTypes>::min_size;
@@ -173,6 +174,7 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3<SmallType
}
unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage);
if (coverage_id == (unsigned) -1) return -1;
if (!Coverage::clone_coverage (c,
coverage_id,
pair_pos_prime_id,
@@ -375,6 +377,8 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
graph.index_for_offset (split_context.this_index, &coverage);
unsigned class_def_1_id =
graph.index_for_offset (split_context.this_index, &classDef1);
if (coverage_id == (unsigned) -1 || class_def_1_id == (unsigned) -1)
return -1;
auto& coverage_v = graph.vertices_[coverage_id];
auto& class_def_1_v = graph.vertices_[class_def_1_id];
Coverage* coverage_table = (Coverage*) coverage_v.obj.head;
@@ -417,6 +421,8 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
// classDef2
unsigned class_def_2_id =
graph.index_for_offset (split_context.this_index, &classDef2);
if (class_def_2_id == (unsigned) -1)
return -1;
auto* class_def_link = graph.vertices_[pair_pos_prime_id].obj.real_links.push ();
class_def_link->width = SmallTypes::size;
class_def_link->objidx = class_def_2_id;
@@ -554,6 +560,8 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
unsigned this_index) const
{
unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage);
if (coverage_id == (unsigned) -1)
return &Null(Coverage);
auto& coverage_v = c.graph.vertices_[coverage_id];
Coverage* coverage_table = (Coverage*) coverage_v.obj.head;
@@ -566,6 +574,8 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
unsigned this_index) const
{
unsigned class_def_1_id = c.graph.index_for_offset (this_index, &classDef1);
if (class_def_1_id == (unsigned) -1)
return &Null(ClassDef);
auto& class_def_1_v = c.graph.vertices_[class_def_1_id];
ClassDef* class_def_1_table = (ClassDef*) class_def_1_v.obj.head;
+2 -3
View File
@@ -303,8 +303,7 @@ bool _process_overflows (const hb_vector_t<graph::overflow_record_t>& 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<graph::overflow_record_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.
//
+46
View File
@@ -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 ();