diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 5bffa574f..cfa0782a4 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -259,6 +259,14 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28) #endif +/* Partially instancing one CFF2 table in the subsetter, in cells of the + * old-region to new-region matrices built for its VarDatas. A VarData + * with k regions costs k * k cells for 2 * k bytes of font, and many + * offsets can share one VarData. Exceeding the budget fails the table. */ +#ifndef HB_BUDGET_SUBSET_CFF2_CELLS +#define HB_BUDGET_SUBSET_CFF2_CELLS ((int64_t) 1 << 24) +#endif + /* The flat pixel-work floor for one raster draw or paint session. Pixel and * outline work use separate live counters, so keep this independent of * the common outline budget above. */ diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 876384fa2..97e326f5c 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -133,12 +133,17 @@ struct cff2_instancing_plan_t /* Instance each VarData with identity delta rows; the surviving tuples' * delta columns are the old-region → new-region transform. */ OT::optimize_scratch_t scratch; + int64_t budget = HB_BUDGET_SUBSET_CFF2_CELLS; for (unsigned m = 0; m < major_count; m++) { const OT::VarData &var_data = varStore.get_sub_table (m); unsigned k = var_data.get_region_index_count (); vardata_transform_t &t = transforms.arrayZ[m]; + /* k identity tuples of k cells each; neither k nor the number of + * VarDatas is otherwise bounded, and offsets may alias one VarData. */ + if (unlikely (!hb_budget_spend (budget, k, k))) return false; + if (!t.gains.resize_exact (k)) return false; varStore.get_region_scalars (m, plan->normalized_coords.arrayZ, plan->normalized_coords.length, @@ -165,6 +170,10 @@ struct cff2_instancing_plan_t if (!tuples.instantiate (plan->axes_location, plan->axes_triple_distances, scratch)) return false; + /* The solver can split a region into several; the surviving tuples' + * columns are kept for the rest of the table. */ + if (unlikely (!hb_budget_spend (budget, tuples.tuple_vars.length, k))) return false; + for (auto &tuple : tuples.tuple_vars) { if (unlikely (tuple.deltas_x.length != k)) return false; diff --git a/test/api/fonts/CFF2.vardata-budget.otf b/test/api/fonts/CFF2.vardata-budget.otf new file mode 100644 index 000000000..f83d3e15f Binary files /dev/null and b/test/api/fonts/CFF2.vardata-budget.otf differ diff --git a/test/api/test-instance-cff2.c b/test/api/test-instance-cff2.c index d996cd3a7..7cdfc8d26 100644 --- a/test/api/test-instance-cff2.c +++ b/test/api/test-instance-cff2.c @@ -87,6 +87,33 @@ test_instance_cff2_private_blend_overflow (void) hb_face_destroy (face); } +static void +test_instance_cff2_vardata_budget (void) +{ + /* 40 VarData offsets aliasing one 800-region VarData no blend uses: + * partial instancing must not build a k×k region transform for each + * of them. Over budget, create() fails, so the partial instance fails + * safely like any other CFF2 planning failure instead of exhausting + * memory. */ + hb_face_t *face = hb_test_open_font_file ("fonts/CFF2.vardata-budget.otf"); + + hb_set_t *glyphs = hb_set_create (); + hb_set_add_range (glyphs, 0, 1); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_union (hb_subset_input_glyph_set (input), glyphs); + hb_set_destroy (glyphs); + g_assert_true (hb_subset_input_set_axis_range (input, face, + HB_TAG ('w', 'g', 'h', 't'), + 200, 600, 400)); + + hb_face_t *subset = hb_subset_or_fail (face, input); + hb_subset_input_destroy (input); + g_assert_null (subset); + + hb_face_destroy (face); +} + static void test_instance_cff2_empty_bounds (void) @@ -376,6 +403,7 @@ main (int argc, char **argv) hb_test_add (test_instance_cff2); hb_test_add (test_instance_cff2_private_blend_overflow); + hb_test_add (test_instance_cff2_vardata_budget); hb_test_add (test_instance_cff2_empty_bounds); hb_test_add (test_instance_cff2_large_store); hb_test_add (test_instance_cff2_absolute_rounding);