mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-23 16:01:07 +01:00
[subset] Don't retain ItemVariationStore rows the source lacks
The subset plan turned every variation index collected by the COLR, GDEF and BASE closures into a row to keep, checking only the outer index against the subtable count and never the inner index against VarData::itemCount. A font could therefore name tens of thousands of rows in a VarData whose itemCount is 2, and partial instancing then sized its work from that inflated row count, allocating regionIndexCount x rows x 5 bytes (3.2 GB from a 240 KB COLRv1 font). generate_varstore_inner_maps() now takes the store and keeps only rows that ItemVariationStore::has_delta_set() confirms exist, and remap_variation_indices() maps such an index to HB_OT_LAYOUT_NO_VARIATIONS_INDEX without consuming a new row. That preserves the runtime meaning of an out-of-range index (delta 0) and keeps the two functions in step; all consumers already handle the sentinel. This covers the COLR, GDEF and BASE callers. Well-formed fonts are byte-identical. The HVAR/VVAR route noted in the report (its planner builds row maps itself, one per retained glyph) is left for a follow-up: folding the missing rows into a shared zero row changes the output form of a map-less HVAR whose glyph count exceeds the store's itemCount, which is better landed on its own. Adds an API regression test (test_subset_colr_phantom_rows) that fails on the unpatched tree. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-0TLE41I0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
aad69a017b
commit
edf8de90d6
7 files changed
+142
-14
No files matched your search
@@ -564,8 +564,7 @@ collect_layout_variation_indices (hb_subset_plan_t* plan)
|
||||
plan->all_axes_pinned,
|
||||
plan->layout_variation_idx_delta_map);
|
||||
|
||||
unsigned subtable_count = gdef->get_var_store ().get_sub_table_count ();
|
||||
generate_varstore_inner_maps (varidx_set, subtable_count, plan->gdef_varstore_inner_maps);
|
||||
generate_varstore_inner_maps (gdef->get_var_store (), varidx_set, plan->gdef_varstore_inner_maps);
|
||||
plan->check_success (!plan->layout_condition_values.in_error ());
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -36,11 +36,13 @@
|
||||
|
||||
#ifndef HB_NO_VAR
|
||||
|
||||
template<typename ItemVarStore>
|
||||
void
|
||||
generate_varstore_inner_maps (const hb_set_t& varidx_set,
|
||||
unsigned subtable_count,
|
||||
generate_varstore_inner_maps (const ItemVarStore &var_store,
|
||||
const hb_set_t& varidx_set,
|
||||
hb_vector_t<hb_inc_bimap_t> &inner_maps /* OUT */)
|
||||
{
|
||||
unsigned subtable_count = var_store.get_sub_table_count ();
|
||||
if (varidx_set.is_empty () || subtable_count == 0) return;
|
||||
|
||||
if (unlikely (!inner_maps.resize (subtable_count))) return;
|
||||
@@ -49,12 +51,20 @@
|
||||
uint16_t major = idx >> 16;
|
||||
uint16_t minor = idx & 0xFFFF;
|
||||
|
||||
if (major >= subtable_count)
|
||||
/* Runtime treats out-of-range delta-set indices as zero; don't
|
||||
* retain rows the store doesn't have. */
|
||||
if (!var_store.has_delta_set (idx))
|
||||
continue;
|
||||
inner_maps[major].add (minor);
|
||||
}
|
||||
}
|
||||
|
||||
template
|
||||
void
|
||||
generate_varstore_inner_maps<OT::ItemVariationStore> (const OT::ItemVariationStore &var_store,
|
||||
const hb_set_t& varidx_set,
|
||||
hb_vector_t<hb_inc_bimap_t> &inner_maps /* OUT */);
|
||||
|
||||
/* Evaluate a variation-region tent at v, mirroring VarRegionAxis::evaluate
|
||||
* (invalid regions evaluate as constant 1). */
|
||||
static double
|
||||
@@ -392,7 +402,7 @@
|
||||
auto *store_cache = var_store.create_cache ();
|
||||
|
||||
unsigned new_major = 0, new_minor = 0;
|
||||
unsigned last_major = (variation_indices.get_min ()) >> 16;
|
||||
unsigned last_major = (unsigned) -1;
|
||||
for (unsigned idx : variation_indices)
|
||||
{
|
||||
int delta = 0;
|
||||
@@ -408,7 +418,15 @@
|
||||
|
||||
uint16_t major = idx >> 16;
|
||||
if (major >= subtable_count) break;
|
||||
if (major != last_major)
|
||||
/* Keep in sync with generate_varstore_inner_maps (): rows the store
|
||||
* doesn't have are not retained, so such an index keeps its runtime
|
||||
* meaning (no delta) instead of consuming a new row. */
|
||||
if (!var_store.has_delta_set (idx))
|
||||
{
|
||||
variation_idx_delta_map.set (idx, hb_pair_t<unsigned, int> (HB_OT_LAYOUT_NO_VARIATIONS_INDEX, delta));
|
||||
continue;
|
||||
}
|
||||
if (last_major != (unsigned) -1 && major != last_major)
|
||||
{
|
||||
new_minor = 0;
|
||||
++new_major;
|
||||
@@ -444,15 +462,13 @@
|
||||
hb_set_t varidx_set;
|
||||
base->collect_variation_indices (plan, varidx_set);
|
||||
const OT::ItemVariationStore &var_store = base->get_var_store ();
|
||||
unsigned subtable_count = var_store.get_sub_table_count ();
|
||||
|
||||
|
||||
remap_variation_indices (var_store, varidx_set,
|
||||
plan->normalized_coords,
|
||||
!plan->pinned_at_default,
|
||||
plan->all_axes_pinned,
|
||||
plan->base_variation_idx_map);
|
||||
generate_varstore_inner_maps (varidx_set, subtable_count, plan->base_varstore_inner_maps);
|
||||
generate_varstore_inner_maps (var_store, varidx_set, plan->base_varstore_inner_maps);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -147,8 +147,7 @@ static void _colr_closure (hb_subset_plan_t* plan,
|
||||
{
|
||||
const OT::ItemVariationStore &var_store = colr.get_var_store ();
|
||||
// generated inner_maps is used by ItemVariationStore serialize(), which is subset only
|
||||
unsigned subtable_count = var_store.get_sub_table_count ();
|
||||
generate_varstore_inner_maps (variation_indices, subtable_count, plan->colrv1_varstore_inner_maps);
|
||||
generate_varstore_inner_maps (var_store, variation_indices, plan->colrv1_varstore_inner_maps);
|
||||
|
||||
if (!plan->all_axes_pinned)
|
||||
remap_variation_indices (var_store,
|
||||
|
||||
@@ -382,9 +382,10 @@ remap_colrv1_delta_set_index_indices (const DeltaSetIndexMap &index_map,
|
||||
hb_map_t &new_deltaset_idx_varidx_map /* OUT */);
|
||||
|
||||
|
||||
template<typename ItemVarStore>
|
||||
HB_INTERNAL void
|
||||
generate_varstore_inner_maps (const hb_set_t& varidx_set,
|
||||
unsigned subtable_count,
|
||||
generate_varstore_inner_maps (const ItemVarStore &var_store,
|
||||
const hb_set_t& varidx_set,
|
||||
hb_vector_t<hb_inc_bimap_t> &inner_maps /* OUT */);
|
||||
|
||||
HB_INTERNAL bool
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,44 @@
|
||||
"""Generate a COLRv1 font whose colour stops name variation rows that the
|
||||
ItemVariationStore does not have."""
|
||||
|
||||
from pathlib import Path
|
||||
from fontTools.fontBuilder import FontBuilder
|
||||
from fontTools.colorLib.builder import buildCOLR, buildCPAL
|
||||
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
||||
from fontTools.varLib.builder import buildVarRegionList, buildVarData, buildVarStore
|
||||
|
||||
fb = FontBuilder(1000, isTTF=True)
|
||||
names = [".notdef", "linear"]
|
||||
fb.setupGlyphOrder(names)
|
||||
fb.setupCharacterMap({0x61: "linear"})
|
||||
fb.setupGlyf({glyph: TTGlyphPen(None).glyph() for glyph in names})
|
||||
fb.setupHorizontalMetrics({glyph: (1000, 0) for glyph in names})
|
||||
fb.setupHorizontalHeader(ascent=800, descent=-200)
|
||||
fb.setupNameTable({"familyName": "COLR Phantom Rows", "styleName": "Regular"})
|
||||
fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200)
|
||||
fb.setupPost()
|
||||
fb.setupFvar([("wght", 0, 0, 1, "Weight")], [])
|
||||
# The store has two rows (0 and 1). Stop 0 uses them; the other stops name
|
||||
# rows 2..5, which runtime evaluates as zero deltas.
|
||||
paint = dict(
|
||||
Format=5,
|
||||
ColorLine=dict(
|
||||
Extend="pad",
|
||||
ColorStop=[
|
||||
dict(StopOffset=0, PaletteIndex=0, Alpha=1, VarIndexBase=0),
|
||||
dict(StopOffset=0.5, PaletteIndex=1, Alpha=1, VarIndexBase=2),
|
||||
dict(StopOffset=1, PaletteIndex=0, Alpha=1, VarIndexBase=4),
|
||||
],
|
||||
),
|
||||
x0=0, y0=0, x1=100, y1=0, x2=0, y2=100, VarIndexBase=0xFFFFFFFF,
|
||||
)
|
||||
regions = buildVarRegionList([{"wght": (0, 1, 1)}], ["wght"])
|
||||
store = buildVarStore(regions, [buildVarData([0], [[1024], [-2048]], optimize=False)])
|
||||
fb.font["COLR"] = buildCOLR(
|
||||
{"linear": paint}, version=1, glyphMap=fb.font.getReverseGlyphMap(),
|
||||
varStore=store, clipBoxes={"linear": (0, 0, 100, 100)},
|
||||
)
|
||||
fb.font["CPAL"] = buildCPAL([[(0, 0, 0, 1), (1, 1, 1, 1)]])
|
||||
fb.font.recalcTimestamp = False
|
||||
fb.font["head"].created = fb.font["head"].modified = 2082844800
|
||||
fb.save(Path(__file__).parent / "colr-phantom-rows.ttf")
|
||||
@@ -876,6 +876,74 @@ test_subset_colr_shared_layers (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
check_colr_phantom_rows (hb_face_t *face, bool variable)
|
||||
{
|
||||
hb_blob_t *blob = hb_face_reference_table (face, HB_TAG ('C','O','L','R'));
|
||||
unsigned length;
|
||||
const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length);
|
||||
g_assert_cmpuint (length, >=, 34);
|
||||
unsigned list = read_uint_be (data + 14, 4);
|
||||
g_assert_cmpuint (list + 10, <=, length);
|
||||
g_assert_cmpuint (read_uint_be (data + list, 4), ==, 1);
|
||||
unsigned paint = list + read_uint_be (data + list + 6, 4);
|
||||
g_assert_cmpuint (paint + 16, <=, length);
|
||||
g_assert_cmpuint (data[paint], ==, variable ? 5 : 4);
|
||||
unsigned line = paint + read_uint_be (data + paint + 1, 3);
|
||||
unsigned stride = variable ? 10 : 6;
|
||||
g_assert_cmpuint (line + 3 + 3 * stride, <=, length);
|
||||
g_assert_cmpuint (read_uint_be (data + line + 1, 2), ==, 3);
|
||||
for (unsigned stop = 0; stop < 3; stop++)
|
||||
{
|
||||
unsigned at = line + 3 + stop * stride;
|
||||
/* Only the first stop names rows the store has; the others keep
|
||||
* their runtime meaning of no variation. */
|
||||
unsigned offset = stop * 8192, alpha = 16384;
|
||||
if (!variable && !stop)
|
||||
{
|
||||
offset = 1024;
|
||||
alpha = 14336;
|
||||
}
|
||||
g_assert_cmpuint (read_uint_be (data + at, 2), ==, offset);
|
||||
g_assert_cmpuint (read_uint_be (data + at + 2, 2), ==, stop % 2);
|
||||
g_assert_cmpuint (read_uint_be (data + at + 4, 2), ==, alpha);
|
||||
if (variable)
|
||||
g_assert_cmpuint (read_uint_be (data + at + 6, 4), ==, stop ? 0xFFFFFFFFu : 0);
|
||||
}
|
||||
unsigned store = read_uint_be (data + 30, 4);
|
||||
if (!variable)
|
||||
g_assert_cmpuint (store, ==, 0);
|
||||
else
|
||||
{
|
||||
g_assert_cmpuint (store + 12, <=, length);
|
||||
g_assert_cmpuint (read_uint_be (data + store + 6, 2), ==, 1);
|
||||
unsigned var_data = store + read_uint_be (data + store + 8, 4);
|
||||
g_assert_cmpuint (var_data + 6, <=, length);
|
||||
/* No rows are invented for the stops the source store doesn't cover. */
|
||||
g_assert_cmpuint (read_uint_be (data + var_data, 2), ==, 2);
|
||||
}
|
||||
hb_blob_destroy (blob);
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_colr_phantom_rows (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/colr-phantom-rows.ttf");
|
||||
for (unsigned mode = 0; mode < 3; mode++)
|
||||
{
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_glyph_set (input), 0, 1);
|
||||
if (mode == 1)
|
||||
g_assert_true (hb_subset_input_set_axis_range (input, face, HB_TAG ('w','g','h','t'), 0, 0.5f, 0));
|
||||
if (mode == 2)
|
||||
g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w','g','h','t'), 1));
|
||||
hb_face_t *subset = hb_subset_test_create_subset (face, input);
|
||||
check_colr_phantom_rows (subset, mode != 2);
|
||||
hb_face_destroy (subset);
|
||||
}
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -896,6 +964,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_subset_colr_var_overflow);
|
||||
hb_test_add (test_subset_colr_layers_out_of_range);
|
||||
hb_test_add (test_subset_colr_shared_layers);
|
||||
hb_test_add (test_subset_colr_phantom_rows);
|
||||
|
||||
return hb_test_run();
|
||||
}
|
||||
Reference in new issue
Block a user