diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index e8c73ffaa..c3b5808eb 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -525,7 +525,13 @@ struct ColorLine hb_paint_extend_t get_extend () const { - return (hb_paint_extend_t) (unsigned int) extend; + switch ((unsigned int) extend) + { + case Extend::EXTEND_REPEAT: return HB_PAINT_EXTEND_REPEAT; + case Extend::EXTEND_REFLECT: return HB_PAINT_EXTEND_REFLECT; + case Extend::EXTEND_PAD: + default: return HB_PAINT_EXTEND_PAD; + } } HB_INTERNAL static hb_paint_extend_t static_get_extend (hb_color_line_t *color_line, diff --git a/src/OT/Color/svg/svg.hh b/src/OT/Color/svg/svg.hh index d6fb71832..9f15dda84 100644 --- a/src/OT/Color/svg/svg.hh +++ b/src/OT/Color/svg/svg.hh @@ -432,8 +432,10 @@ parse_cache_entries_linear (const char *svg, { open_elem_t stack[MAX_DEPTH]; unsigned depth = 0; - defs_entries->alloc (256); - id_entries->alloc (256); + if (unlikely (!defs_entries->alloc (256) || + !glyph_spans->alloc (256) || + !id_entries->alloc (256))) + return false; unsigned defs_depth = 0; unsigned i = 0; @@ -516,30 +518,24 @@ parse_cache_entries_linear (const char *svg, if (e.id.len) { - auto *id_slot = id_entries->push (); - if (unlikely (!id_slot)) + if (unlikely (!id_entries->push_or_fail (id_entry_t {e.id, (uint32_t) e.start, (uint32_t) end}))) return false; - *id_slot = {e.id, (uint32_t) e.start, (uint32_t) end}; if (e.in_defs_content) { - auto *slot = defs_entries->push (); - if (unlikely (!slot)) + if (unlikely (!defs_entries->push_or_fail ())) return false; - slot->id = e.id; - slot->start = e.start; - slot->end = end; + auto &slot = defs_entries->tail (); + slot.id = e.id; + slot.start = e.start; + slot.end = end; } hb_codepoint_t gid; if (parse_glyph_id_span (e.id, &gid)) { - auto *span = glyph_spans->push (); - if (unlikely (!span)) + if (unlikely (!glyph_spans->push_or_fail (glyph_entry_t {gid, (uint32_t) e.start, (uint32_t) end}))) return false; - span->glyph = gid; - span->start = (uint32_t) e.start; - span->end = (uint32_t) end; } } @@ -564,30 +560,24 @@ parse_cache_entries_linear (const char *svg, unsigned end = gt + 1; if (e.id.len) { - auto *id_slot = id_entries->push (); - if (unlikely (!id_slot)) + if (unlikely (!id_entries->push_or_fail (id_entry_t {e.id, (uint32_t) e.start, (uint32_t) end}))) return false; - *id_slot = {e.id, (uint32_t) e.start, (uint32_t) end}; if (e.in_defs_content) { - auto *slot = defs_entries->push (); - if (unlikely (!slot)) + if (unlikely (!defs_entries->push_or_fail ())) return false; - slot->id = e.id; - slot->start = e.start; - slot->end = end; + auto &slot = defs_entries->tail (); + slot.id = e.id; + slot.start = e.start; + slot.end = end; } hb_codepoint_t gid; if (parse_glyph_id_span (e.id, &gid)) { - auto *span = glyph_spans->push (); - if (unlikely (!span)) + if (unlikely (!glyph_spans->push_or_fail (glyph_entry_t {gid, (uint32_t) e.start, (uint32_t) end}))) return false; - span->glyph = gid; - span->start = (uint32_t) e.start; - span->end = (uint32_t) end; } } } @@ -788,6 +778,8 @@ SVG::accelerator_t::doc_cache_get_glyph_span (const svg_doc_cache_t *doc, const auto &span = doc->glyph_spans.arrayZ[glyph - doc->start_glyph]; if (span.first == INVALID_SPAN) return false; + if (unlikely (span.first > span.second || span.second > doc->len)) + return false; if (start) *start = span.first; if (end) *end = span.second; @@ -805,6 +797,8 @@ SVG::accelerator_t::doc_cache_find_id_span (const svg_doc_cache_t *doc, hb_pair_t *span = nullptr; if (!doc->id_spans.has (id, &span)) return false; + if (unlikely (span->first > span->second || span->second > doc->len)) + return false; if (start) *start = span->first; if (end) *end = span->second; return true; diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index 81c133197..269a705c1 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -482,7 +482,8 @@ emit_segment (hb_raster_draw_t *draw, } else { e.xL = X1; e.yL = Y1; e.xH = X0; e.yH = Y0; e.wind = -1; } - e.slope = ((int64_t) (e.xH - e.xL) * (int64_t) (1 << 16)) / (e.yH - e.yL); + e.slope = (((int64_t) e.xH - (int64_t) e.xL) * (int64_t) 65536) / + ((int64_t) e.yH - (int64_t) e.yL); draw->edges.push (e); } @@ -1066,7 +1067,8 @@ edge_sweep_row (int32_t *area, if (total_dx > 0) { /* Left-to-right edge. */ - int32_t x_b = (cx0 + 1) << HB_RASTER_PIXEL_BITS; + int32_t x_b = (int32_t) hb_clamp (((int64_t) cx0 + 1) * HB_RASTER_ONE_PIXEL, + (int64_t) INT32_MIN, (int64_t) INT32_MAX); int32_t fy_b = fy0 + (int32_t) ((int64_t) (x_b - x0) * total_dy / total_dx); cell_add (area, cover, width, cx0 - x_org, fx0, fy0, HB_RASTER_ONE_PIXEL, fy_b, wind, x_min, x_max); @@ -1083,7 +1085,8 @@ edge_sweep_row (int32_t *area, else { /* Right-to-left edge. */ - int32_t x_b = cx0 << HB_RASTER_PIXEL_BITS; + int32_t x_b = (int32_t) hb_clamp ((int64_t) cx0 * HB_RASTER_ONE_PIXEL, + (int64_t) INT32_MIN, (int64_t) INT32_MAX); int32_t fy_b = fy0 + (int32_t) ((int64_t) (x_b - x0) * total_dy / total_dx); cell_add (area, cover, width, cx0 - x_org, fx0, fy0, 0, fy_b, wind, x_min, x_max); @@ -1328,7 +1331,8 @@ hb_raster_draw_render (hb_raster_draw_t *draw) for (unsigned row = 0; row < ext.height; row++) { - int32_t y_top = (ext.y_origin + (int) row) << HB_RASTER_PIXEL_BITS; + int64_t y_top_64 = ((int64_t) ext.y_origin + (int64_t) row) * HB_RASTER_ONE_PIXEL; + int32_t y_top = (int32_t) hb_clamp (y_top_64, (int64_t) INT32_MIN, (int64_t) INT32_MAX); /* Add new edges from this row's bucket. */ draw->active_edges.extend (draw->edge_buckets.arrayZ[row]); diff --git a/src/hb-subset-instancer-solver.cc b/src/hb-subset-instancer-solver.cc index 03c59dc80..c838240cd 100644 --- a/src/hb-subset-instancer-solver.cc +++ b/src/hb-subset-instancer-solver.cc @@ -370,7 +370,8 @@ double renormalizeValue (double v, const Triple &triple, const TripleDistances &triple_distances, bool extrapolate) { double lower = triple.minimum, def = triple.middle, upper = triple.maximum; - assert (lower <= def && def <= upper); + if (unlikely (!(lower <= def && def <= upper))) + return hb_clamp (v, -1.0, +1.0); if (!extrapolate) v = hb_clamp (v, lower, upper); @@ -384,14 +385,26 @@ double renormalizeValue (double v, const Triple &triple, /* default >= 0 and v != default */ if (v > def) - return (v - def) / (upper - def); + { + double positive_range = upper - def; + if (unlikely (!positive_range)) + return +1.0; + return (v - def) / positive_range; + } /* v < def */ if (lower >= 0.0) - return (v - def) / (def - lower); + { + double negative_range = def - lower; + if (unlikely (!negative_range)) + return -1.0; + return (v - def) / negative_range; + } /* lower < 0 and v < default */ double total_distance = triple_distances.negative * (-lower) + triple_distances.positive * def; + if (unlikely (!total_distance)) + return 0.0; double v_distance; if (v >= 0.0) diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index 1853aa3ae..fd04bf211 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -94,8 +94,10 @@ struct hb_vector_paint_t unsigned clip_rect_counter = 0; unsigned gradient_counter = 0; unsigned color_glyph_counter = 0; + unsigned color_glyph_depth = 0; hb_set_t *defined_outlines = nullptr; hb_set_t *defined_clips = nullptr; + hb_set_t *active_color_glyphs = nullptr; hb_hashmap_t defined_color_glyphs; hb_vector_t color_stops_scratch; hb_vector_t subset_body_scratch; @@ -1093,17 +1095,27 @@ hb_vector_paint_color_glyph (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_vector_paint_ensure_initialized (paint))) return false; + if (unlikely (paint->color_glyph_depth >= HB_MAX_NESTING_LEVEL)) + return false; + if (unlikely (hb_set_has (paint->active_color_glyphs, glyph))) + return false; + + paint->color_glyph_depth++; + hb_set_add (paint->active_color_glyphs, glyph); + hb_codepoint_t old_gid = paint->current_svg_image_glyph; hb_face_t *old_face = paint->current_face; paint->current_svg_image_glyph = glyph; paint->current_face = hb_font_get_face (font); hb_font_paint_glyph (font, glyph, - hb_vector_paint_funcs_get (), - paint, - paint->palette, - paint->foreground); + hb_vector_paint_funcs_get (), + paint, + paint->palette, + paint->foreground); paint->current_svg_image_glyph = old_gid; paint->current_face = old_face; + hb_set_del (paint->active_color_glyphs, glyph); + paint->color_glyph_depth--; return true; } @@ -1132,6 +1144,7 @@ hb_vector_paint_create_or_fail (hb_vector_format_t format) paint->defined_outlines = hb_set_create (); paint->defined_clips = hb_set_create (); + paint->active_color_glyphs = hb_set_create (); paint->defs.alloc (4096); paint->path.alloc (2048); paint->subset_body_scratch.alloc (2048); @@ -1174,6 +1187,7 @@ hb_vector_paint_destroy (hb_vector_paint_t *paint) hb_blob_destroy (paint->recycled_blob); hb_set_destroy (paint->defined_outlines); hb_set_destroy (paint->defined_clips); + hb_set_destroy (paint->active_color_glyphs); hb_object_actually_destroy (paint); hb_free (paint); } @@ -1714,12 +1728,14 @@ hb_vector_paint_clear_render_state (hb_vector_paint_t *paint) paint->clip_rect_counter = 0; paint->gradient_counter = 0; paint->color_glyph_counter = 0; + paint->color_glyph_depth = 0; paint->current_color_glyph_has_svg_image = false; paint->current_svg_image_glyph = HB_CODEPOINT_INVALID; paint->current_face = nullptr; paint->svg_image_counter = 0; hb_set_clear (paint->defined_outlines); hb_set_clear (paint->defined_clips); + hb_set_clear (paint->active_color_glyphs); paint->defined_color_glyphs.clear (); paint->color_stops_scratch.clear (); paint->subset_body_scratch.clear (); diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index b46f4047b..57209bc65 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -43,6 +43,14 @@ #ifndef HB_NO_SVG #include "hb-vector-svg.hh" +static bool +hb_svg_range_is_valid (unsigned start, + unsigned end, + unsigned len) +{ + return start <= end && end <= len; +} + static bool hb_svg_append_with_prefix (hb_vector_t *out, const char *s, @@ -330,6 +338,8 @@ hb_svg_subset_glyph_image (hb_face_t *face, if (!face->table.SVG->doc_cache_get_glyph_span (doc_cache, glyph, &glyph_start, &glyph_end)) goto done; + if (unlikely (!hb_svg_range_is_valid (glyph_start, glyph_end, len))) + goto done; defs_entries = face->table.SVG->doc_cache_get_defs_entries (doc_cache); if (!hb_svg_find_root_open_tag (svg, len, &root_open_len, &root_missing_viewport)) @@ -358,6 +368,8 @@ hb_svg_subset_glyph_image (hb_face_t *face, chosen_def_marks.arrayZ[i] = 1; if (unlikely (!chosen_defs.push_or_fail (i))) goto done; + if (unlikely (!hb_svg_range_is_valid (e.start, e.end, len))) + goto done; if (!hb_svg_collect_refs (svg + e.start, e.end - e.start, &needed_ids, &needed_ids_set)) goto done; @@ -376,6 +388,8 @@ hb_svg_subset_glyph_image (hb_face_t *face, for (unsigned i = 0; i < chosen_defs.length; i++) { const auto &e = defs_entries->arrayZ[chosen_defs.arrayZ[i]]; + if (unlikely (!hb_svg_range_is_valid (e.start, e.end, len))) + goto done; if (!hb_svg_append_with_prefix (defs_dst, svg + e.start, e.end - e.start, prefix, (unsigned) prefix_len)) goto done; if (!hb_svg_append_c (defs_dst, '\n')) @@ -384,6 +398,8 @@ hb_svg_subset_glyph_image (hb_face_t *face, if (glyph_is_root_svg && root_missing_viewport && root_open_len > 1) { + if (unlikely (root_open_len > glyph_end - glyph_start)) + goto done; unsigned upem = face->get_upem (); if (!hb_svg_append_with_prefix (body_dst, svg + glyph_start, @@ -413,6 +429,8 @@ hb_svg_subset_glyph_image (hb_face_t *face, } else if (glyph_is_root_svg && root_open_len > 1) { + if (unlikely (root_open_len > glyph_end - glyph_start)) + goto done; if (!hb_svg_append_with_prefix (body_dst, svg + glyph_start, root_open_len - 1, diff --git a/src/test-subset-instancer-solver.cc b/src/test-subset-instancer-solver.cc index 41e535869..2ebf900d0 100644 --- a/src/test-subset-instancer-solver.cc +++ b/src/test-subset-instancer-solver.cc @@ -82,6 +82,13 @@ main (int argc, char **argv) hb_always_assert (out.length == 0); } + { + /* malformed triple should not abort in renormalization */ + Triple malformed_range (0.4, 0.2, 0.3); + double normalized = renormalizeValue (0.25, malformed_range, default_axis_distances, true); + hb_always_assert (approx (normalized, 0.25)); + } + /* Case 2 */ { Triple tent (0.0, 1.0, 1.0); diff --git a/test/fuzzing/clusterfuzz-testcase-minimized-hb-subset-fuzzer-6461128124465152 b/test/fuzzing/clusterfuzz-testcase-minimized-hb-subset-fuzzer-6461128124465152 new file mode 100644 index 000000000..8db0a2a7f Binary files /dev/null and b/test/fuzzing/clusterfuzz-testcase-minimized-hb-subset-fuzzer-6461128124465152 differ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6252581608554496 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6252581608554496 new file mode 100644 index 000000000..8795db1a2 Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6252581608554496 differ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6454028543524864 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6454028543524864 new file mode 100644 index 000000000..c85df5999 Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6454028543524864 differ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-5448347908636672 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-5448347908636672 new file mode 100644 index 000000000..5445d3967 Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-5448347908636672 differ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-5985308075425792 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-5985308075425792 new file mode 100644 index 000000000..9f304d525 Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-5985308075425792 differ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-6120408016551936 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-6120408016551936 new file mode 100644 index 000000000..086a47872 Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-6120408016551936 differ