Commit Graph
347 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 5.5 5ac43f0a80 [buffer] Restore plain enter() limits; inject the graphite2 fault in the test (#6614)
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>
2026-11-09 20:25:53 -06:00
Behdad EsfahbodandClaude Opus 5.5 0c910506de [buffer] Restore simple loops in merge_clusters_impl() (#6615)
#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>
2026-11-09 18:33:28 -06:00
Behdad Esfahbod 7939d2f295 Merge pull request #6511 from harfbuzz/scanner/misc
[core] Fix four OSS-scanner findings: map UAF, NULL out-params, qsort DoS, shape-plan limits
2026-10-31 22:32:48 -06:00
Behdad EsfahbodandClaude Opus 4.8 02b4120e34 [shape-plan] Install buffer work limits in hb_shape_plan_execute()
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>
2026-10-30 16:49:35 -06:00
Behdad EsfahbodandClaude Opus 4.8 6202af118b [buffer] Charge cluster re-merge walks to max_ops
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>
2026-10-30 15:24:10 -06:00
Behdad EsfahbodandClaude Opus 4.8 6620feeae2 [buffer] Add missing immutable checks to two setters and create_similar
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>
2026-10-30 15:21:14 -06:00
Behdad Esfahbod 199cae8b28 [buffer] Document message callback lifetime across reset (#6354)
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
2026-10-10 16:58:18 -06:00
Behdad Esfahbod 86c92a4735 [buffer] Clarify what clear_contents preserves (#6353)
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
2026-10-10 16:57:40 -06:00
Behdad EsfahbodandClaude Fable 5 cf30f712e7 [buffer] Charge move_to moves against max_ops (#6191)
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>
2026-08-20 18:10:34 -06:00
Behdad Esfahbod 00bfc56167 [buffer] Compare full-range positions safely
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
2026-08-12 18:39:17 -06:00
Behdad Esfahbod c5497821f7 [buffer] Saturate glyph normalization positions
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>
2026-08-12 17:32:30 -06:00
Behdad Esfahbod c34dd6e24b [buffer] Bounds-check in add_utf() (#5864)
Fixes https://github.com/harfbuzz/harfbuzz/security/advisories/GHSA-q4gc-p4hh-3765
2026-03-22 22:49:14 -06:00
Khaled Hosny a0fc099681 13.0.0 2026-03-04 22:28:50 +02:00
Behdad EsfahbodandClaude Opus 4.6 6a1c028a61 [buffer] Add hb_buffer_changed() (#5755)
* [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>
2026-03-04 09:16:54 -07:00
Ramin Farajpour Cami f391d3ea2a [buffer] Disallow hb_buffer_set_message_func during callback (#5766)
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
2026-02-27 08:26:13 -07:00
Behdad Esfahbod 7ffc215001 [buffer] Add a missing immutable check 2026-02-24 04:00:48 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 63fb2eec90 Monotone characters fixups (#5752)
* [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>
2026-02-20 11:25:24 -07:00
Behdad Esfahbod 12ec668212 [buffer] Micro-optimize set_masks
Somehow this specialization loop was in HarfRust / RustyBuzz.
2025-09-02 14:21:57 -06:00
Behdad Esfahbod 598da8c888 [buffer] Add some buffer op accounting to a few methods (#5522)
Should fix a fuzzer-found issue, now that we have increased
most buffer limits.
2025-09-02 14:21:08 -06:00
Behdad Esfahbod 6c42e2d707 [buffer] Consume ops in shift_forward (#5488)
Makes failing infinite-recursion fonts and stuff much faster.
2025-08-21 00:04:56 -06:00
Behdad Esfahbod b51de93600 Remove duplicate buffer->leave call (#5487)
* 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
2025-08-20 23:15:46 -06:00
Behdad Esfahbod 17c875c309 [buffer] Minor add a function 2025-03-28 16:26:27 -06:00
Behdad Esfahbod 7518718671 [buffer] Add HB_BUFFER_CLUSTER_LEVEL_GRAPHEMES
https://github.com/harfbuzz/harfbuzz/discussions/5026#discussioncomment-12168668
2025-03-10 00:52:00 -06:00
Behdad Esfahbod 3e50a8dfec [buffer] Use UINT_MAX instead of wrong macro 2025-02-04 13:06:23 +00:00
Khaled Hosny d5261f7234 10.0.0 2024-09-23 23:07:55 +03:00
Behdad Esfahbod 287046f71a [buffer] Hook up not-found-variation-selector-glyph
Fixes https://github.com/harfbuzz/harfbuzz/issues/4398
2024-09-21 12:21:56 -06:00
Behdad Esfahbod a003890e84 [buffer] Add hb_buffer_[sg]et_not_found_variation_selector_glyph()
Unused.
2024-09-21 12:21:56 -06:00
Khaled Hosny 63973005bc 8.4.0 2024-03-29 16:32:00 +02:00
Behdad Esfahbod f1ac867dee [buffer] Add API for random state
Fixes https://github.com/harfbuzz/harfbuzz/issues/4620

New API:
+hb_buffer_set_random_state()
+hb_buffer_get_random_state()
2024-03-20 14:44:22 -06:00
Bruce Mitchener f380a32825 Fix some typos. 2023-08-25 07:51:05 -06:00
Behdad Esfahbod a41368bd7a [buffer] Minor micro-optimize 2023-07-30 17:04:02 -06:00
Han Seung Min - 한승민 e768754b0f [buffer] fix hb_buffer_t::similar setting replacement to src.invisible
```cpp
void
hb_buffer_t::similar (const hb_buffer_t &src)
{
  hb_unicode_funcs_destroy (unicode);
  unicode = hb_unicode_funcs_reference (src.unicode);
  flags = src.flags;
  cluster_level = src.cluster_level;
  replacement = src.invisible; // <- this should be src.replacement
  invisible = src.invisible;
  not_found = src.not_found;
}
```
2023-04-29 09:58:38 -06:00
Behdad Esfahbod 48f8ed7e02 Docs 2023-04-22 10:14:01 -06:00
Behdad Esfahbod ce6440fceb [buffer] Speed up merge_clusters_impl 2023-02-06 16:12:03 -07:00
Behdad Esfahbod e6bbf112ea [buffer] Better document set_content_type
Fixes https://github.com/harfbuzz/harfbuzz/issues/4000
2023-01-03 12:35:48 -07:00
Behdad Esfahbod f2297e6978 [buffer] Documentation
Fixes https://github.com/harfbuzz/harfbuzz/issues/3889
2022-11-25 11:33:00 -07:00
Behdad Esfahbod 54ae3345b2 [buffer] Improve documentation of hb_buffer_add_codepoints()
Fixes https://github.com/harfbuzz/harfbuzz/issues/3889
2022-11-24 12:00:36 -07:00
Behdad Esfahbod 3b43096ef3 [buffer] Whitespace 2022-11-23 22:04:02 -07:00
Behdad Esfahbod 094f80738a [buffer] Handle null buffer in set_message_func 2022-11-23 22:03:26 -07:00
Behdad Esfahbod 404cb99d86 [buffer-diff] Fix check for glyph flag equality
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
2022-11-23 15:37:03 -07:00
Behdad Esfahbod 59c45f6deb Use hb_memcpy instead of memcpy consistently 2022-11-22 12:54:50 -07:00
Behdad Esfahbod ac0efaf818 Use hb_memset instead of memset consistently 2022-11-22 12:50:36 -07:00
Behdad Esfahbod 4c14043b06 [algs] Add output argument to hb_unsigned_mul_overflows() 2022-11-21 12:56:33 -07:00
Behdad Esfahbod ed43bc5118 [buffer] Move delete_glyphs_inplace() here 2022-11-20 13:10:19 -07:00
Behdad Esfahbod 1945b400da [cpluscplus] Wrap hb-subset types as well
Also changes signature of get_user_data of hb.h types to take const
object. This is safe.
2022-07-25 10:45:55 -06:00
Behdad Esfahbod 0722b627f4 [buffer] Return delta from sync_so_far 2022-07-24 17:35:44 -06:00
Behdad Esfahbod c55c019771 [buffer] Add assertions of buffer sync status to message_impl 2022-07-24 16:30:33 -06:00
Behdad Esfahbod da9edce84c [buffer] Add sync_so_far()
This removes separate out-buffer, at the cost of possibly changing
idx.
2022-07-24 16:30:19 -06:00
Khaled Hosny 98e90cc67c [docs] Reduce warnings
Use markdown syntax for inline code blocks instead of %true, %false, and
%NULL.
2022-06-30 08:44:40 +02:00
Behdad Esfahbod 689c77530a [buffer] Actually remove TODO item
In reality, one side is smaller and one side is larger. The existing code
handles that just fine.
2022-06-10 07:37:45 -06:00