Lookups can share subtables, features can share lookups, and each
shaping stage applies its lookups anew, so the number of passes over
the buffer is not bounded by the font size. An earlier lookup can
also grow the buffer far beyond the text the budget was sized from:
a font that grows one character into a long buffer and then walks it
with thousands of lookups (as in harfbuzz/harfrust#571) made hb_shape()
spend CPU out of proportion to the text, and none of it was charged
to buffer->max_ops.
Charge each pass in apply_string() len / 16 for walking the buffer,
even if it applies at no glyph (skipping a glyph is cheap, hence the
division; applying at one is charged only through the buffer
operations and nested lookups it does), and stop the stage loop in
hb_ot_map_t::apply() once the buffer has failed. Nothing is charged
in the matching hot loops (skipping iterator, RuleSet, ChainRuleSet,
LigatureSet): charging there cost real fonts most of the budget in
HarfRust (Gulzar spent about half of max_ops on ordinary Urdu prose,
Noto Sans Duployan ran out on its own sample text). Aliased rules and
ligatures stay bounded by the sanitizer, including the ChainRule array
charge. Matches harfbuzz/harfrust#601.
test_shape_lookup_budget grows one 'a' to 65025 glyphs before applying
aliased lookups, and checks that the pass charge stops the shape.
With the font from harfbuzz/harfrust#571 (one 'A' grown to 30,000
glyphs, then 2,000 lookups), hb_shape() takes about 10 ms instead of
0.6 s, stopping after about 35 passes with the glyphs kept. Gulzar
and Noto Nastaliq Urdu spend under 0.5% of max_ops on Urdu and Persian
prose. Noto Sans Duployan spends up to 45% on its sample text, as on
main: nearly all of it is the _infos_set_glyph_flags() charge from
#5522; the per-pass charge adds about 5%.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6A9E6OEX.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-K2T2LOJH.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-DVG5XAU0.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Anchor coordinates are scaled as float, and apply_cursive_attachment()
and the mark attachment code converted them to hb_position_t with a
plain cast or an implicit conversion. A value out of range for the
target type makes that conversion undefined; on x86 it gives INT32_MIN.
#6154 made the additions around these conversions saturate, but not
the conversions themselves.
With NotoNastaliqUrdu-Regular.ttf at a scale of INT32_MAX, a cursive
anchor scales to 2.23e9, and the glyph ended up with an x_advance of
-2147483648.
Convert with hb_clamp_to(), and negate the cursive cross-direction
offsets with hb_saturate_neg(), since they can now be INT32_MIN.
Results that fit do not change.
Tested with test-shape under UBSan: the new test fails on the base
commit at CursivePosFormat1.hh:141 and passes here. No test reaches
the two conversions in MarkArray.hh; they are changed to match.
Assisted-by: Claude
A kerx format-4 control-point action asks the font for two glyph
contour points, which the font functions may satisfy by loading the
whole glyph outline (two full FT_Load_Glyph calls with hb-ft). The
state-machine driver charges a non-advancing transition only one op,
so a looping entry turned the whole work budget into glyph loads:
two letters and a 680-byte font stalled hb_shape() for seconds.
Charge 2 * HB_BUDGET_256 to buffer->max_ops at the action, before the
callbacks, using the same decrement-and-return idiom morx insertion
uses; once the budget is spent the action is skipped and the driver's
existing max_ops check forces the machine forward.
The sibling GPOS AnchorFormat2 contour-point path has the same
uncharged callback but lives in the layout subsystem and is left for
a separate change. GDEF CaretValueFormat2 also calls it but only
from hb_ot_layout_get_ligature_carets, bounded per API call.
Regression test in test/api/test-shape.c uses a counting
glyph_contour_point callback on a builder-made looping kerx font
(131074 calls before, <= 256 after).
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EE451WE1.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
HarfRust signals a shaping failure -- a max_ops / preprocessing-budget trip, an allocation failure, or an internal error -- by returning false from its shape function, whereas the ot shaper always returns true and reports such failures through the buffer's success flag.
Because hb_shape() returns void, hb_buffer_allocation_successful() is the only failure signal its callers have. The HarfRust shim returned false without touching the C hb_buffer_t, so after a HarfRust budget trip hb_buffer_allocation_successful() still reported success while the buffer was left unshaped.
Mirror the ot shaper in hb-harfrust.cc: always return true and, on failure, mark the buffer unsuccessful. As with the ot shaper after a budget trip, the buffer is then labelled GLYPHS but its contents must not be trusted; the caller must check hb_buffer_allocation_successful().
Add a test-shape regression test that drives the HarfRust shaper with many ranged features (overrunning setup_masks()'s op budget) and asserts the failure now reaches hb_buffer_allocation_successful(). It runs in the test-shape-harfrust CI job and skips when HarfRust is not built.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
test_shape_overlong_cluster_remerge checks the native cluster re-merge op
budget added in hb-buffer.cc, which only the "ot" shaper exercises. Under
HB_SHAPER_LIST=harfrust the harfrust shaper handles clusters itself and
never trips that budget, so the buffer stayed allocation-successful and the
test aborted in the rust CI job. Pin the test to hb_shape_full() with an
"ot" shaper list.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The work budget (max_ops) is meant to bound every buffer pass whose
length the font or text controls, but the same-cluster walks that
re-merge a neighbouring run were never charged:
- the backward merge in delete_glyph() (reached from an empty
MultipleSubst Sequence);
- its copy in delete_glyphs_inplace() (default-ignorable / AAT
glyph deletion);
- the "extend" and cross-buffer continuation loops in
merge_clusters_impl() / merge_out_clusters_impl() (reached from
the Thai/Lao shaper, the normalizer, morx ligatures, and any
ligature/multiple substitution).
Once one long same-cluster run exists, every further merge or
deletion next to it walks the whole run again, so a single hb_shape()
call does O(N^2) work the budget never sees: e.g. U+0E01 followed by
130,000 x U+0E33 takes ~21 s.
Charge the glyphs actually walked to max_ops and fail the buffer when
the budget is gone, following shift_forward()/move_to(), and skip the
walk once the buffer has failed (delete_glyphs_inplace() and the
shaper-level merge_clusters() callers keep calling after a failure).
merge_out_clusters_impl() also gains the "cluster != ..." guards that
merge_clusters_impl() already had, so it no longer walks a run that
already holds the merged value; set_cluster() is a no-op on such a
glyph, so output is unchanged. Verified: default-direction Thai
output is byte-identical to before at sizes that fit the budget, and
pathological inputs now end with hb_buffer_allocation_successful() ==
false, like any other max_ops exhaustion.
Regression test added to test/api/test-shape.c using a stock font.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CQ04GDSH.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Mark-to-mark attachment can accumulate an unbounded cross-direction
offset. Clamp anchor conversion and saturate the immediate addition so
overlong mark clusters stop at hb_position_t limits instead of invoking
signed-overflow undefined behavior.
Add a regression for issue #6151 using 35,000 combining marks.
Testing: meson test -C build --print-errorlogs
Assisted-by: Codex <codex@openai.com>
Based on test from https://code.google.com/p/chromium/issues/detail?id=497578
Currently fails. Basically, if there's a default_ignorable at the
start of text, and font has no space glyph, we remove the default_ignorable,
and that makes the first char in text to correspond to no cluster.
Fix coming.