[cairo] Paint nested color glyphs inline to bound recursion

hb_cairo_paint_color_glyph() accepted the color_glyph paint callback
and drew the nested glyph with cairo_show_glyphs(), which re-enters
hb_font_paint_glyph() through a new cairo scaled font with a fresh
hb_paint_context_t.  The glyph decycler, HB_MAX_NESTING_LEVEL and the
edge budget therefore start over at every level, so two glyphs that
paint each other with PaintColrGlyph recurse until the stack is
exhausted.  The per-scaled-font hb_cairo_budget_t cannot bound this
either: the nested glyph is shown through a different scaled font, so
each level gets its own budget.

Decline the callback, returning false exactly as hb-raster's does, so
the COLR (and hb-ft-colr) walk paints the nested glyph inline under the
decycler and depth limits of the walk already in progress.

A regression test with two PaintColrGlyph glyphs that paint each other
is added to test/api/test-cairo.c; it aborts on the unpatched library
and passes with the fix.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-VPKA505E.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-10-30 14:43:49 -06:00
1 parent 88e5077a80
commit fd8c9209b6
3 files changed
+53 -24

No files matched your search

+1 -1
View File
@@ -34,7 +34,7 @@
struct hb_cairo_budget_t
{
/* Cairo serializes glyph-cache population for a scaled font. Keeping the
* owner there lets recursive color-glyph callbacks share one session. */
* owner there lets the draw and paint callbacks share one session. */
int64_t budget = HB_BUDGET_DEFAULT;
int64_t budget_remaining = HB_BUDGET_GLYPH;
unsigned int active = 0;
+10 -23
View File
@@ -227,31 +227,18 @@ hb_cairo_fill_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED,
static hb_bool_t
hb_cairo_paint_color_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED,
void *paint_data,
hb_codepoint_t glyph,
hb_font_t *font,
void *paint_data HB_UNUSED,
hb_codepoint_t glyph HB_UNUSED,
hb_font_t *font HB_UNUSED,
void *user_data HB_UNUSED)
{
hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data;
cairo_t *cr = c->cr;
if (unlikely (!c->spend (HB_BUDGET_1)))
return true;
cairo_save (cr);
hb_position_t x_scale, y_scale;
hb_font_get_scale (font, &x_scale, &y_scale);
cairo_scale (cr, x_scale, -y_scale);
cairo_glyph_t cairo_glyph = { glyph, 0, 0 };
cairo_set_scaled_font (cr, c->scaled_font);
cairo_set_font_size (cr, 1);
cairo_show_glyphs (cr, &cairo_glyph, 1);
cairo_restore (cr);
return true;
/* Showing the nested glyph through cairo_show_glyphs() would re-enter
* hb_font_paint_glyph() on a scaled font of its own, with a fresh paint
* context: the glyph decycler, nesting depth and work limits of the
* walk in progress do not carry over, so two glyphs painting each other
* recurse until the stack is exhausted. Decline, and let the caller
* paint the nested glyph inline under the limits of the current walk. */
return false;
}
static void
+42
View File
@@ -67,11 +67,53 @@ test_hb_cairo_budget (void)
hb_face_destroy (face);
}
static void
test_hb_cairo_color_glyph_cycle (void)
{
#ifdef HB_TEST_CAIRO_TSAN
g_test_skip ("Cairo user-font rendering has a pre-existing lock-order inversion under TSan");
return;
#endif
/* Two COLRv1 glyphs that paint each other with PaintColrGlyph. The
* color_glyph callback used to show the nested glyph through cairo,
* re-entering hb_font_paint_glyph() without the cycle and depth
* guards of the paint walk in progress. */
hb_face_t *face = hb_test_open_font_file ("fonts/test_glyphs-glyf_colr_1.ttf");
hb_font_t *font = hb_font_create (face);
cairo_font_face_t *cairo_face = hb_cairo_font_face_create_for_font (font);
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 64, 64);
cairo_t *cr = cairo_create (surface);
cairo_set_font_face (cr, cairo_face);
cairo_set_font_size (cr, 30);
/* paintcolrglyph_cycle_first, paintcolrglyph_cycle_second */
for (hb_codepoint_t u = 0xF1100; u <= 0xF1101; u++)
{
hb_codepoint_t gid;
g_assert_true (hb_font_get_nominal_glyph (font, u, &gid));
cairo_glyph_t glyph = {gid, 0, 30};
cairo_show_glyphs (cr, &glyph, 1);
}
g_assert_cmpint (cairo_status (cr), ==, CAIRO_STATUS_SUCCESS);
g_assert_cmpint (cairo_surface_status (surface), ==, CAIRO_STATUS_SUCCESS);
cairo_destroy (cr);
cairo_surface_destroy (surface);
cairo_font_face_destroy (cairo_face);
hb_font_destroy (font);
hb_face_destroy (face);
}
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_hb_cairo_budget);
hb_test_add (test_hb_cairo_color_glyph_cycle);
return hb_test_run ();
}