[layout] Charge the skip, skipped-record and ligature walks The per-pass `len / 16` charge does not bound per-position inner work: the skipping iterator's rescans over default-ignorables, apply_lookup's walk over a rule's skipped LookupRecords, and match_input / ligate_input's walk over a ligature's components and marks. Each is a CPU-DoS a crafted font triggers under the default max_ops. Charge each on its work path only, so honest shaping is unaffected: skipping_iterator_t::next/prev charge one op per skip (a first-try match charges nothing); apply_lookup charges each skipped record; match_input charges the ligbase backward walk and ligate_input the mark re-adjust walk, once each. Reproducers drop from 20 s / 6.8 s / 3.8 s to 0.05 s / 0.003 s / 0.02 s; in-house and aots shaping tests pass with no output change (the pre-existing text-rendering-tests failures are unchanged). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index f8966f1..ecd6f80 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh
@@ -1085,6 +1085,11 @@ return false; } case SKIP: + /* Only skipping charges, so a first-try match adds no work while + * repeated rescans over default-ignorables exhaust the budget. Keep + * walking (idx advances), so the loop-end still sets *unsafe_to; the + * outer per-pass max_ops check stops shaping. */ + c->buffer->max_ops--; continue; } } @@ -1113,6 +1118,8 @@ return false; } case SKIP: + /* Charged as in next(). */ + c->buffer->max_ops--; continue; } } @@ -2070,6 +2077,12 @@ j--; } + /* The walk covers every glyph of the ligature, and each match + * attempt repeats it, so it is charged. */ + buffer->max_ops -= (int) (buffer->out_len - j); + if (unlikely (buffer->max_ops < 0)) + buffer->successful = false; + if (found && skippy_iter.may_skip (out[j]) == matcher_t::SKIP_YES) ligbase = LIGBASE_MAY_SKIP; else @@ -2203,7 +2216,8 @@ if (!is_mark_ligature && last_lig_id) { /* Re-adjust components for any marks following. */ - for (unsigned i = buffer->idx; i < buffer->len; ++i) + unsigned i = buffer->idx; + for (; i < buffer->len; ++i) { if (last_lig_id != _hb_glyph_info_get_lig_id (&buffer->info[i])) break; @@ -2215,6 +2229,11 @@ hb_min (this_comp, last_num_components); _hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp); } + /* The marks are rewritten again by the next ligation, so a run of + * ligatures over the same marks is quadratic; charge the walk. */ + buffer->max_ops -= (int) (i - buffer->idx); + if (unlikely (buffer->max_ops < 0)) + buffer->successful = false; } return_trace (true); } @@ -2763,15 +2782,23 @@ for (unsigned int i = 0; i < lookupCount && buffer->successful; i++) { + /* A record that recurses is charged by recurse(); the ones skipped + * here are charged too, as a rule can carry up to 65535 of them. */ unsigned int idx = lookupRecord[i].sequenceIndex; if (idx >= count) + { + if (unlikely (--buffer->max_ops < 0)) buffer->successful = false; continue; + } unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len (); /* This can happen if earlier recursed lookups deleted many entries. */ if (unlikely (c->match_positions.arrayZ[idx] >= orig_len)) + { + if (unlikely (--buffer->max_ops < 0)) buffer->successful = false; continue; + } if (unlikely (!buffer->move_to (c->match_positions.arrayZ[idx]))) break;