From 21a894f055bbff6afb45cbcceed89ac92091bb3e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 15 Aug 2023 10:21:26 -0600 Subject: [PATCH] [cairo] Protect against infinite loops Fixes https://github.com/harfbuzz/harfbuzz/issues/4375 Maybe we should do this in hb-ft and COLR instead? --- src/hb-cairo-utils.hh | 3 +++ src/hb-cairo.cc | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/hb-cairo-utils.hh b/src/hb-cairo-utils.hh index a26bf59d9..54355632b 100644 --- a/src/hb-cairo-utils.hh +++ b/src/hb-cairo-utils.hh @@ -30,11 +30,14 @@ #include "hb.hh" #include "hb-cairo.h" +#include "hb-set.hh" + typedef struct { cairo_scaled_font_t *scaled_font; cairo_t *cr; + hb_set_t current_glyphs; hb_map_t *color_cache; } hb_cairo_context_t; diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index f4f9f54ab..29461be80 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -176,6 +176,11 @@ hb_cairo_paint_color_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data; cairo_t *cr = c->cr; + if (unlikely (c->current_glyphs.has (glyph))) + return true; + + c->current_glyphs.add (glyph); + cairo_save (cr); hb_position_t x_scale, y_scale; @@ -189,6 +194,8 @@ hb_cairo_paint_color_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, cairo_restore (cr); + c->current_glyphs.del (glyph); + return true; } @@ -634,6 +641,7 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, c.scaled_font = scaled_font; c.cr = cr; c.color_cache = (hb_map_t *) cairo_scaled_font_get_user_data (scaled_font, &color_cache_key); + c.current_glyphs.add (glyph); hb_font_paint_glyph (font, glyph, hb_cairo_paint_get_funcs (), &c, palette, color);