From 2b6ffde1e5c7edced5c20a3a134ca3d73d60190f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 21 Oct 2026 19:58:41 -0600 Subject: [PATCH] [cff] Discard ignored Private DICT blend operands while loading CFF2 (#6431) The ordinary CFF2 Private DICT parser ignores hint blend operators but leaves their raw operands on its stack. Multiple valid blend groups can therefore overflow the stack during font loading, making glyph extents and drawing fail. The subset parser already discards these operands. Clear ignored operands after each hint blend in the ordinary parser too. Add a synthetic font with two seven-result groups over 40 regions and check its glyph extents at default and non-default locations. Each group fits the stack, but their raw operands together exceed its capacity. The regression fails before the fix and passes after. The complete Meson suite passes: 272 tests passed, one skipped. Assisted-by: Codex --- src/hb-ot-cff2-table.hh | 3 ++ test/api/fonts/cff2-private-blend-groups.otf | Bin 0 -> 1648 bytes ...generate-cff2-private-blend-groups-font.py | 44 ++++++++++++++++++ test/api/test-ot-extents-cff.c | 22 +++++++++ 4 files changed, 69 insertions(+) create mode 100644 test/api/fonts/cff2-private-blend-groups.otf create mode 100644 test/api/generate-cff2-private-blend-groups-font.py diff --git a/src/hb-ot-cff2-table.hh b/src/hb-ot-cff2-table.hh index 76e8899c6..18440c51e 100644 --- a/src/hb-ot-cff2-table.hh +++ b/src/hb-ot-cff2-table.hh @@ -303,6 +303,9 @@ struct cff2_private_dict_opset_t : dict_opset_t env.clear_args (); break; case OpCode_blenddict: + /* Hint values are ignored here. Discard each group so raw operands + * from successive blends cannot overflow the stack. */ + env.clear_args (); break; default: diff --git a/test/api/fonts/cff2-private-blend-groups.otf b/test/api/fonts/cff2-private-blend-groups.otf new file mode 100644 index 0000000000000000000000000000000000000000..17bb03131fe3fb7719008c1814243149ac4c0516 GIT binary patch literal 1648 zcmeHH&ubGw6#iy6-6k5_w6>cNA5K*gMl-%L`8kp2bs?arI;n|bqQ-g`TnUtP`P2wKo# z-dkG(cv+$GPtwpA)v zuf7q#WxM8bK+klq6MtvRx_;CYdqfZcmEl{>0NS$Tamg!|Z>Rl_yX12H`=DBjcrJ>z zPGv$+vjuOeYY)e7{DiLm;0T_~y=x8j5>s?&Js{WUW_PjAWG6ih3OUaq?RBvyo?-;C z!RZ6=KVEK9XqV%2keJ+9#jgz1ZbVZ&sq{3(J!wH5Q#{x0l2La*{XMUNdwncuzaNK& zQ8INJDg&pa$5?_R^T_nEhB4gfVcH~7SPs3071`!S*{+mKC#=?k+KTPe%T`#hJ;#lh zkq{mlun@uK(Kb+KUqOl3;anAU1gK$!qYiPImLUpc9=Xd^6jeV7hKt5jCpu%v?62Pw zFJ`2Tc%ULlO0C{`;1-|%Ni$eR&G0qmFUAxZtZ*C&-qfS4-7(g394Sm-631EjX`DbB zGdPJ^*8Vik;4IFuNEw{R1!OUYi?~F8nh+xK{N?nA*Tv5x(!UT!=WAznvbdvU`%s2v lpq=jJXJ_c_;ox>VZSZqa_cq0Ew)by}Z^@s?zVIqbgFoz@vX=k= literal 0 HcmV?d00001 diff --git a/test/api/generate-cff2-private-blend-groups-font.py b/test/api/generate-cff2-private-blend-groups-font.py new file mode 100644 index 000000000..262606326 --- /dev/null +++ b/test/api/generate-cff2-private-blend-groups-font.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""Generate a CFF2 font with multiple valid Private DICT blend groups.""" +from pathlib import Path + +from fontTools.cffLib import PrivateDictCompiler, encodeNumber +from fontTools.fontBuilder import FontBuilder +from fontTools.misc.psCharStrings import T2CharString + + +class BlendGroupsCompiler(PrivateDictCompiler): + def compile(self, reason): + # Emit two seven-result groups directly, independently of FontTools' + # split-blend compiler. Live results keep the stack below 513 entries. + data = bytearray() + for group in range(2): + defaults = ([-40] + [2] * 6) if group == 0 else [2] * 7 + for value in defaults + [0] * (7 * 40) + [7]: + data.extend(encodeNumber(value)) + data.append(23) # blend + data.append(6) # BlueValues + return bytes(data) + + +builder = FontBuilder(1000, isTTF=False) +builder.font.recalcBBoxes = False +builder.font.recalcTimestamp = False +builder.font["head"].created = builder.font["head"].modified = 2082844800 +builder.setupGlyphOrder([".notdef", "A"]) +builder.setupCharacterMap({65: "A"}) +builder.setupHorizontalMetrics({name: (500, 0) for name in [".notdef", "A"]}) +builder.setupHorizontalHeader(ascent=800, descent=-200) +builder.setupNameTable({"familyName": "Private blend groups", "styleName": "Regular"}) +builder.setupOS2() +builder.setupPost() +builder.setupFvar([("wght", 0, 0, 1, "Weight")], []) +builder.setupCFF2( + { + ".notdef": T2CharString(program=[]), + "A": T2CharString(program=[10, 20, "rmoveto", 40, 30, -40, 60, "rlineto"]), + }, + regions=[{"wght": (0, 1, 1)}] * 40, +) +builder.font["CFF2"].cff.topDictIndex[0].FDArray[0].Private.compilerClass = BlendGroupsCompiler +builder.save(Path(__file__).parent / "fonts" / "cff2-private-blend-groups.otf") diff --git a/test/api/test-ot-extents-cff.c b/test/api/test-ot-extents-cff.c index c867e66de..ce04503a3 100644 --- a/test/api/test-ot-extents-cff.c +++ b/test/api/test-ot-extents-cff.c @@ -208,6 +208,27 @@ test_extents_cff2_vsindex_named_instance (void) hb_font_destroy (font); } +static void +test_extents_cff2_private_blend_groups (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/cff2-private-blend-groups.otf"); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + + for (unsigned i = 0; i < 2; i++) + { + float coordinate = i * 0.5f; + hb_font_set_var_coords_design (font, &coordinate, 1); + hb_glyph_extents_t extents; + g_assert_true (hb_font_get_glyph_extents (font, 1, &extents)); + g_assert_cmpint (extents.x_bearing, ==, 10); + g_assert_cmpint (extents.y_bearing, ==, 110); + g_assert_cmpint (extents.width, ==, 40); + g_assert_cmpint (extents.height, ==, -90); + } + hb_font_destroy (font); +} + int main (int argc, char **argv) { @@ -219,6 +240,7 @@ main (int argc, char **argv) hb_test_add (test_extents_cff2); hb_test_add (test_extents_cff2_vsindex); hb_test_add (test_extents_cff2_vsindex_named_instance); + hb_test_add (test_extents_cff2_private_blend_groups); return hb_test_run (); }