[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 <noreply@openai.com>
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-21 19:58:41 -06:00
1 parent 5a1c0c4660
commit 2b6ffde1e5
4 files changed
+69

No files matched your search

+3
View File
@@ -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:
Binary file not shown.
@@ -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")
+22
View File
@@ -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 ();
}