diff --git a/src/hb-subset-cff-common.cc b/src/hb-subset-cff-common.cc index 96e148afc..57206c608 100644 --- a/src/hb-subset-cff-common.cc +++ b/src/hb-subset-cff-common.cc @@ -49,6 +49,7 @@ bool hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int fdCount, const FDSelect &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 */, @@ -83,7 +84,13 @@ hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, /* fonttools retains FDSelect & font dicts for missing glyphs. do the same */ old_glyph = gid; } - auto fd_range = src.get_fd_range (old_glyph); + /* FDSelect may only cover the CharStrings glyphs; see subset_get_fd (). */ + hb_pair_t fd_range = + likely (old_glyph < src_num_glyphs) ? + src.get_fd_range (old_glyph) : + hb_pair_t {0, HB_CODEPOINT_INVALID}; + if (old_glyph < src_num_glyphs) + fd_range.second = hb_min (fd_range.second, src_num_glyphs); unsigned fd = fd_range.first; if (fd != prev_fd) diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 9f8883a01..7ae3e3ea1 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -34,6 +34,16 @@ namespace CFF { +/* CFF2 CharStrings may omit a tail of glyphs provided by VARC, in which + * case FDSelect only covers the CharStrings glyphs. Use FD 0 for glyphs + * beyond that. */ +template +static inline unsigned +subset_get_fd (const ACC &acc, hb_codepoint_t glyph) +{ + return likely (glyph < acc.num_glyphs) ? acc.fdSelect->get_fd (glyph) : 0; +} + /* Used for writing a temporary charstring */ struct str_encoder_t { @@ -378,7 +388,7 @@ struct subr_flattener_t continue; } const hb_ubytes_t str = (*acc.charStrings)[glyph]; - unsigned int fd = acc.fdSelect->get_fd (glyph); + unsigned int fd = subset_get_fd (acc, glyph); if (unlikely (fd >= acc.fdCount)) return false; @@ -869,7 +879,7 @@ struct subr_subsetter_t hb_codepoint_t old_glyph = _.second; const hb_ubytes_t str = (*acc.charStrings)[old_glyph]; - unsigned int fd = acc.fdSelect->get_fd (old_glyph); + unsigned int fd = subset_get_fd (acc, old_glyph); if (unlikely (fd >= acc.fdCount)) return false; @@ -972,7 +982,7 @@ struct subr_subsetter_t } last++; // Skip over gid - unsigned int fd = acc.fdSelect->get_fd (old_glyph); + unsigned int fd = subset_get_fd (acc, old_glyph); if (unlikely (fd >= acc.fdCount)) return false; if (unlikely (!encode_str (get_parsed_charstring (gid), fd, buffArray.arrayZ[gid], encode_prefix))) @@ -1156,7 +1166,7 @@ struct subr_subsetter_t { hb_codepoint_t new_glyph = _.first; hb_codepoint_t old_glyph = _.second; - unsigned int fd = acc.fdSelect->get_fd (old_glyph); + unsigned int fd = subset_get_fd (acc, old_glyph); if (unlikely (fd >= acc.fdCount)) return false; @@ -1315,6 +1325,7 @@ HB_INTERNAL bool hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int fdCount, const CFF::FDSelect &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 */, diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index fee2fb933..505c4bdc5 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -677,6 +677,7 @@ struct cff1_subset_plan if (unlikely (!hb_plan_subset_cff_fdselect (plan, orig_fdcount, *acc.fdSelect, + acc.num_glyphs, subset_fdcount, info.fd_select.size, subset_fdselect_format, diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 6fc62124a..c56d06e79 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -1141,7 +1141,7 @@ struct cff2_subset_plan if (!plan->old_gid_for_new_gid (i, &glyph)) continue; const hb_ubytes_t str = (*acc.charStrings)[glyph]; - unsigned int fd = acc.fdSelect->get_fd (glyph); + unsigned int fd = CFF::subset_get_fd (acc, glyph); if (unlikely (fd >= acc.fdCount)) return false; used_fds.add (fd); @@ -1183,7 +1183,7 @@ struct cff2_subset_plan if (!plan->old_gid_for_new_gid (i, &glyph)) continue; const hb_ubytes_t str = (*acc.charStrings)[glyph]; - unsigned int fd = acc.fdSelect->get_fd (glyph); + unsigned int fd = CFF::subset_get_fd (acc, glyph); if (unlikely (fd >= acc.fdCount)) return false; @@ -1253,6 +1253,7 @@ struct cff2_subset_plan if (unlikely (!hb_plan_subset_cff_fdselect (plan, orig_fdcount, *(const FDSelect *)acc.fdSelect, + acc.num_glyphs, subset_fdcount, subset_fdselect_size, subset_fdselect_format, diff --git a/test/fuzzing/fonts/cff2-varc-short-fdselect0-oob.otf b/test/fuzzing/fonts/cff2-varc-short-fdselect0-oob.otf new file mode 100644 index 000000000..3a08acc04 Binary files /dev/null and b/test/fuzzing/fonts/cff2-varc-short-fdselect0-oob.otf differ