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>
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>
After the March refactor that made clear() a direct
shrink_vector(0), reset() going through resize(0) adds an
indirection (resize_full -> alloc(0) no-op -> shrink_vector(0))
for identical observable behavior. Use clear() directly.
Assisted-by: Claude <noreply@anthropic.com>
* [subset] Return size_t rather than unsigned from get_size() methods
It's possible some get_size() computations during serialization
may overflow 32-bit arithmetic.
(The result of get_size() is used as size_t when allocating via
hb_serialize_context_t::extend(), but if the return value was
declared as unsigned int, the value may have wrapped resulting
in under-allocation.)
* [subset] Use size_t when accumulating subset_data_size
* [graph] Use size_t (rather than signed int64_t) for vertex_len variables
* Use size_t for sanitize_closure_t::stride to avoid narrowing
Failing-malloc fuzzing exposed two holes in the GPU draw path.
Accumulation ignored push failures, and encode reused scratch vectors
that could remain poisoned after a previous allocation failure.
Poison the draw object on accumulator OOM, bail out of the remaining
path-building callbacks, and have hb_gpu_draw_encode() return NULL once
the object has failed. Also check the encode scratch resizes and reset
poisoned vectors before reuse. Resetting the draw now fully resets the
curve vector as well, so callers can recover after an OOM by calling
hb_gpu_draw_reset().
Testing:
- meson test -C build --suite fuzzing --print-errorlogs
- meson test -C build
Co-authored-by: OpenAI Codex <codex@openai.com>
Reduce to one templated hb_qsort, one templated qsort() on
hb_array_t/hb_vector_t, plus the no-arg qsort() that casts
Type::cmp to resolve ambiguity.
Convert all external call sites to lambdas so comparators
inline everywhere. Remove the void*-arg overload and the
plain function-pointer overload.
225 tests pass, 0 failures.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Make sort_r_simple and sort_r_cmpswap take the comparator as a
template parameter instead of a function pointer. This allows
lambdas and functors to inline into the sort, avoiding the
indirect-call overhead.
The function-pointer overloads remain for backward compatibility
and for the __OPTIMIZE_SIZE__ / qsort_r paths.
17% speedup on GPU encode benchmark (NotoSansCJK).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add an hb_array_t::qsort overload that takes a comparator with a
void* context argument, forwarding to hb_qsort's existing qsort_r
support. Use it in the GPU encoder to eliminate the last std::
usage from library code.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Also simplify clear() itself to just call shrink_vector(0) directly,
avoiding the resize() -> alloc() path which both fails in error state
and requires Type to be default-constructible.
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The previous fix (d134f888) zeroed `length` but skipped element
destructors. Call shrink_vector(0) instead, which properly destructs
items before clearing length.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
clear() called resize(0) which calls alloc(0), but alloc() bails
immediately when the vector is in error state, so length was never
zeroed. Under failing-malloc, a vector could enter error state from
a failed push, making all subsequent clear() calls silently fail,
leaving stale pointers.
This caused a heap-use-after-free in hb_raster_paint_destroy():
render() released surfaces to surface_cache then called
surface_stack.clear() which silently failed, so destroy() iterated
the stale surface_stack pointers and double-freed images already
in surface_cache.
Fixes https://oss-fuzz.com/testcase?key=5078588000108544
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add bool exact=false parameter to all extend() overloads. When true,
the allocation is exact (no growth headroom), useful for copy
constructors. Pass exact=true from the Iterable copy constructor.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
extend(Iterable) called alloc(hb_len(iter)) which only allocated
space for the new elements, ignoring existing length. When the
vector already had content, push_has_room would write past the
allocated buffer.
Fix: alloc(length + hb_len(iter)).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Reduce allocation of match_positions by caching one in ctx
* Use a stack-array for small match-positions
This surgically enables stack-backed storage in vector.
* Handle match-positions alloc failure
And make allocations unchecked.
* Clean up CFF hack to use static storage in vector
* Fixup for match_positions allocations
* Fix vector handling of foreign arrays memory management
Fix sites that were doing this under our feet.
This lowers precision of some instancing operations from double
to float, which modifies a few results by rounding error.
TODO: Update tests.
Building with clang complains about the use of `tuple_variations_t`'s
implicit copy constructor and copy assignment operator, since automatic
generation of these is deprecated when declaring a non-default
destructor. This is a good warning because it isn't obvious that copies
were being made instead of the object being moved and this struct should
be moved and not copied. Declare all the special member functions,
defaulting the moves and deleting the copies.
After making `tuple_variations_t` move only, an issue is revealed in
`hb_vector_t::push` which has been requiring that objects be copyable.
Remove the old non-emplacing `push` now that this works with all
existing objects and make a single `push` which is more like
`std::vector::emplace_back` since that is somewhat what the newer `push`
is attempting to do.