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.
Add focused API tests for hb_buffer_create_similar() and buffer
configuration persistence across clear/reset. Also exercise public hb_map
copy, equality, hash, update, iteration, keys, and values paths with
order-independent assertions.
Validation:
meson test -C build-tgl-precheck --print-errorlogs --suite api test-buffer
meson test -C build-tgl-precheck --print-errorlogs --suite api test-map
meson test -C build-tgl-precheck --print-errorlogs --suite api test-buffer test-map
Assisted-by: OpenAI Codex <codex@openai.com>
Co-authored-by: jeffhuang <jeff@docker.xydrsucermoubd24xgo33yhsgd.bx.internal.cloudapp.net>
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>