[subset] Use CFF1 Encoding format 1 for more than 255 codes (#6460)

A format 1 Encoding can give codes to more than 255 glyphs, since
codes may repeat across ranges. When the subset keeps more than 255
of those glyphs, as with retain-gids, plan_subset_encoding() hit
assert (subset_enc_num_codes <= 0xFF).

Choose format 0 only when it can hold the codes, and fail cleanly in
the format 1 serializer instead of truncating nRanges past 255.

Add a test font whose format 1 Encoding covers 768 glyphs, and the
reporter's font to the fuzzing corpus.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6031
Assisted-by: Claude
Assisted-by: OpenAI Codex
This commit is contained in:
Beleniuc Ilie authored and GitHub committed 2026-10-29 16:35:22 -06:00
1 parent c305720f01
commit 4868fd314f
6 files changed
+29 -2

No files matched your search

+1
View File
@@ -205,6 +205,7 @@ struct Encoding
case 1:
{
if (unlikely (code_ranges.length > 0xFF)) return_trace (false);
Encoding1 *fmt1 = c->allocate_size<Encoding1> (Encoding1::min_size + Encoding1_Range::static_size * code_ranges.length);
if (unlikely (!fmt1)) return_trace (false);
fmt1->nRanges () = code_ranges.length;
+3 -2
View File
@@ -487,11 +487,12 @@ struct cff1_subset_plan
subset_enc_code_ranges.complete (glyph);
assert (subset_enc_num_codes <= 0xFF);
size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.length;
if (size0 < size1)
/* Format 0 holds at most 255 codes; a format 1 source encoding
* can give codes to more glyphs than that. */
if (subset_enc_num_codes <= 0xFF && size0 < size1)
subset_enc_format = 0;
else
subset_enc_format = 1;
Binary file not shown.
+25
View File
@@ -339,6 +339,30 @@ test_subset_cff1_retaingids (void)
hb_face_destroy (face_ac);
}
static void
test_subset_cff1_encoding_many_codes_retaingids (void)
{
/* A format 1 encoding gives codes to 768 glyphs; with retain-gids
* the subset keeps more than 255 of them, too many for format 0. */
hb_face_t *face = hb_test_open_font_file ("fonts/cff1_encoding_many_codes.otf");
hb_face_t *face_expected = hb_test_open_font_file ("fonts/cff1_encoding_many_codes.300.retaingids.otf");
hb_set_t *glyphs = hb_set_create ();
hb_subset_input_t *input;
hb_face_t *face_subset;
hb_set_add (glyphs, 300);
input = hb_subset_test_create_input_from_glyphs (glyphs);
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_RETAIN_GIDS);
face_subset = hb_subset_test_create_subset (face, input);
hb_set_destroy (glyphs);
hb_subset_test_check (face_expected, face_subset, HB_TAG ('C','F','F',' '));
hb_face_destroy (face_subset);
hb_face_destroy (face);
hb_face_destroy (face_expected);
}
static void
test_subset_cff1_j_retaingids (void)
{
@@ -489,6 +513,7 @@ main (int argc, char **argv)
hb_test_add (test_subset_cff1_dotsection);
hb_test_add (test_subset_cff1_retaingids);
hb_test_add (test_subset_cff1_j_retaingids);
hb_test_add (test_subset_cff1_encoding_many_codes_retaingids);
hb_test_add (test_subset_cff1_identity_charset);
hb_test_add (test_subset_cff1_j_identity_charset);