[subset] Check instanced GPOS value ranges

Adding a variation delta directly to HBINT16 silently wraps values
outside the signed 16-bit range. Use the serializer's checked assignment
with a wide sum, as other layout coordinates already do.

Cover all four value fields, both signs, valid boundaries, partial
instancing, and both axis orders with a reproducible two-axis font.
All 255 configured tests pass; the new test fails with the old library.

Assisted-by: Codex
This commit is contained in:
Behdad Esfahbod committed 2026-10-10 22:16:15 -06:00
1 parent 3f60c824e6
commit 340aec5f77
4 files changed
+150 -6

No files matched your search

+8 -6
View File
@@ -222,25 +222,25 @@ struct ValueFormat : HBUINT16
if (format & xPlaDevice)
{
add_delta_to_value (x_placement, base, values, layout_variation_idx_delta_map);
add_delta_to_value (c, x_placement, base, values, layout_variation_idx_delta_map);
copy_device (c, base, values++, layout_variation_idx_delta_map, new_format, xPlaDevice);
}
if (format & yPlaDevice)
{
add_delta_to_value (y_placement, base, values, layout_variation_idx_delta_map);
add_delta_to_value (c, y_placement, base, values, layout_variation_idx_delta_map);
copy_device (c, base, values++, layout_variation_idx_delta_map, new_format, yPlaDevice);
}
if (format & xAdvDevice)
{
add_delta_to_value (x_adv, base, values, layout_variation_idx_delta_map);
add_delta_to_value (c, x_adv, base, values, layout_variation_idx_delta_map);
copy_device (c, base, values++, layout_variation_idx_delta_map, new_format, xAdvDevice);
}
if (format & yAdvDevice)
{
add_delta_to_value (y_adv, base, values, layout_variation_idx_delta_map);
add_delta_to_value (c, y_adv, base, values, layout_variation_idx_delta_map);
copy_device (c, base, values++, layout_variation_idx_delta_map, new_format, yAdvDevice);
}
}
@@ -331,7 +331,8 @@ struct ValueFormat : HBUINT16
return base + offset;
}
void add_delta_to_value (HBINT16 *value,
void add_delta_to_value (hb_serialize_context_t *c,
HBINT16 *value,
const ValueBase *base,
const Value *src_value,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *layout_variation_idx_delta_map) const
@@ -341,7 +342,8 @@ struct ValueFormat : HBUINT16
hb_pair_t<unsigned, int> *varidx_delta;
if (!layout_variation_idx_delta_map->has (varidx, &varidx_delta)) return;
*value += hb_second (*varidx_delta);
c->check_assign (*value, (int64_t) (int) *value + hb_second (*varidx_delta),
HB_SERIALIZE_ERROR_INT_OVERFLOW);
}
bool copy_device (hb_serialize_context_t *c,
@@ -0,0 +1,67 @@
"""Generate signed-16 positioning limits and overflowing variation deltas."""
from pathlib import Path
from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
from fontTools.fontBuilder import FontBuilder
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.ttLib import newTable
from fontTools.ttLib.tables import otTables as ot
from fontTools.varLib.builder import (
buildVarData,
buildVarDevTable,
buildVarRegionList,
buildVarStore,
)
fb = FontBuilder(1000, isTTF=True)
names = [".notdef"] + ["g" + str(i) for i in range(16)]
fb.setupGlyphOrder(names)
fb.setupCharacterMap({0x61 + i: name for i, name in enumerate(names[1:])})
pen = TTGlyphPen(None)
pen.moveTo((0, 0))
pen.lineTo((500, 0))
pen.lineTo((500, 500))
pen.closePath()
fb.setupGlyf({name: pen.glyph() for name in names})
fb.setupHorizontalMetrics({name: (600, 0) for name in names})
fb.setupHorizontalHeader(ascent=800, descent=-200)
fb.setupNameTable({"familyName": "Layout Value Limits", "styleName": "Regular"})
fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200)
fb.setupPost()
fb.setupFvar([("wght", 0, 0, 1, "Weight"), ("wdth", 0, 0, 1, "Width")], [])
font = fb.font
rules = []
rows = []
for i in range(16):
sign = 1 if i % 2 == 0 else -1
values = [0] * 4
values[(i % 8) // 2] = sign * (30000 if i < 8 else 32760)
rules.append(f"pos g{i} <{' '.join(map(str, values))}>;")
rows.append([sign * 10000 if i < 8 else (7 if sign > 0 else -8), sign])
addOpenTypeFeaturesFromString(font, "feature kern {\n" + "\n".join(rules) + "\n} kern;")
fields = ["XPlaDevice", "YPlaDevice", "XAdvDevice", "YAdvDevice"]
for lookup in font["GPOS"].table.LookupList.Lookup:
for subtable in lookup.SubTable:
records = (
subtable.Value
if subtable.Format == 2
else [subtable.Value] * len(subtable.Coverage.glyphs)
)
for name, record in zip(subtable.Coverage.glyphs, records):
index = int(name[1:])
field = (index % 8) // 2
setattr(record, fields[field], buildVarDevTable(index))
subtable.ValueFormat |= 0x10 << field
font["GDEF"] = newTable("GDEF")
gdef = font["GDEF"].table = ot.GDEF()
gdef.Version = 0x00010003
gdef.populateDefaults()
gdef.VarStore = buildVarStore(
buildVarRegionList([{"wght": (0, 1, 1)}, {"wdth": (0, 1, 1)}], ["wght", "wdth"]),
[buildVarData([0, 1], rows)],
)
font["head"].created = font["head"].modified = 2082844800
font.recalcTimestamp = False
font.save(Path(__file__).with_name("layout-value-overflow.ttf"))
Binary file not shown.
+75
View File
@@ -81,6 +81,80 @@ test_subset_gpos_pairpos1_vf (void)
hb_face_destroy (face_wa);
}
static void
test_subset_gpos_value_limits (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/layout-value-overflow.ttf");
for (unsigned glyph = 0; glyph < 16; glyph++)
{
for (unsigned partial = 0; partial < 2; partial++)
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_unicode_set (input), 'a' + glyph);
g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w', 'g', 'h', 't'), 1));
if (!partial)
g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w', 'd', 't', 'h'), 0));
hb_face_t *subset = hb_subset_or_fail (face, input);
hb_subset_input_destroy (input);
if (glyph < 8)
{
/* 30000 + 10000 and -30000 - 10000 must not wrap. */
g_assert_null (subset);
continue;
}
g_assert_nonnull (subset);
for (unsigned width = 0; width <= (partial ? 2 : 0); width++)
{
hb_font_t *fonts[] = {hb_font_create (face), hb_font_create (subset)};
const int coords[] = {16384, (int) width * 8192};
hb_font_set_var_coords_normalized (fonts[0], coords, 2);
if (partial)
hb_font_set_var_coords_normalized (fonts[1], &coords[1], 1);
hb_buffer_t *buffers[] = {hb_buffer_create (), hb_buffer_create ()};
const hb_glyph_position_t *positions[2];
for (unsigned i = 0; i < 2; i++)
{
hb_buffer_add (buffers[i], 'a' + glyph, 0);
hb_buffer_set_content_type (buffers[i], HB_BUFFER_CONTENT_TYPE_UNICODE);
hb_buffer_guess_segment_properties (buffers[i]);
hb_shape (fonts[i], buffers[i], NULL, 0);
unsigned length;
positions[i] = hb_buffer_get_glyph_positions (buffers[i], &length);
g_assert_cmpuint (length, ==, 1);
}
g_assert_cmpint (positions[1]->x_offset, ==, positions[0]->x_offset);
g_assert_cmpint (positions[1]->y_offset, ==, positions[0]->y_offset);
g_assert_cmpint (positions[1]->x_advance, ==, positions[0]->x_advance);
g_assert_cmpint (positions[1]->y_advance, ==, positions[0]->y_advance);
for (unsigned i = 0; i < 2; i++)
{
hb_buffer_destroy (buffers[i]);
hb_font_destroy (fonts[i]);
}
}
hb_face_destroy (subset);
}
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_unicode_set (input), 'a' + glyph);
g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w', 'd', 't', 'h'), 0));
hb_face_t *width_first = hb_subset_or_fail (face, input);
g_assert_nonnull (width_first);
hb_subset_input_destroy (input);
input = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_unicode_set (input), 'a' + glyph);
g_assert_true (hb_subset_input_pin_axis_location (input, width_first, HB_TAG ('w', 'g', 'h', 't'), 1));
hb_face_t *subset = hb_subset_or_fail (width_first, input);
if (glyph < 8)
g_assert_null (subset);
else
g_assert_nonnull (subset);
hb_face_destroy (subset);
hb_face_destroy (width_first);
hb_subset_input_destroy (input);
}
hb_face_destroy (face);
}
static void
test_subset_gpos_device_only_pairpos (void)
{
@@ -140,6 +214,7 @@ main (int argc, char **argv)
hb_test_add (test_subset_gpos_lookup_subtable);
hb_test_add (test_subset_gpos_pairpos1_vf);
hb_test_add (test_subset_gpos_device_only_pairpos);
hb_test_add (test_subset_gpos_value_limits);
return hb_test_run ();
}