Commit Graph
207 Commits
Author SHA1 Message Date
Behdad Esfahbod f3c6b3ab92 [algs] Saturating arithmetic for get_size() on 32-bit (#5988)
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>
2026-05-18 15:11:34 -06:00
Behdad Esfahbod cf9c3eb36e [vector] Fix heap buffer overflow in append_base64 / append_len
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>
2026-05-16 13:06:40 +01:00
Behdad Esfahbod fe04146acb [vector] Simplify reset() to use clear() instead of resize(0)
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>
2026-04-14 22:00:32 -06:00
jfkthame 603ab95602 Return size_t rather than unsigned int from get_size() (#5915)
* [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
2026-04-13 11:01:19 -06:00
Behdad EsfahbodandOpenAI Codex c111ff82ff [gpu] Fail cleanly on OOM in draw encode
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>
2026-04-03 16:29:27 -06:00
Behdad EsfahbodandClaude Opus 4.6 df433afe6c [algs] Simplify hb_qsort: single template, inline all callers
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>
2026-03-27 12:50:30 -06:00
Behdad EsfahbodandClaude Opus 4.6 05762b2fac [algs] Template hb_qsort comparator for inlining
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>
2026-03-27 12:36:59 -06:00
Behdad EsfahbodandClaude Opus 4.6 33ad6419e3 [gpu] Remove std::sort; add qsort with context to hb_array_t
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>
2026-03-26 19:48:10 -06:00
Behdad EsfahbodandClaude Opus 4.6 e6d8ad97dc [vector] Use clear() instead of resize(0)/shrink(0) (#5876)
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>
2026-03-26 15:15:56 -06:00
Behdad EsfahbodandClaude Opus 4.6 41de3c3272 [vector] Destruct items in clear() when vector is in error state
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>
2026-03-26 14:19:41 -06:00
Behdad EsfahbodandClaude Opus 4.6 d134f88824 [vector] Fix clear() when vector is in error state
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>
2026-03-23 20:26:18 -06:00
Behdad Esfahbod 2329724c1e Fix hb_vector push failure checks 2026-03-09 23:08:02 -03:00
Behdad Esfahbod 3c099ee71c Add initial libharfbuzz-vector SVG backend and utility rename 2026-02-24 08:52:34 -03:00
Behdad EsfahbodandClaude Opus 4.6 a1d9c296d8 hb_vector_t::extend: add exact parameter for tight allocation
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>
2026-02-21 17:26:46 -03:00
Behdad EsfahbodandClaude Opus 4.6 65270b19e8 Fix hb_vector_t::extend() buffer overflow for non-empty vectors
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>
2026-02-21 17:26:32 -03:00
Bastien Dejean 8f5abdc42f GCC 4.9.4: Keep template names unique (#5713)
Fixes #5712.
2026-01-10 05:02:17 -07:00
Garret Rieger 09c14fa4f2 Fix fuzzer found memory leak. (#5643)
Fixes https://oss-fuzz.com/testcase-detail/6083012753424384
2025-11-14 02:27:35 -07:00
Behdad Esfahbod e1fd2beeec [alloc-pool] Reduce memory usage by reclaiming some discards 2025-09-18 11:18:09 -04:00
Behdad Esfahbod a22add2530 [alloc-pool] Implement alignment 2025-09-18 11:04:06 -04:00
Behdad Esfahbod 5b04b40dd5 [instancer/tuplevars] Allocate compiled_deltas from alloc pool 2025-09-16 16:52:53 -06:00
Behdad Esfahbod bdac0aec2e [vector] Make shrink() useful in un-owned vectors 2025-09-16 16:42:32 -06:00
Behdad Esfahbod c2f2121527 [vector] Simplify allocate_from_pool / set_storage 2025-09-16 16:29:23 -06:00
Behdad Esfahbod fb0ef230c9 Revert "[tuplevars] Allocate from pool"
This reverts commit 94d78de954.

Slowed things down.
2025-09-16 11:16:30 -06:00
Behdad Esfahbod 94d78de954 [tuplevars] Allocate from pool 2025-09-16 11:13:19 -06:00
Behdad Esfahbod f5fdf42615 [tuplevar] Use pool for duplicating 2025-09-16 10:45:33 -06:00
Behdad Esfahbod 755ed10915 Add hb-alloc-pool and use in gvar instancer 2025-09-16 00:43:29 -06:00
Behdad Esfahbod 8237091363 [vector] Simplify resize() 2025-09-15 20:52:05 -06:00
Behdad Esfahbod 5e816ef7ae [iup] Reuse vectors 2025-09-15 18:54:10 -06:00
Behdad Esfahbod db86e546ab [instancer/gvar] Combine peak_coords mallocs into one chunk 2025-09-15 16:46:32 -06:00
Behdad Esfahbod 26cc735551 [vector] Remove a comment 2025-09-15 15:49:36 -06:00
Behdad Esfahbod 740cbf1c81 [vector] Add malloc debug facility 2025-09-11 15:39:15 -06:00
Behdad Esfahbod 04953dca8d [vector] Enable set_storage only on trivial types (#5493) 2025-08-21 23:00:37 -06:00
jfkthame c1a75d36c7 Avoid UB if shrink_vector(0) is called on an already-empty vector (#5491)
Fixes #5489
2025-08-21 10:38:04 -06:00
Behdad Esfahbod 2254574384 Match positions no malloc (#5481)
* 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
2025-08-20 00:06:00 -06:00
Behdad Esfahbod 5f61ccf07d [set] Fix reallocation
Was shrinking malloced vectors inadverently.
2025-03-13 13:22:28 -06:00
Behdad Esfahbod ad0a5c93b5 [vector] Add fast path in extend() for length-known iterators 2025-03-01 16:02:10 -07:00
Behdad Esfahbod a037d3815f [vector] Fix operator = 2025-02-26 12:05:12 -07:00
Behdad Esfahbod 0cd98ebbf7 [vector] Add a missing fast-path 2025-02-25 18:53:04 -07:00
Behdad Esfahbod 41626401b0 [vector] Add faster extend() for array types 2025-02-25 18:48:34 -07:00
Behdad Esfahbod aaf5c06d62 [vector] Rewrite copy_array() to extend vector 2025-02-25 18:45:38 -07:00
Behdad Esfahbod e450552d07 [coord-setter] Fix memory access in case of malloc failure
Fixes https://oss-fuzz.com/testcase-detail/5383702943432704
2025-02-25 14:54:37 -07:00
Behdad Esfahbod aaecaa4c0e [vector] Disallow accidental creation of transient vectors
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.
2025-02-25 19:02:11 +00:00
Behdad Esfahbod 13ee8edf06 [vector] Speed up extend() 2025-02-24 21:17:58 -07:00
Behdad Esfahbod 46485124ea [vector] Simplify a method 2025-02-24 21:04:34 -07:00
Behdad Esfahbod 76c3beaf36 [VARC] Reduce malloc overhead 2025-02-24 19:50:51 -07:00
Behdad Esfahbod d2a88fbd7b [vector] Add alloc_exact() 2025-02-12 21:52:01 -05:00
Bruce Mitchener 3cfdbd6717 Use noexcept on swap, move constructors, etc. 2024-02-17 11:04:10 +01:00
Behdad Esfahbod 29d49eced8 [vector] Introduce realloc_moves tag
Some types (even aggregate) can be moved using realloc().
Extend the fast path to hb_hashmap and tuple_delta_t.
2023-11-01 15:55:47 -06:00
inobelar 69da5aae02 Added minor fixes to build without errors with gcc 4.9.2 2023-09-23 11:54:01 -06:00
Ben Wagner 4cfc6d8e17 Specify tuple_variations_t special member functions
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.
2023-08-31 22:49:42 +03:00