From a61e59716ffd58c15f664ada028b7532433d879b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 15:30:22 -0600 Subject: [PATCH 1/3] [subset/cff1] Remap Encoding supplement SIDs through sidmap cff1_subset_plan renumbers every custom SID it emits through sidmap (charset via plan_subset_charset, Top DICT names via reassignSIDs, FD names via sidmap[]), and the output String INDEX is serialized from that map. plan_subset_encoding was the one emitter that pushed the source font's SID unchanged into subset_enc_supp_codes, so once an earlier custom-named glyph was dropped a supplement code named the wrong glyph, or a string past the end of the renumbered String INDEX. Look the SID up in sidmap where the supplement entry is recorded. The source SID is still used for get_supplement_codes(), which searches the source Encoding. This is safe on every path that reaches it: subset_encoding implies a non-CID font, so plan_subset_charset has already added each visited glyph's name (sidmap.add is idempotent), and when the charset is predefined and plan_subset_charset is skipped all glyph SIDs are standard strings, which sidmap[] returns unchanged. Adds test_subset_cff1_encoding_supplement with a generated CFF font (custom names, format-0 Encoding with supplement 98 -> 'second') and its expected B,C subset; the test fails before this change. Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-JORVXUQZ. Co-Authored-By: Claude Opus 4.8 --- src/hb-subset-cff1.cc | 3 +++ .../fonts/cff1_encoding_supplement.42,43.otf | Bin 0 -> 200 bytes test/api/fonts/cff1_encoding_supplement.otf | Bin 0 -> 212 bytes test/api/test-subset-cff1.c | 23 ++++++++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 test/api/fonts/cff1_encoding_supplement.42,43.otf create mode 100644 test/api/fonts/cff1_encoding_supplement.otf diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index d4213383c..96b5c9a62 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -479,6 +479,9 @@ struct cff1_subset_plan { hb_codepoint_t sid = acc.glyph_to_sid (old_glyph, &glyph_to_sid_cache); encoding->get_supplement_codes (sid, supp_codes); + /* Supplement entries name the glyph by SID; emit the renumbered one + * (plan_subset_charset () already added all glyph names to sidmap). */ + sid = sidmap[sid]; for (unsigned int i = 0; i < supp_codes.length; i++) subset_enc_supp_codes.push (code_pair_t {supp_codes[i], sid}); } diff --git a/test/api/fonts/cff1_encoding_supplement.42,43.otf b/test/api/fonts/cff1_encoding_supplement.42,43.otf new file mode 100644 index 0000000000000000000000000000000000000000..fa3889e3f504e05fedbc42bc73c91ef24f00d724 GIT binary patch literal 200 zcmW-ZOA5k35Jampqi#Zst|R0C9w7@!B)AYF7~JP0=ps7!!>ucC;B`EN2O4VvUBxT9 zx{I+*#uz4II literal 0 HcmV?d00001 diff --git a/test/api/fonts/cff1_encoding_supplement.otf b/test/api/fonts/cff1_encoding_supplement.otf new file mode 100644 index 0000000000000000000000000000000000000000..b81854993bcb4faad04863e5c56471f291122353 GIT binary patch literal 212 zcmeYd3GruOW&i_cH#Y?ci@^p+rzGbl7C`w6fV56-Vg-zU2FPb)WME-rU}R)u4K6Jx z05Rlbfx;gAAktp|L`DdLNCps*BE-PV$jHVcn3h>oTvD8xoS&Ccl95@I0+eHHXY63? zY+!bDa%N0o>;M|e$i&RT$_E4>S)ltEfiw>T3xfiXW&uJF-}(Q4AnEx3|NjGEK9Cat Hl4Sq@s}~)h literal 0 HcmV?d00001 diff --git a/test/api/test-subset-cff1.c b/test/api/test-subset-cff1.c index 0d7156320..6d836e152 100644 --- a/test/api/test-subset-cff1.c +++ b/test/api/test-subset-cff1.c @@ -316,6 +316,28 @@ test_subset_cff1_seac_precheck (void) } } +static void +test_subset_cff1_encoding_supplement (void) +{ + /* Dropping "first" renumbers the custom glyph-name SIDs; the Encoding + * supplement (code 98 -> "second") must follow them. */ + hb_face_t *face = hb_test_open_font_file ("fonts/cff1_encoding_supplement.otf"); + hb_face_t *face_subset = hb_test_open_font_file ("fonts/cff1_encoding_supplement.42,43.otf"); + hb_face_t *face_test; + + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, 0x42); /* B */ + hb_set_add (codepoints, 0x43); /* C */ + face_test = hb_subset_test_create_subset (face, hb_subset_test_create_input (codepoints)); + hb_set_destroy (codepoints); + + hb_subset_test_check (face_subset, face_test, HB_TAG ('C','F','F',' ')); + + hb_face_destroy (face_test); + hb_face_destroy (face_subset); + hb_face_destroy (face); +} + static void test_subset_cff1_dotsection (void) { @@ -646,6 +668,7 @@ main (int argc, char **argv) hb_test_add (test_subset_cff1_seac); hb_test_add (test_subset_cff1_cid_seac); hb_test_add (test_subset_cff1_seac_precheck); + hb_test_add (test_subset_cff1_encoding_supplement); hb_test_add (test_subset_cff1_dotsection); hb_test_add (test_subset_cff1_retaingids); hb_test_add (test_subset_cff1_j_retaingids); From fb58fbe2596e158eb1835d9d0168c1aac3a09f9c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 15:33:37 -0600 Subject: [PATCH 2/3] [subset/cff2] Plan FDSelect from the real CFF2FDSelect type cff2_subset_plan::create() cast acc.fdSelect (a CFF2FDSelect) to the CFF1 FDSelect type for hb_plan_subset_cff_fdselect(), whose get_fd_range() dispatch knows only formats 0 and 3. A CFF2-only format-4 FDSelect therefore reported FD 0 for every glyph, so the planner kept only Font DICT 0: every retained glyph belonging to another Font DICT lost that DICT's Private DICT and local subrs, and when it called a local subr it either lost its outline or drew through FD 0's subrs with the wrong shape. The call still succeeded, writing a single-Font-DICT font. Format 4 is the only FDSelect format with a 16-bit FD index, so every CFF2 font with more than 256 Font DICTs is affected. The planner only needs get_fd_range() and format, so give CFF2FDSelect its own get_fd_range() dispatching formats 0, 3 and 4, make hb_plan_subset_cff_fdselect a template over the FDSelect type with explicit instantiations for CFF1FDSelect and CFF2FDSelect (same pattern as other explicitly instantiated subset helpers), and drop the cast. The existing format-4 writer in hb_serialize_cff_fdselect is now reachable. Because a CFF2 font with >256 retained FDs can now legitimately plan format 4, serialize_cff2_to_cff1() rejects that case (CFF1 has no format 4) instead of emitting an invalid CFF table; with fewer FDs the downgrade is unchanged. Adds test_subset_cff2_fdselect_format4, which subsets the format-3 and format-4 variants of a two-Font-DICT font and requires identical CFF2 output and glyph 2's extents; it fails before this change. Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-7RSDR0F0. Co-Authored-By: Claude Opus 4.8 --- src/hb-ot-cff2-table.hh | 13 +++++++ src/hb-subset-cff-common.cc | 26 ++++++++++++- src/hb-subset-cff-common.hh | 3 +- src/hb-subset-cff2.cc | 6 ++- test/api/test-subset-cff2.c | 74 +++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) diff --git a/src/hb-ot-cff2-table.hh b/src/hb-ot-cff2-table.hh index 18440c51e..6c418bdc6 100644 --- a/src/hb-ot-cff2-table.hh +++ b/src/hb-ot-cff2-table.hh @@ -82,6 +82,19 @@ struct CFF2FDSelect default:return 0; } } + /* Returns pair of fd and one after last glyph in range. */ + hb_pair_t get_fd_range (hb_codepoint_t glyph) const + { + if (this == &Null (CFF2FDSelect)) return {0, 1}; + + switch (format) + { + case 0: hb_barrier (); return u.format0.get_fd_range (glyph); + case 3: hb_barrier (); return u.format3.get_fd_range (glyph); + case 4: hb_barrier (); return u.format4.get_fd_range (glyph); + default:return {0, 1}; + } + } bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount, unsigned int charstring_count = HB_CODEPOINT_INVALID) const diff --git a/src/hb-subset-cff-common.cc b/src/hb-subset-cff-common.cc index 57206c608..108bba974 100644 --- a/src/hb-subset-cff-common.cc +++ b/src/hb-subset-cff-common.cc @@ -29,6 +29,7 @@ #ifndef HB_NO_SUBSET_CFF #include "hb-ot-cff-common.hh" +#include "hb-ot-cff1-table.hh" #include "hb-ot-cff2-table.hh" #include "hb-subset-cff-common.hh" @@ -45,10 +46,11 @@ using namespace CFF; * along with a font index remapping table */ +template bool hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int fdCount, - const FDSelect &src, /* IN */ + const FDSELECT &src, /* IN */ unsigned int src_num_glyphs, unsigned int &subset_fd_count /* OUT */, unsigned int &subset_fdselect_size /* OUT */, @@ -160,6 +162,28 @@ hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, return true; } +template bool +hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, + unsigned int fdCount, + const CFF1FDSelect &src, /* IN */ + unsigned int src_num_glyphs, + unsigned int &subset_fd_count /* OUT */, + unsigned int &subset_fdselect_size /* OUT */, + unsigned int &subset_fdselect_format /* OUT */, + hb_vector_t &fdselect_ranges /* OUT */, + hb_inc_bimap_t &fdmap /* OUT */); + +template bool +hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, + unsigned int fdCount, + const CFF2FDSelect &src, /* IN */ + unsigned int src_num_glyphs, + unsigned int &subset_fd_count /* OUT */, + unsigned int &subset_fdselect_size /* OUT */, + unsigned int &subset_fdselect_format /* OUT */, + hb_vector_t &fdselect_ranges /* OUT */, + hb_inc_bimap_t &fdmap /* OUT */); + template static inline bool serialize_fdselect_3_4 (hb_serialize_context_t *c, diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 5f355a2f9..5cbd2774e 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -1382,10 +1382,11 @@ struct subr_subsetter_t } /* namespace CFF */ +template HB_INTERNAL bool hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int fdCount, - const CFF::FDSelect &src, /* IN */ + const FDSELECT &src, /* IN */ unsigned int src_num_glyphs, unsigned int &subset_fd_count /* OUT */, unsigned int &subset_fdselect_size /* OUT */, diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 33218fc66..876384fa2 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -1330,7 +1330,7 @@ struct cff2_subset_plan { if (unlikely (!hb_plan_subset_cff_fdselect (plan, orig_fdcount, - *(const FDSelect *)acc.fdSelect, + *acc.fdSelect, acc.num_glyphs, subset_fdcount, subset_fdselect_size, @@ -1645,6 +1645,10 @@ serialize_cff2_to_cff1 (hb_serialize_context_t *c, // CFF2 makes it optional when there's only one FD if (acc.fdSelect != &Null (CFF2FDSelect)) { + // CFF1 has no FDSelect format 4; more than 256 FDs cannot be represented + if (unlikely (plan.subset_fdselect_format == 4)) + return_trace (false); + c->push (); if (likely (hb_serialize_cff_fdselect (c, plan.num_glyphs, *(const FDSelect *)acc.fdSelect, diff --git a/test/api/test-subset-cff2.c b/test/api/test-subset-cff2.c index 1b6a3cd2e..57da4b343 100644 --- a/test/api/test-subset-cff2.c +++ b/test/api/test-subset-cff2.c @@ -161,6 +161,79 @@ test_subset_cff2_retaingids (void) hb_face_destroy (face_ac); } +static void +test_subset_cff2_fdselect_format4 (void) +{ + /* Three glyphs and two Font DICTs: glyphs 0-1 use FD 0, glyph 2 uses FD 1 and + * draws one line through FD 1's local subr 0. The two tables differ only in + * the FDSelect encoding: format 3, or the CFF2-only format 4. */ + static const char maxp_data[] = {0, 0, 0x50, 0, 0, 3}; + static const char cff2_fdselect3_data[] = { + 0x02, 0, 0x05, 0, 0x14, /* CFF2 header */ + 0x1D, 0, 0, 0, 0x1D, 0x11, 0x1D, 0, 0, 0, 0x6D, 0x0C, 0x24, 0x1D, 0, 0, 0, 0x62, 0x0C, 0x25, /* Top DICT: CharStrings, FDArray, FDSelect */ + 0, 0, 0, 0, /* Global Subrs INDEX (empty) */ + 0, 0, 0, 0x03, 0x01, 0x01, 0x16, 0x2B, 0x3D, /* CharStrings INDEX */ + 0x1C, 0, 0, 0x1C, 0, 0, 0x15, 0x1C, 0, 0x0A, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0, 0x0A, 0x05, /* .notdef */ + 0x1C, 0, 0x32, 0x1C, 0, 0, 0x15, 0x1C, 0x01, (char) 0xC2, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0x02, (char) 0xBC, 0x05, /* glyph 1 */ + 0x1C, 0, 0x64, 0x1C, (char) 0xFF, 0x38, 0x15, 0x1C, (char) 0xFF, (char) 0x95, 0x0A, 0x1C, 0, 0, 0x1C, 0x01, 0x2C, 0x05, /* glyph 2: calls FD 1's local subr 0 */ + 0x03, 0, 0x02, 0, 0, 0, 0, 0x02, 0x01, 0, 0x03, /* FDSelect format 3: glyphs 0-1 -> FD 0, glyph 2 -> FD 1 */ + 0, 0, 0, 0x02, 0x01, 0x01, 0x0A, 0x13, /* FDArray INDEX */ + 0x1C, 0, 0x04, 0x1D, 0, 0, 0, (char) 0x87, 0x12, /* Font DICT 0 */ + 0x1C, 0, 0x0A, 0x1D, 0, 0, 0, (char) 0x8B, 0x12, /* Font DICT 1 */ + 0x1C, 0, 0x32, 0x0A, /* Private DICT 0 */ + 0x1C, 0, 0x50, 0x0A, 0x1D, 0, 0, 0, 0x0A, 0x13, /* Private DICT 1: Subrs */ + 0, 0, 0, 0x01, 0x01, 0x01, 0x08, 0x1C, 0, 0x64, 0x1C, 0, 0x64, 0x05, /* FD 1 local Subrs INDEX */ + }; + static const char cff2_fdselect4_data[] = { + 0x02, 0, 0x05, 0, 0x14, /* CFF2 header */ + 0x1D, 0, 0, 0, 0x1D, 0x11, 0x1D, 0, 0, 0, 0x77, 0x0C, 0x24, 0x1D, 0, 0, 0, 0x62, 0x0C, 0x25, /* Top DICT: CharStrings, FDArray, FDSelect */ + 0, 0, 0, 0, /* Global Subrs INDEX (empty) */ + 0, 0, 0, 0x03, 0x01, 0x01, 0x16, 0x2B, 0x3D, /* CharStrings INDEX */ + 0x1C, 0, 0, 0x1C, 0, 0, 0x15, 0x1C, 0, 0x0A, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0, 0x0A, 0x05, /* .notdef */ + 0x1C, 0, 0x32, 0x1C, 0, 0, 0x15, 0x1C, 0x01, (char) 0xC2, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0x02, (char) 0xBC, 0x05, /* glyph 1 */ + 0x1C, 0, 0x64, 0x1C, (char) 0xFF, 0x38, 0x15, 0x1C, (char) 0xFF, (char) 0x95, 0x0A, 0x1C, 0, 0, 0x1C, 0x01, 0x2C, 0x05, /* glyph 2: calls FD 1's local subr 0 */ + 0x04, 0, 0, 0, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02, 0, 0x01, 0, 0, 0, 0x03, /* FDSelect format 4: glyphs 0-1 -> FD 0, glyph 2 -> FD 1 */ + 0, 0, 0, 0x02, 0x01, 0x01, 0x0A, 0x13, /* FDArray INDEX */ + 0x1C, 0, 0x04, 0x1D, 0, 0, 0, (char) 0x91, 0x12, /* Font DICT 0 */ + 0x1C, 0, 0x0A, 0x1D, 0, 0, 0, (char) 0x95, 0x12, /* Font DICT 1 */ + 0x1C, 0, 0x32, 0x0A, /* Private DICT 0 */ + 0x1C, 0, 0x50, 0x0A, 0x1D, 0, 0, 0, 0x0A, 0x13, /* Private DICT 1: Subrs */ + 0, 0, 0, 0x01, 0x01, 0x01, 0x08, 0x1C, 0, 0x64, 0x1C, 0, 0x64, 0x05, /* FD 1 local Subrs INDEX */ + }; + + hb_face_t *face_fdselect3 = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face_fdselect3, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face_fdselect3, "CFF2", cff2_fdselect3_data); + + hb_face_t *face_fdselect4 = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face_fdselect4, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face_fdselect4, "CFF2", cff2_fdselect4_data); + + hb_set_t *glyphs = hb_set_create (); + hb_face_t *face_fdselect3_subset, *face_fdselect4_subset; + hb_set_add_range (glyphs, 0, 2); + face_fdselect3_subset = hb_subset_test_create_subset (face_fdselect3, hb_subset_test_create_input_from_glyphs (glyphs)); + face_fdselect4_subset = hb_subset_test_create_subset (face_fdselect4, hb_subset_test_create_input_from_glyphs (glyphs)); + hb_set_destroy (glyphs); + + /* Both retain FD 1; the format-4 source must not have been planned as FD 0 only. */ + hb_subset_test_check (face_fdselect3_subset, face_fdselect4_subset, HB_TAG ('C','F','F','2')); + + hb_font_t *font = hb_font_create (face_fdselect4_subset); + hb_glyph_extents_t extents; + g_assert_true (hb_font_get_glyph_extents (font, 2, &extents)); + g_assert_cmpint (extents.x_bearing, ==, 100); + g_assert_cmpint (extents.y_bearing, ==, 200); + g_assert_cmpint (extents.width, ==, 100); + g_assert_cmpint (extents.height, ==, -400); + hb_font_destroy (font); + + hb_face_destroy (face_fdselect4_subset); + hb_face_destroy (face_fdselect3_subset); + hb_face_destroy (face_fdselect4); + hb_face_destroy (face_fdselect3); +} + #ifdef HB_EXPERIMENTAL_API static void test_subset_cff2_iftb_requirements (void) @@ -469,6 +542,7 @@ main (int argc, char **argv) hb_test_add (test_subset_cff2_desubr); hb_test_add (test_subset_cff2_desubr_strip_hints); hb_test_add (test_subset_cff2_retaingids); + hb_test_add (test_subset_cff2_fdselect_format4); #ifdef HB_EXPERIMENTAL_API hb_test_add (test_subset_cff2_iftb_requirements); From 051a12a37a4df047d66e11578bc5dfbdd8ce5188 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 15:35:06 -0600 Subject: [PATCH 3/3] [cff] Return the sentinel as the end of the last FDSelect range FDSelect::get_fd_range() is documented to return "one after last glyph in range", but for the last format-3/4 range FDSelect3_4::get_fd_range() returned the range's first glyph (62f5ed461 changed it from 0 to that value to stop an infinite loop, but it is still below every glyph in the range). hb_plan_subset_cff_fdselect() assigns that end to its loop counter, so with a custom old-to-new glyph map the loop re-walks the last range once per mapped glyph, making CFF1/CFF2 subsetting quadratic. The correct end is the FDSelect sentinel: sanitize() guarantees sentinel > ranges[last].first and sentinel >= num_glyphs (== num_glyphs for CFF1, >= CharStrings count for CFF2), and the caller only queries old_glyph < src_num_glyphs and clamps the end to src_num_glyphs, so the counter now only ever moves forward. Fixing the accessor (rather than guarding the loop) keeps the one place that establishes the documented contract; FDSelect0::get_fd_range() already returns glyph + 1, and the format-4 instantiation is covered by the same template. Adds an assertion to the existing test_fd_select_sentinel() in src/test-cff.cc; it fails before this change. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CGK397GA. Co-Authored-By: Claude Opus 4.8 --- src/hb-ot-cff-common.hh | 2 +- src/test-cff.cc | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hb-ot-cff-common.hh b/src/hb-ot-cff-common.hh index 04222eaf3..28b227f97 100644 --- a/src/hb-ot-cff-common.hh +++ b/src/hb-ot-cff-common.hh @@ -280,7 +280,7 @@ struct FDSelect3_4 { auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range); unsigned fd = range ? range->fd : ranges[nRanges () - 1].fd; - hb_codepoint_t end = range ? range[1].first : ranges[nRanges () - 1].first; + hb_codepoint_t end = range ? range[1].first : sentinel (); return {fd, end}; } diff --git a/src/test-cff.cc b/src/test-cff.cc index 16e5e90fc..95089e3ec 100644 --- a/src/test-cff.cc +++ b/src/test-cff.cc @@ -150,6 +150,8 @@ test_fd_select_sentinel () select->sentinel () = 4; hb_always_assert (select->sanitize (&c, 1, 3)); hb_always_assert (select->sanitize (&c, 1)); + /* The last range ends at the sentinel. */ + hb_always_assert (select->get_fd_range (2).second == 4); select->sentinel () = 2; hb_always_assert (!select->sanitize (&c, 1, 3)); select->sentinel () = 5;