From 340aec5f774efeaf2604f014c06ab7043bbb114f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 10 Oct 2026 22:16:15 -0600 Subject: [PATCH] [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 --- src/OT/Layout/GPOS/ValueFormat.hh | 14 ++-- .../fonts/generate-layout-value-overflow.py | 67 ++++++++++++++++ test/api/fonts/layout-value-overflow.ttf | Bin 0 -> 1404 bytes test/api/test-subset-gpos.c | 75 ++++++++++++++++++ 4 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 test/api/fonts/generate-layout-value-overflow.py create mode 100644 test/api/fonts/layout-value-overflow.ttf diff --git a/src/OT/Layout/GPOS/ValueFormat.hh b/src/OT/Layout/GPOS/ValueFormat.hh index 006f84130..518736a4a 100644 --- a/src/OT/Layout/GPOS/ValueFormat.hh +++ b/src/OT/Layout/GPOS/ValueFormat.hh @@ -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> *layout_variation_idx_delta_map) const @@ -341,7 +342,8 @@ struct ValueFormat : HBUINT16 hb_pair_t *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, diff --git a/test/api/fonts/generate-layout-value-overflow.py b/test/api/fonts/generate-layout-value-overflow.py new file mode 100644 index 000000000..4ff0229ea --- /dev/null +++ b/test/api/fonts/generate-layout-value-overflow.py @@ -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")) diff --git a/test/api/fonts/layout-value-overflow.ttf b/test/api/fonts/layout-value-overflow.ttf new file mode 100644 index 0000000000000000000000000000000000000000..c3226f62ddb7313b11ca8a9eececf390e9f94b98 GIT binary patch literal 1404 zcmah}O-NKx6#mYAGtQ`^GgBf`5L0MIM2){01Qm=5HK1tzt?HrAH$yWz=$q6CaurdV zw(W!_%w*LrSx+{P%z znIF@7-kvYuID+(dQBO?SdFR!OKST7Vsedj^&w8ul&o}ul|H( zeWwKvo8B(&e#?ezP`9A8mBbt?;qwgB%>=!=(ryxCi8H6<*~;9BvDBzMXRm=B(~;7i z>eon&v{5MaU?uQrUuFVQn$te}K~?u`CN$Ac3m9(5iQ(0E1#jTReqzJ~! zvY-l1+FqD0dWm5>S9B8TY(DGF4mfTxXU~i{SvTX2WGB1~(j4Fdrcs2)BZjHvC_9wX z$RbbnFpB|NT-tN=&tQaFmU@Q15lRz$XW0JsUTFC+iu0mMED1$t~noavQmw+`+?yci&CN%f9$sx_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 (); }