Follow-up to #6511. Moving enter()/leave() into HB_SHAPER_EXECUTE
(so hb_shape_plan_execute() gets per-text max_len/max_ops, the
ANT-2026-SUMXXLXH fix) made enter() overwrite the max_len that
test-graphite2-buffer narrows to inject an output-growth failure.
#6511 worked around that in the library: enter() kept any limit
already below the idle default. That is extra logic in every shape
for the sake of one test.
Restore enter() to simply compute the per-text limits, and have the
test call the plan's shaper_func directly in the failing iteration,
so the max_len it sets is not replaced. enter()/leave() stay in
HB_SHAPER_EXECUTE, so hb_shape_plan_execute() remains bounded.
The verify path in hb_shape_full() now runs after leave(); that is
fine: verify only reads the shaped buffer's info/pos and shapes
fresh buffers through hb_shape_full(), each of which gets its own
enter()/leave(). It uses no buffer vars or work limits of the
outer buffer.
Tested: meson test -C build (278/278 pass), and a second build with
-Dgraphite2=enabled (280/280 pass, incl. test-graphite2-buffer).
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
#6501 charged the neighboring-cluster walks in merge_clusters_impl()
and merge_out_clusters_impl() to max_ops by wrapping the extension
and cross-buffer continuation loops in an if (successful) block with
their own charges. These are hot shaping paths, and the restructure
made them harder to follow.
Keep the original loop structure instead: return early once the
buffer has failed (callers such as the Thai shaper keep merging after
that, which is what made the walks quadratic), and move the existing
max_ops charge after the extension loops so that it covers the
extended range plus the cross-buffer continuation. The cluster !=
guards #6501 added to merge_out_clusters_impl(), matching
merge_clusters_impl(), are kept; without them the Thai SARA AM case
walks the merged run on every call. delete_glyph() and
delete_glyphs_inplace() are left as they are.
Testing: U+0E01 + 130000 x U+0E33 shapes in 0.04 s LTR / 0.56 s RTL,
same as current main (3.8 s / 5 s without the guards / early return).
test_shape_overlong_cluster_remerge passes; full meson test passes.
Follow-up to #6501
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
The per-text limits max_len/max_ops are set by hb_buffer_t::enter() and
reset by leave(), but since e246723f0 only hb_shape_full() called them,
so the public hb_shape_plan_execute() ran the shapers with the idle
defaults (0x3FFFFFFF glyphs / 0x1FFFFFFF ops). A client that executes a
plan itself could be made to allocate gigabytes or spend seconds of CPU
on a single character. A reused buffer also kept its spent op budget
across executions, silently skipping nested lookups afterwards.
Move the enter()/leave() pair from hb_shape_full() into the
HB_SHAPER_EXECUTE step of _hb_shape_plan_execute_internal(), the single
point every public path reaches the shapers through, so each call gets a
fresh allowance. It runs after the empty-buffer early return, so the
immutable empty buffer is never written.
enter() now runs on every shape, so guard against discarding a limit
that internal code narrowed below the idle default before shaping (the
output-growth fault injection in src/test-graphite2-buffer.cc sets
max_len that way): compute the per-text limit as before, but keep the
stricter value when the current limit is already below the default.
Public paths always arrive at the idle defaults (from hb_buffer_create()
or leave()), so they get exactly the computed per-text limit as today;
reused buffers are refilled because leave() restores the defaults first.
hb_shape_full()'s VERIFY pass now runs after leave(), which is harmless:
it reshapes fragment copies via hb_shape_full() (which install their own
limits) and does not read max_len/max_ops. No API/ABI change.
A regression test in test/api/test-shape-plan.c asserts that
hb_shape_plan_execute() yields the same buffer length as hb_shape().
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-SUMXXLXH.
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>
Every hb_buffer_set_*() is meant to be a no-op on an immutable
buffer, which is what keeps the static _hb_Null_hb_buffer_t
(returned by hb_buffer_get_empty(), and by hb_buffer_create() on
allocation failure) inert. Three entry points skipped that guard:
- hb_buffer_set_not_found_variation_selector_glyph() stored into
the singleton;
- hb_buffer_guess_segment_properties() wrote direction and
language into it;
- hb_buffer_create_similar() called similar() on the singleton
when its internal hb_buffer_create() failed, copying the source
buffer's settings onto the shared empty buffer and leaking a
reference to its unicode funcs.
Add the same hb_object_is_immutable() early return the sibling
setters use, and guard the create_similar() allocation-failure path
the way hb_font_create_sub_font() already does. No other public
setter in hb-buffer.cc lacks the check. No API/ABI change; the
only behaviour change is that these calls on the empty buffer now
do nothing, as the documented object model promises.
A regression check is appended to test_buffer_empty().
Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-RNK5DQFY.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Buffer reset has retained message callbacks since their introduction,
although its documentation promised a freshly created buffer state.
Clarify that callbacks, their associated data, and attached user data
survive reset. Document explicit callback removal and destruction.
Add a regression test for callback-directed lookup skipping after
clear/reset, restoration of ligatures after removal, and exactly-once
destruction on replacement, removal, and final buffer destruction.
Tested with meson test -C build: 287 passed, 1 skipped, 0 failed.
The API test also compiles without buffer messages or OT shaping.
Fixes: https://github.com/harfbuzz/harfbuzz/issues/6341
Assisted-by: OpenAI Codex
The documentation listed only two retained settings, although
clear_contents also preserves flags, cluster level, invisible and
not-found glyphs, the message callback, and attached user data.
Document these properties and the random-state reset without changing
existing behavior.
Tested with meson test -C build: 287 passed, 1 skipped, 0 failed.
Fixes: https://github.com/harfbuzz/harfbuzz/issues/6340
Assisted-by: OpenAI Codex
move_to()'s two memmoves - the forward copy into out_info and the
rewind copy-back - were not charged against max_ops, while
shift_forward() already charges its moved tail. A morx insertion
subtable that keeps its mark far behind the insertion point can force
an O(distance) rewind plus re-copy per insertion action, for O(N^2)
total work that the max_ops budget never sees.
Charge count in both branches, mirroring shift_forward()'s failure
pattern. Charging only the rewind branch would also bound the
quadratic, but both memmoves are attacker-drivable work, and
symmetric accounting is simpler to reason about.
Fixes https://github.com/harfbuzz/harfbuzz/issues/6190
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Widen position differences before taking their absolute value. This
avoids signed overflow when hb_buffer_diff() compares positions near
opposite ends of the hb_position_t range.
Add an API regression covering INT32_MIN versus INT32_MAX.
Tested with: meson test -C build --print-errorlogs
Assisted-by: OpenAI Codex
Glyph normalization accumulates cluster advances, moves offsets, and
then redistributes the result after sorting. Saturate each hb_position_t
update so very large clusters cannot overflow during normalization.
This is part of the complete position-overflow fix for issue #6151.
Testing: meson test -C build --print-errorlogs
Assisted-by: Codex <codex@openai.com>
* [buffer] Add hb_buffer_changed()
To be called by message callbacks if they modify buffer glyph indices,
to update internal caches (currently the buffer digest).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* [buffer] Implement changed() callback and hook up to OT shaper.
* Only install buffer changed() callback if messaging enabled
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Reject re-entrant calls to hb_buffer_set_message_func() when
called from within a message callback (message_depth > 0).
Previously, calling hb_buffer_set_message_func() from inside a
message callback would destroy the old user_data via the destroy
callback while the current callback still held a reference to it,
leading to a use-after-free.
Fixes: GHSA-h4c3-cvr5-ccq2
* [buffer] Fix a typo and doc string
* [buffer] Add merge_grapheme_clusters / merge_out_grapheme_clusters
Cluster merges serve two distinct purposes that require different level
gates:
- Grapheme grouping: combining marks merged into their base cluster.
Correct gate: IS_GRAPHEMES (levels 0 and 3).
New functions: merge_grapheme_clusters / merge_out_grapheme_clusters.
- Monotonicity maintenance: clusters merged to prevent non-monotone
output after ligature formation, reordering, etc.
Correct gate: IS_MONOTONE (levels 0 and 1).
Existing functions: merge_clusters / merge_out_clusters.
Previously both paths shared merge_clusters_impl's single IS_MONOTONE
gate, so GRAPHEMES (level 3) was incorrectly blocked from merging
grapheme clusters. This also caused a regression in the Thai and
Hangul shapers for MONOTONE_CHARACTERS (level 1): when their explicit
== MONOTONE_GRAPHEMES call-site guards were removed in #5069, the
impls' IS_MONOTONE gate no longer protected them.
Gates are now in the inline wrappers in hb-buffer.hh; the _impl
functions are gate-free. hb_form_clusters, CoreText grapheme
detection, and the Thai/Hangul shapers are updated to call the
grapheme-semantic variants. Fixes#5750.
Fixes https://github.com/harfbuzz/harfbuzz/issues/5751
Fixes https://github.com/harfbuzz/harfbuzz/issues/5750
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* [thai] Adjust grapheme merging logic
* [hangul] Tweak grapheme merging
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
* Remove duplicate buffer->leave call
hb-shape.cc does it for us.
* Fail shaping if max len reached
* Increase buffer limits
https://github.com/harfbuzz/harfrust/issues/107
* Remove shaping_failed; fail in buffer.successful
I'm not sure if the old behavior was intentional, but it was checking
that the glyph flags were a subset of the reference buffer's glyph
flags. I don't see why that is useful. Fix that.
Then make the buffer-verify code ignore flag differences when verifying
buffers, since our unsafe-to-concat flag at least, is conservative and
not guaranteed to be produced the same in fragments. See:
https://github.com/harfbuzz/harfbuzz/issues/3888