From d73f4eea07bd8683608ebf6c83e58d1d449693a9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 14:53:31 -0600 Subject: [PATCH] [instancer] Treat a zero-size pool allocation as success in gvar instancing hb_alloc_pool_t::alloc(0) legitimately returns nullptr while no shared chunk is open, but hb_vector_t::allocate_from_pool() took any nullptr as an allocation failure and flagged the (empty) vector with set_error(). The next allocate_from_pool() on it then tripped assert(!length && !allocated), aborting hb-subset during partial instancing of a crafted ~1 KB variable font whose retained glyph has more than 16,376 points (so no small request ever opens the shared chunk) and whose tuple is split by the requested axis range. Fix the root cause where the vector already special-cases zero-size requests on the heap path: a null result is a failure only when bytes were requested (size && !arrayZ). tuple_delta_t::copy_from() also discarded the results of its seven duplicate_vector_from_pool() calls, so a genuine pool failure during a tuple clone would reach the same assert (or silently drop gvar under NDEBUG); it now returns bool, change_tuple_var_axis_limit() marks out in error, and change_tuple_variations_axis_limits() fails instead of continuing. A regression test for the zero-size pool allocation is added to src/test-vector.cc. The attack font now instances to the same output as the control; test-vector and the subset suite pass. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-N0R68QRI. Co-Authored-By: Claude Opus 4.8 --- src/hb-ot-var-common.hh | 26 +++++++++++++++++--------- src/hb-vector.hh | 3 ++- src/test-vector.cc | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index d12a582d0..c55c272f0 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -320,16 +320,17 @@ struct tuple_delta_t return *this; } - void copy_from (const tuple_delta_t& o, hb_alloc_pool_t *pool = nullptr) + bool copy_from (const tuple_delta_t& o, hb_alloc_pool_t *pool = nullptr) { axis_tuples = o.axis_tuples; - indices.duplicate_vector_from_pool (pool, o.indices); - deltas_x.duplicate_vector_from_pool (pool, o.deltas_x); - deltas_y.duplicate_vector_from_pool (pool, o.deltas_y); - compiled_tuple_header.duplicate_vector_from_pool (pool, o.compiled_tuple_header); - compiled_deltas.duplicate_vector_from_pool (pool, o.compiled_deltas); - compiled_peak_coords.duplicate_vector_from_pool (pool, o.compiled_peak_coords); - compiled_interm_coords.duplicate_vector_from_pool (pool, o.compiled_interm_coords); + return !axis_tuples.in_error () && + indices.duplicate_vector_from_pool (pool, o.indices) && + deltas_x.duplicate_vector_from_pool (pool, o.deltas_x) && + deltas_y.duplicate_vector_from_pool (pool, o.deltas_y) && + compiled_tuple_header.duplicate_vector_from_pool (pool, o.compiled_tuple_header) && + compiled_deltas.duplicate_vector_from_pool (pool, o.compiled_deltas) && + compiled_peak_coords.duplicate_vector_from_pool (pool, o.compiled_peak_coords) && + compiled_interm_coords.duplicate_vector_from_pool (pool, o.compiled_interm_coords); } void remove_axis (hb_tag_t axis_tag) @@ -427,7 +428,13 @@ struct tuple_delta_t tuple_delta_t new_var; if (i < solutions.length - 1) - new_var.copy_from (*this, pool); + { + if (unlikely (!new_var.copy_from (*this, pool))) + { + out.ensure_error (); + return; + } + } else new_var = std::move (*this); @@ -1158,6 +1165,7 @@ struct TupleVariationData { // This may move var out. var.change_tuple_var_axis_limit (axis_tag, *axis_limit, axis_triple_distances, out, scratch, pool); + if (unlikely (out.in_error ())) return false; if (!out) continue; unsigned new_len = new_vars.length + out.length; diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 36ca91e7f..58a758a9e 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -638,7 +638,8 @@ struct hb_vector_t { assert (!length && !allocated); arrayZ = (Type *) allocator->alloc (size * sizeof (Type), alignof (Type)); - if (unlikely (!arrayZ)) + /* The pool returns nullptr for a zero-size request; that is not a failure. */ + if (unlikely (size && !arrayZ)) { set_error (); return false; diff --git a/src/test-vector.cc b/src/test-vector.cc index b81392058..97195b749 100644 --- a/src/test-vector.cc +++ b/src/test-vector.cc @@ -27,6 +27,7 @@ #include "hb.hh" #include "hb-vector.hh" +#include "hb-alloc-pool.hh" #include "hb-set.hh" #include "hb-map.hh" #include @@ -314,5 +315,18 @@ main (int argc, char **argv) v.push (m); } + /* An empty allocation from a pool with no open chunk is not a failure. */ + { + hb_alloc_pool_t pool; + hb_vector_t v, empty; + hb_always_assert (v.allocate_from_pool (&pool, 0)); + hb_always_assert (!v.in_error ()); + hb_always_assert (v.duplicate_vector_from_pool (&pool, empty)); + hb_always_assert (!v.in_error ()); + hb_always_assert (v.allocate_from_pool (&pool, 2)); + hb_always_assert (!v.in_error ()); + hb_always_assert (v.length == 2); + } + return 0; }