The vector work budget charged both outline traversal and serialized
output bytes, and left embedded bitmaps entirely uncharged -- so a COLR
walk with many sweep-gradient or image layers could emit output that the
outline budget never sees, bounded only by the structural per-node caps
times the node count.
Bound the output with a flat document-size cap instead: hb_vector_buf_t
refuses to grow past HB_VECTOR_MAX_DOCUMENT_SIZE (16 MB) and folds that
into in_error(), so every emission site that already checks it -- path
commands, gradient meshes -- stops cleanly, and image embedding is bounded
for the first time. clear() resets the sticky flag so a context reused
across renders (the documented clear -> paint -> render loop) recovers
after one over-cap glyph. vector is one glyph per context, so a flat cap
suffices; SVG's whole document and PDF's (assembled through the same
hb_vector_buf_t) share it.
With output bounded structurally, the work budget goes back to pure
outline traversal: drop the per-command byte charge and the sweep-patch
charge so it no longer conflates two different resources. It is still
needed on its own -- a CFF charstring can do a lot of work while emitting
little output.
Once the cap trips, remaining appends are dropped, so a truncated document
is malformed; render returns it (or null) rather than looping to build
gigabytes, which is the point.
Add a regression test that paints an oversized image and confirms the
document stays bounded and the context recovers on reuse; it fails if the
cap is removed.
Tests: meson test -C build (275/275 passed); the vector COLR regression
seed completes in 0.85s.
Assisted-by: Claude <noreply@anthropic.com>
hb_vector_buf_t::append_num() formatted every coordinate with two
snprintf calls (one just to build the "%.Nf" format string) plus
strstr/strchr/strlen for the locale decimal-point fix-up and the
trailing-zero strip. On oss-fuzz's i386 ASAN builds those
intercepted libc calls run ~60-100x slower than native, so a COLR
paint session that emits its full 16MB work budget of path text
(~2M numbers) blew the 60s fuzzer timeout; locally the same input
spent 75% of its time inside glibc __printf_fp.
Replace the common case with fixed-point integer formatting: scale
by 10^p, round half-to-even, and print the digits with integer
math. A float has a 24-bit significand and 5^12 adds 28 more bits,
so v*10^p is exact in a double for p <= 12; the result therefore
matches snprintf "%.pf" digit-for-digit, verified against the old
code on 40M random and targeted inputs (ties, denormals, FLT_MAX,
NaN/Inf, negative zero, all precisions). Magnitudes that do not
fit int64 fall back to the old snprintf path.
The timeout input drops from 0.84s to 0.17s locally, and the
remaining profile is HarfBuzz code rather than intercepted libc.
check-symbols, the vector suite, and the full test suite pass with
byte-identical outputs.
https://oss-fuzz.com/testcase-detail/4700345193005056
Assisted-by: Claude Fable 5 <noreply@anthropic.com>
hb_vector_buf_t::append_base64() sized its output as
unsigned out_len = ((len + 2) / 3) * 4;
which overflows 32-bit for a very large len (len + 2 wraps, and the * 4
wraps for len above ~3.2 GB). len is the attacker-controlled embedded
PNG length on the vector SVG paint path (hb-vector-paint-svg.cc), so a
wrapped out_len under-sizes the buffer while the encode loop still writes
4 bytes per 3 input bytes -- a heap out-of-bounds write.
Compute the group count without the +2 (len / 3 + (len % 3 != 0)) and use
hb_unsigned_mul_overflows()/hb_unsigned_add_overflows() so an oversized
input fails the allocation instead of wrapping. resize_full() already
rejects a size that does not fit in int, so the remaining path is safe.
Assisted-by: Claude Opus 4.8 (1M context) <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>
Replace hb_vector_scale_precision() free function with
hb_buf_t::scale_precision() method.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>