[depend] Fail graph construction on allocation errors

Dependency graph extraction must not silently drop edges when allocation
fails. Mark the builder failed when set, edge, or context storage cannot
be allocated, and stop context extraction before recording invalid set
indexes.

This keeps hb_subset_depend_from_face_or_fail() fail-closed instead of
returning a partial dependency graph that could under-approximate closure.

Testing: meson test -C build --suite api

Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod committed 2026-07-14 11:25:10 -06:00
1 parent 77256e876b
commit e2ea4c5301
4 files changed
+123 -17

No files matched your search

+7
View File
@@ -44,8 +44,15 @@ struct Ligature
hb_set_t complete_ligset;
complete_ligset.add (first);
+ hb_iter (component) | hb_sink (complete_ligset);
if (unlikely (complete_ligset.in_error ()))
{
c->depend_data->fail ();
return;
}
hb_codepoint_t ligset_idx = c->depend_data->new_ligature_set(complete_ligset);
if (unlikely (ligset_idx == HB_CODEPOINT_INVALID))
return;
// Track whether any edge using this ligset_idx was actually added
bool any_added = false;
@@ -90,6 +90,8 @@ struct ReverseChainSingleSubstFormat1
else if (back_glyphs.get_population () > 1)
{
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (back_glyphs);
if (unlikely (set_idx == HB_CODEPOINT_INVALID))
return;
context_glyphs.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx);
}
}
@@ -105,14 +107,24 @@ struct ReverseChainSingleSubstFormat1
else if (look_glyphs.get_population () > 1)
{
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (look_glyphs);
if (unlikely (set_idx == HB_CODEPOINT_INVALID))
return;
context_glyphs.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx);
}
}
/* Allocate context set and save/restore around edge creation */
if (unlikely (context_glyphs.in_error ()))
{
c->depend_data->fail ();
return;
}
hb_codepoint_t context_set_idx = context_glyphs.is_empty ()
? HB_CODEPOINT_INVALID
: c->depend_data->find_or_create_context_set (context_glyphs);
if (unlikely (!context_glyphs.is_empty () &&
context_set_idx == HB_CODEPOINT_INVALID))
return;
hb_codepoint_t saved_context = c->depend_data->current_context_set_index;
c->depend_data->current_context_set_index = context_set_idx;
+30 -7
View File
@@ -302,7 +302,7 @@ struct hb_depend_data_builder_t
return;
}
data.sets[set_index]->clear ();
free_set_list.push (set_index);
check_success (free_set_list.push_or_fail (set_index));
}
/* Allocate a new ligature set (no deduplication). */
@@ -321,16 +321,23 @@ struct hb_depend_data_builder_t
{
set_index = free_set_list.pop ();
data.sets[set_index]->set (set);
if (unlikely (data.sets[set_index]->in_error ()))
return fail_invalid ();
}
else
{
set_index = data.sets.length;
hb_set_t *new_set = hb_set_create ();
if (unlikely (!new_set))
return HB_CODEPOINT_INVALID;
return fail_invalid ();
new_set->set (set);
if (unlikely (new_set->in_error ()))
{
hb_set_destroy (new_set);
return fail_invalid ();
}
if (unlikely (!data.sets.push_or_fail (hb::unique_ptr<hb_set_t> {new_set})))
return HB_CODEPOINT_INVALID;
return fail_invalid ();
}
return set_index;
@@ -348,7 +355,8 @@ struct hb_depend_data_builder_t
if (unlikely (new_idx == HB_CODEPOINT_INVALID))
return HB_CODEPOINT_INVALID;
set_to_index.set (data.sets[new_idx].get (), new_idx);
if (unlikely (!set_to_index.set (data.sets[new_idx].get (), new_idx)))
return fail_invalid ();
return new_idx;
}
@@ -401,10 +409,14 @@ struct hb_depend_data_builder_t
hb_set_t filtered_set;
filtered_set.set (back_set);
filtered_set.subtract (direct_requirements);
if (unlikely (filtered_set.in_error ()))
return fail_invalid ();
if (!filtered_set.is_empty ())
{
hb_codepoint_t set_idx = find_or_create_context_set (filtered_set);
if (unlikely (set_idx == HB_CODEPOINT_INVALID))
return HB_CODEPOINT_INVALID;
context_elements.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx);
}
}
@@ -420,10 +432,14 @@ struct hb_depend_data_builder_t
hb_set_t filtered_set;
filtered_set.set (look_set);
filtered_set.subtract (direct_requirements);
if (unlikely (filtered_set.in_error ()))
return fail_invalid ();
if (!filtered_set.is_empty ())
{
hb_codepoint_t set_idx = find_or_create_context_set (filtered_set);
if (unlikely (set_idx == HB_CODEPOINT_INVALID))
return HB_CODEPOINT_INVALID;
context_elements.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx);
}
}
@@ -431,6 +447,9 @@ struct hb_depend_data_builder_t
}
context_elements.union_ (direct_requirements);
if (unlikely (direct_requirements.in_error () ||
context_elements.in_error ()))
return fail_invalid ();
if (context_elements.is_empty ())
return HB_CODEPOINT_INVALID;
@@ -455,11 +474,13 @@ struct hb_depend_data_builder_t
if (seen_edges.has (key))
return false;
seen_edges.set (key, true);
if (unlikely (!seen_edges.set (key, true)))
return fail ();
auto &gdr = data.glyph_dependencies[target];
gdr.dependencies.push (table_tag, dependent, layout_tag, lig_set,
context_set, flags);
if (unlikely (!gdr.dependencies.push_or_fail (table_tag, dependent, layout_tag,
lig_set, context_set, flags)))
return fail ();
return true;
}
@@ -495,6 +516,8 @@ struct hb_depend_data_builder_t
HB_INTERNAL bool compile (hb_face_t *face);
bool fail () { successful = false; return false; }
hb_codepoint_t fail_invalid () { fail (); return HB_CODEPOINT_INVALID; }
bool check_success (bool s) { successful = (successful && s); return successful; }
HB_INTERNAL void get_gsub_dependencies (hb_face_t *face);
+74 -10
View File
@@ -440,9 +440,9 @@ struct hb_depend_context_t :
hb_vector_t<hb_set_t> lookahead_sets; /* Sets of glyphs in lookahead positions */
};
void push_context (context_info_t &&ctx)
bool push_context (context_info_t &&ctx)
{
context_stack.push (std::move (ctx));
return depend_data->check_success (context_stack.push_or_fail (std::move (ctx)));
}
void pop_context ()
@@ -1975,6 +1975,11 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c,
hb_set_t filtered;
filtered.set (*original_set);
filtered.subtract (position_context);
if (unlikely (filtered.in_error ()))
{
c->depend_data->fail ();
return;
}
if (filtered == *original_set) {
/* No overlap with position_context - add full requirement */
@@ -1992,10 +1997,17 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c,
hb_set_t filtered;
filtered.set ((*input_position_glyphs)[j]);
filtered.subtract (position_context);
if (unlikely (filtered.in_error ()))
{
c->depend_data->fail ();
return;
}
if (filtered == (*input_position_glyphs)[j]) {
/* No overlap with position_context - add full requirement */
hb_codepoint_t idx = c->depend_data->find_or_create_context_set ((*input_position_glyphs)[j]);
if (unlikely (idx == HB_CODEPOINT_INVALID))
return;
filtered_disjunctive_indices.add (HB_DEPEND_CONTEXT_SET_FLAG | idx);
}
/* Otherwise: position_context intersects, requirement satisfied */
@@ -2004,11 +2016,20 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c,
/* Combine direct + disjunctive */
position_context.union_ (filtered_disjunctive_indices);
if (unlikely (position_context.in_error () ||
filtered_disjunctive_indices.in_error ()))
{
c->depend_data->fail ();
return;
}
/* Allocate final context_set */
hb_codepoint_t context_set_idx = position_context.is_empty ()
? HB_CODEPOINT_INVALID
: c->depend_data->find_or_create_context_set (position_context);
if (unlikely (!position_context.is_empty () &&
context_set_idx == HB_CODEPOINT_INVALID))
return;
/* Save outer context to restore after this lookup. When a contextual lookup
* calls another contextual lookup, we want each lookup in this rule to see
@@ -2077,7 +2098,11 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c,
covered_seq_indicies.add (seqIndex);
hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs))
{
c->depend_data->current_context_set_index = saved_context_set_index;
c->depend_data->current_edge_flags = saved_edge_flags;
return;
}
if (has_pos_glyphs) {
*cur_active_glyphs = std::move (pos_glyphs);
} else {
@@ -2350,7 +2375,11 @@ static inline void context_depend_lookup (hb_depend_context_t *c,
collect_coverage (&pos0_glyphs, value, lookup_context.intersects_data);
break;
}
input_position_glyphs.push (pos0_glyphs);
if (unlikely (!input_position_glyphs.push_or_fail (pos0_glyphs)))
{
c->depend_data->fail ();
return;
}
/* Positions 1+ (Input array) */
for (unsigned i = 0; i < inputCount - 1; i++)
@@ -2368,12 +2397,17 @@ static inline void context_depend_lookup (hb_depend_context_t *c,
collect_coverage (&pos_glyphs, input[i], lookup_context.intersects_data);
break;
}
input_position_glyphs.push (pos_glyphs);
if (unlikely (!input_position_glyphs.push_or_fail (pos_glyphs)))
{
c->depend_data->fail ();
return;
}
}
/* Push context before recursing (for backtrack/lookahead tracking - empty for ContextSubst) */
typename hb_depend_context_t::context_info_t ctx_info;
c->push_context (std::move (ctx_info));
if (unlikely (!c->push_context (std::move (ctx_info))))
return;
context_depend_recurse_lookups (c,
inputCount, input,
@@ -3664,7 +3698,13 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
break;
}
if (!pos_glyphs.is_empty ())
ctx_info.backtrack_sets.push (pos_glyphs);
{
if (unlikely (!ctx_info.backtrack_sets.push_or_fail (pos_glyphs)))
{
c->depend_data->fail ();
return;
}
}
}
/* Extract lookahead glyphs */
@@ -3684,7 +3724,13 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
break;
}
if (!pos_glyphs.is_empty ())
ctx_info.lookahead_sets.push (pos_glyphs);
{
if (unlikely (!ctx_info.lookahead_sets.push_or_fail (pos_glyphs)))
{
c->depend_data->fail ();
return;
}
}
}
/* Build preliminary_context with backtrack + lookahead encoded */
@@ -3695,6 +3741,8 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
preliminary_context.add (back_set.get_min ()); // Direct glyph
} else if (back_set.get_population () > 1) {
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (back_set);
if (unlikely (set_idx == HB_CODEPOINT_INVALID))
return;
preliminary_context.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx); // Encoded set reference
}
}
@@ -3704,9 +3752,16 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
preliminary_context.add (look_set.get_min ()); // Direct glyph
} else if (look_set.get_population () > 1) {
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (look_set);
if (unlikely (set_idx == HB_CODEPOINT_INVALID))
return;
preliminary_context.add (HB_DEPEND_CONTEXT_SET_FLAG | set_idx); // Encoded set reference
}
}
if (unlikely (preliminary_context.in_error ()))
{
c->depend_data->fail ();
return;
}
/* Build glyph sets for ALL input positions */
hb_vector_t<hb_set_t> input_position_glyphs;
@@ -3727,7 +3782,11 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
collect_coverage (&pos0_glyphs, value, lookup_context.intersects_data[1]);
break;
}
input_position_glyphs.push (pos0_glyphs);
if (unlikely (!input_position_glyphs.push_or_fail (pos0_glyphs)))
{
c->depend_data->fail ();
return;
}
/* Positions 1+ (Input array) */
for (unsigned i = 0; i < inputCount - 1; i++)
@@ -3745,11 +3804,16 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
collect_coverage (&pos_glyphs, input[i], lookup_context.intersects_data[1]);
break;
}
input_position_glyphs.push (pos_glyphs);
if (unlikely (!input_position_glyphs.push_or_fail (pos_glyphs)))
{
c->depend_data->fail ();
return;
}
}
/* Push context before recursing (use move to avoid copy issues) */
c->push_context (std::move (ctx_info));
if (unlikely (!c->push_context (std::move (ctx_info))))
return;
context_depend_recurse_lookups (c,
inputCount, input,