Update the Rust bridge and Cargo dependency to harfrust 0.8.
Switch shaping calls to ShapeOptions, keep the cached shaper
path, and remove an unused face_data argument from the shaping
ABI.
Tested: meson compile -C build
Assisted-by: OpenAI Codex
Cast and add #[allow(clippy::unnecessary_cast)] as the C compiler
chooses different signedness for the hb_script_t and hb_tag_t enum
types depending on architecture. Add casts where needed and add
clippy suppressions to avoid warnings when compiled on systems
where the enum is unsigned by default.
On 32-bit platforms size_t is unsigned int, so the typical
get_size() computation `min_size + count * static_size`
silently wraps when the product exceeds UINT_MAX. The
truncated value then flows into sanitize / serialize as a
small but plausible size, defeating bounds checks.
Add three helpers in hb-algs.hh:
hb_unsigned_mul_saturate (a, b, ...)
hb_unsigned_add_saturate (a, b, ...)
hb_unsigned_mul_add_saturate (a, b, c) /* a*b + c */
They saturate to SIZE_MAX on overflow. Variadic overloads
left-fold so 3+ arguments work. On 64-bit (size_t wider than
unsigned int), all three reduce to plain a*b / a+b — the
inputs to size computations are at most unsigned int so the
result fits in size_t without overflow.
Convert every get_size() that does arithmetic over variable
inputs:
hb-open-type.hh UnsizedArrayOf, ArrayOf,
HeadlessArrayOf, ArrayOfM1,
sorted-array-with-header, CFFIndex
hb-ot-layout-common.hh CmapSubtableFormat-style,
VarRegionList, VarData, MultiVarData,
DeltaSetIndexMapFormat01
hb-ot-hdmx-table.hh DeviceRecord, hdmx
hb-ot-cff-common.hh Charset0, Charset wrapper
hb-ot-cff1-table.hh Encoding, Charset wrapper
(also widened unsigned accumulator
to size_t)
hb-ot-os2-table.hh OS/2 (widened accumulator to size_t)
hb-ot-var-common.hh TupleVariationHeader,
TupleVariationData accumulator
hb-ot-var-hvar-table.hh DeltaSetIndexMap
hb-vector.hh hb_vector_t::get_size
When saturation kicks in, check_range() and extend_size()
naturally reject the resulting value (SIZE_MAX truncated to
unsigned is UINT_MAX, far past any real blob; extend_size
already rejects sizes >= INT_MAX).
Assisted-by: Claude <noreply@anthropic.com>
Bindgen generates a std::os::raw::c_char as return type for
functions like hb_blob_get_data. Depending on platform/arch, this
can be an i8 or u8. In the Rust side code, receiving
the data, these are interpreted as u8. Perform the pointer
casts in a way that compiles without clippy warnings about
unnecessary casts when the c_char type was already u8.
Correct the ArabicPUATraditional.txt entry for 0xF295 which should be
isolated not final form. Also enable 0xFC43 ligature so the ligature is
actually used.
Fixes https://github.com/harfbuzz/harfbuzz/issues/5984
When the sum old_len + out_len (or old_len + l) exceeded INT_MAX,
the (int) cast turned it negative. resize_full() treated negative
size as zero, alloc(0) succeeded, and the subsequent write loop
overflowed the buffer.
Fix both layers:
- resize_full(): reject negative size_ (return false) instead of
silently treating it as zero. This is defense-in-depth for every
caller that casts unsigned to int.
- append_len() and append_base64(): use hb_unsigned_add_overflows()
to detect unsigned wraparound before the cast. This catches the
case where old_len + new_bytes > UINT_MAX wraps to a small
positive value that would pass the negative check.
Assisted-by: Claude <noreply@anthropic.com>
In embedding projects, the global_allocator might already have been
overriden. There is no known way to keep the allocator library-local,
and allocator-bound containers are not a stable Rust feature yet.
Add an option to disable the allocator mapping to hb_malloc and friends,
so that an embedder can keep their own allocator.
The std::sort revert (d3a1de25e3) inadvertently inverted the
post-table glyph-name sort. hb_array_t::cmp has reversed
argument order — x.cmp(y) returns the sign of (y - x), not
(x - y) — so dropping the `> 0` from the bool-returning
lambda turned ascending into descending order.
On Linux the quicksort happened to leave the array in an
order that bsearch could still find names; on macOS, where
the system sort behaves differently, this exposed the bug.
Pass (b, a) to find_glyph_name(...).cmp(...) to recover the
intended ascending order.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Restructure hb_qsort_inline so the recursive partitioning loop
stops at subranges of size <= threshold without sorting them,
and the entry point runs one insertion sort over the entire
array at the end. After the partitioning loop, every element
is within the threshold of its sorted position, so the final
pass is O(n * threshold) — cache-friendlier than a per-leaf
insertion sort, which is what libstdc++'s std::sort does too.
Also bump the threshold from 16 to 32, which the deferred
scheme prefers (per-leaf insertion sort prefers 16; the
deferred form amortizes a larger threshold better).
Bench (NotoSansCJK GPU encode, smaller is better):
std::sort baseline: 570 ms
per-leaf insertion sort, threshold 16 (parent): 613 ms
per-leaf insertion sort, threshold 32: 623 ms
deferred insertion sort, threshold 16: 620 ms
deferred insertion sort, threshold 32 (this): 610 ms
deferred insertion sort, threshold 64: 610 ms
deferred insertion sort, threshold 96: 620 ms
Tested: 240/240 tests pass; nm -D libharfbuzz.so shows zero
remaining _ZSt sort weak symbols.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Rewrite the internal qsort used by hb_array_t::qsort(Compar) as a
typed template hb_qsort_inline<T, Compar>:
* Operates on T* directly, so swap is T-typed (one move per
half-element instead of a per-byte loop). hb_swap is used,
which uses ADL std::swap.
* Two-way Hoare partition instead of three-way Bentley-McIlroy.
The previous three-way scheme paid extra branch overhead in
the inner partition loop without payoff on the typical
HarfBuzz workload (sorting unique curve indices, glyph IDs,
feature tags, etc).
* Median-of-three pivot, parked at last-1.
* Tail-call elimination on the larger partition; recurse only
on the smaller. Bounds stack depth at O(log n).
* Insertion-sort threshold bumped from 10 to 16, matching
libstdc++'s std::sort.
The byte-based sort_r_simple (and helpers SORT_R_SWAP, sort_r_swap,
sort_r_cmpswap, sort_r_swap_blocks) is removed; nothing else used
it. hb_qsort still forwards to system qsort.
Bench (NotoSansCJK GPU encode, smaller is better):
std::sort baseline: 570 ms
byte-based sort_r_simple (step 1): 650 ms (+14%)
three-way typed quicksort: 640 ms
two-way typed quicksort (this): 613 ms (+7.5% vs baseline)
ABI: zero std:: weak symbols leak from libharfbuzz.so, same as
step 1.
Tested: 240/240 tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
std::sort instantiates several internal STL helpers
(__introsort_loop, __insertion_sort, __adjust_heap, __heap_select,
__unguarded_linear_insert) as weak symbols in the std:: namespace.
These leaked into libharfbuzz.so for every distinct (T, Compar)
pair, polluting the public ABI surface.
Route hb_array_t::qsort(Compar) through our existing templated
sort_r_simple instead. The comparator is still inlined into the
sort body (sort_r_simple has been comparator-templated since
05762b2fac).
Drop the bool-returning comparator overload in favor of a single
int-sign convention; convert the call sites added during the
std::sort migration back to int-sign comparators.
Step 1 of two: this restores ABI hygiene at the cost of typed-
element swap (sort_r_simple swaps byte-by-byte). A follow-up
will introduce a typed sort to recover that perf.
Tested: 240/240 tests pass; nm -D libharfbuzz.so shows zero
remaining _ZSt sort weak symbols.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* [fontations] Fix OOB write on glyph-name callback with size == 0
The Rust `_hb_fontations_glyph_name()` callback computed
`glyph_name.len().min(size as usize - 1)`. When a caller passed
`size == 0`, `size as usize - 1` underflowed a `usize` to
`usize::MAX`, the `min()` clamp collapsed to `glyph_name.len()`,
and the subsequent `copy_nonoverlapping` / trailing NUL wrote
`glyph_name.len() + 1` bytes into a zero-capacity caller buffer.
This is an out-of-bounds write reachable from the public API
whenever the build was configured with `-Dfontations=enabled`
and the application had called `hb_fontations_font_set_funcs()`.
Short-circuit the `size == 0` case before the subtraction.
Return `true` in that case to match the existing post and CFF1
backends (`hb-ot-post-table.hh`, `hb-ot-cff1-table.hh`), which
both early-out with `return true` on `!buf_len` — the glyph
does have a name; we simply had no buffer to write it into.
Regression tests added in `test/api/test-glyph-names.c` cover
the zero-size path on post and CFF1 backends, a sub-font, a
NULL buffer, and `size == 1` / small-buffer / exact-fit cases
so the truncation path does not silently regress.
Fixes: https://github.com/harfbuzz/harfbuzz/issues/5946
Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* [font] Reject glyph-name lookups with size == 0 at the wrapper
`hb_font_t::get_glyph_name()` previously pre-wrote the trailing
NUL only when `size != 0` but still dispatched to the backend
callback with `size == 0`. Every current glyph-name backend has
to re-validate that argument: `post` and `cff1` short-circuit
with `return true`, the new Rust `fontations` callback now
short-circuits too (prior commit), and the CoreText callback in
`hb-coretext-font.cc` has a structurally similar `(CFIndex)size
- 1` pattern.
Normalize this at the wrapper layer: return `false` immediately
on `size == 0`, before any backend dispatch and before the
`*name = '\0'` write. This makes the zero-size case a single,
auditable no-op for every glyph-name provider (native or Rust,
current or future) and removes the CoreText underflow from the
reachable surface through the same chokepoint.
Observable change: callers that invoked
`hb_font_get_glyph_name(font, gid, buf, 0)` on post/cff1 backed
fonts used to get `true` back; they now get `false`. `size == 0`
queries carry no useful information for the caller, so this is
considered a safe hardening rather than a contract break.
Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* [font] Preserve glyph-name zero-size probe semantics
Keep `hb_font_get_glyph_name(..., NULL, 0)` as a valid
return-value-only probe instead of rejecting `size == 0` in the
wrapper.
Fix the remaining backend-specific zero-size paths in hb-ft and
hb-coretext-font so they answer the probe without dereferencing the
caller buffer, and update the API tests to assert the preserved
behavior.
Testing: CCACHE_DISABLE=1 meson test -C build test-glyph-names
Testing: CCACHE_DISABLE=1 meson test -C build test-ft
Assisted-by: OpenAI Codex <codex@openai.com>
---------
Co-authored-by: jeffhuang <jeff@docker.xydrsucermoubd24xgo33yhsgd.bx.internal.cloudapp.net>
push_clip_glyph kept a hash + linear-scan cache of emitted
clip paths to dedup repeated clip-glyph uses within a single
COLRv1 paint tree. With paint contexts being one-glyph per
use, the cache mostly churned; it also made hb_memcmp trip
on sanitizer-null when an empty path matched an equally-empty
existing entry (fuzzer-reached null-deref).
Drop the cache: always emit a fresh <path> + <clipPath> pair
per push_clip_glyph. Removes defined_paths / content_entry_t
from the paint struct.
Regenerate the two handwriting COLR expected SVGs whose
clip-reuse now produces distinct path defs.
Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Accept caller-supplied extents in any orientation and
normalize to min-corner origin with positive width/height.
Lets callers use the glyph-extents-style {top, -height} form
matching hb_glyph_extents_t convention, and brings set_extents
in line with the Y-up internal model the paint/draw contexts
now use.
Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Exposes the path separation as a public function so callers
using the draw funcs directly can separate glyph outlines.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove the initial base_transform push from ensure_initialized.
Instead, _glyph_impl pushes/pops the positioning transform via
the paint funcs, matching the vector paint model.
current_transform() returns identity when the stack is empty.
pop_transform allows popping to empty (for the final glyph-level
pop).
This makes changing set_transform between glyph calls work
correctly — the transform is applied fresh for each glyph.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Each _glyph_or_fail function now documents what it is
equivalent to in terms of the underlying font draw/paint
API + the context's own funcs.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove hb_pdf_paint_ensure_initialized — it was identical to
paint->ensure_initialized(). All PDF callbacks now call the
common method. Remove redundant call from _glyph_impl since
paint callbacks handle it.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
SVG render now wraps all body content in a single
<g transform="scale(1,-1)">. All coordinates inside are
in Y-up (font) space. The viewBox maps the flipped range.
This eliminates:
- Per-path scale(1,-1) wrappers in draw flush_path_svg
- Per-glyph Y-flip in paint _glyph_impl's push_transform
- The {-xy, -yy, -ty} negation in extents transforms
- Format switch in paint _glyph_impl
paint _glyph_impl now passes the raw transform to
push_transform — no format knowledge needed.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove content-based color glyph dedup from paint — no more
<defs>/<use> for color glyphs. Each glyph's paint output is
emitted inline, wrapped by push_transform/pop_transform from
the paint funcs (which emit <g transform="..."> for SVG and
q cm ... Q for PDF).
_glyph_impl is now format-agnostic: extend extents,
push_transform, paint, pop_transform.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>