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.
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>