diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index 05394038a..39da8fa7a 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -365,64 +365,66 @@ template struct Charset1_2 { inline bool sanitize (hb_sanitize_context_t *c, unsigned int num_glyphs) const { TRACE_SANITIZE (this); if (unlikely (!c->check_struct (this))) return_trace (false); num_glyphs--; for (unsigned int i = 0; num_glyphs > 0; i++) { if (unlikely (!ranges[i].sanitize (c) || (num_glyphs < ranges[i].nLeft + 1))) return_trace (false); num_glyphs -= (ranges[i].nLeft + 1); } return_trace (true); } inline hb_codepoint_t get_sid (hb_codepoint_t glyph) const { if (glyph == 0) return 0; glyph--; for (unsigned int i = 0;; i++) { if (glyph <= ranges[i].nLeft) return (hb_codepoint_t)ranges[i].first + glyph; glyph -= (ranges[i].nLeft + 1); } return 0; } - inline hb_codepoint_t get_glyph (hb_codepoint_t sid) const + inline hb_codepoint_t get_glyph (hb_codepoint_t sid, unsigned int num_glyphs) const { if (sid == 0) return 0; hb_codepoint_t glyph = 1; for (unsigned int i = 0;; i++) { - if ((ranges[i].first <= sid) && sid <= ranges[i].first + ranges[i].nLeft) + if (glyph >= num_glyphs) + return 0; + if ((ranges[i].first <= sid) && (sid <= ranges[i].first + ranges[i].nLeft)) return glyph + (sid - ranges[i].first); glyph += (ranges[i].nLeft + 1); } return 0; } inline unsigned int get_size (unsigned int num_glyphs) const { unsigned int size = HBUINT8::static_size; int glyph = (int)num_glyphs; assert (glyph > 0); glyph--; for (unsigned int i = 0; glyph > 0; i++) { glyph -= (ranges[i].nLeft + 1); size += Charset_Range::static_size; } return size; } Charset_Range ranges[VAR]; DEFINE_SIZE_ARRAY (0, ranges); }; @@ -435,132 +437,132 @@ typedef Charset_Range Charset2_Range; struct Charset { inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); if (unlikely (!c->check_struct (this))) return_trace (false); if (format == 0) return_trace (u.format0.sanitize (c, c->get_num_glyphs ())); else if (format == 1) return_trace (u.format1.sanitize (c, c->get_num_glyphs ())); else if (likely (format == 2)) return_trace (u.format2.sanitize (c, c->get_num_glyphs ())); else return_trace (false); } /* serialize a fullset Charset */ inline bool serialize (hb_serialize_context_t *c, const Charset &src, unsigned int num_glyphs) { TRACE_SERIALIZE (this); unsigned int size = src.get_size (num_glyphs); Charset *dest = c->allocate_size (size); if (unlikely (dest == nullptr)) return_trace (false); memcpy (dest, &src, size); return_trace (true); } /* serialize a subset Charset */ inline bool serialize (hb_serialize_context_t *c, uint8_t format, unsigned int num_glyphs, const hb_vector_t& sid_ranges) { TRACE_SERIALIZE (this); Charset *dest = c->extend_min (*this); if (unlikely (dest == nullptr)) return_trace (false); dest->format.set (format); if (format == 0) { Charset0 *fmt0 = c->allocate_size (Charset0::min_size + HBUINT16::static_size * (num_glyphs - 1)); if (unlikely (fmt0 == nullptr)) return_trace (false); unsigned int glyph = 0; for (unsigned int i = 0; i < sid_ranges.len; i++) { hb_codepoint_t sid = sid_ranges[i].code; for (int left = (int)sid_ranges[i].glyph; left >= 0; left--) fmt0->sids[glyph++].set (sid++); } } else if (format == 1) { Charset1 *fmt1 = c->allocate_size (Charset1::min_size + Charset1_Range::static_size * sid_ranges.len); if (unlikely (fmt1 == nullptr)) return_trace (false); for (unsigned int i = 0; i < sid_ranges.len; i++) { assert (sid_ranges[i].glyph <= 0xFF); fmt1->ranges[i].first.set (sid_ranges[i].code); fmt1->ranges[i].nLeft.set (sid_ranges[i].glyph); } } else /* format 2 */ { Charset2 *fmt2 = c->allocate_size (Charset2::min_size + Charset2_Range::static_size * sid_ranges.len); if (unlikely (fmt2 == nullptr)) return_trace (false); for (unsigned int i = 0; i < sid_ranges.len; i++) { assert (sid_ranges[i].glyph <= 0xFFFF); fmt2->ranges[i].first.set (sid_ranges[i].code); fmt2->ranges[i].nLeft.set (sid_ranges[i].glyph); } } return_trace (true); } /* parallel to above: calculate the size of a subset Charset */ static inline unsigned int calculate_serialized_size ( uint8_t format, unsigned int count) { unsigned int size = min_size; if (format == 0) size += Charset0::min_size + HBUINT16::static_size * (count - 1); else if (format == 1) size += Charset1::min_size + Charset1_Range::static_size * count; else size += Charset2::min_size + Charset2_Range::static_size * count; return size; } inline unsigned int get_size (unsigned int num_glyphs) const { unsigned int size = min_size; if (format == 0) size += u.format0.get_size (num_glyphs); else if (format == 1) size += u.format1.get_size (num_glyphs); else size += u.format2.get_size (num_glyphs); return size; } inline hb_codepoint_t get_sid (hb_codepoint_t glyph) const { if (format == 0) return u.format0.get_sid (glyph); else if (format == 1) return u.format1.get_sid (glyph); else return u.format2.get_sid (glyph); } inline hb_codepoint_t get_glyph (hb_codepoint_t sid, unsigned int num_glyphs) const { if (format == 0) return u.format0.get_glyph (sid, num_glyphs); else if (format == 1) - return u.format1.get_glyph (sid); + return u.format1.get_glyph (sid, num_glyphs); else - return u.format2.get_glyph (sid); + return u.format2.get_glyph (sid, num_glyphs); } HBUINT8 format; union { Charset0 format0; Charset1 format1; Charset2 format2; } u; DEFINE_SIZE_MIN (1); }; diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-shape-fuzzer-5700264032468992 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-shape-fuzzer-5700264032468992 new file mode 100644 index 000000000..82a462bc4 Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-shape-fuzzer-5700264032468992 differ