[gpu] Count ignored rectangle clips so their pop does not pop a real clip

The GPU paint encoder left push_clip_rectangle at the no-op default
while pop_clip unconditionally decremented clip_depth.  COLR emits a
rectangle push/pop pair around every PaintColrGlyph that has a ClipBox,
so that pop removed whatever glyph/path clip sat below it on the stack.
When such a PaintColrGlyph is nested inside a PaintGlyph, the sibling
layers encoded afterwards carried one clip outline too few, painting
ink outside the enclosing outline, or were dropped entirely when they
reached an emitter with clip_depth == 0.

Register a push_clip_rectangle callback that records the push in a
small per-depth counter (rect_clips[d] = rectangles pushed while
clip_depth was d).  pop_clip consumes a non-zero count at the current
depth first and only otherwise pops a real clip.  Rectangle clips are
still left unencoded, exactly as the v1 design notes state; the counter
only keeps the glyph/path clip depth balanced across the ignored pair.
This is correct for any LIFO push/pop order without allocation, and
changes nothing for paint trees without rectangle clips (the top-level
ClipBox in COLR::paint_glyph is already balanced).

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-ZEL64M07.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-10-30 16:54:46 -06:00
1 parent e8f8ee612f
commit f97d6bbb0d
3 files changed
+84

No files matched your search

+28
View File
@@ -104,9 +104,35 @@ hb_gpu_paint_pop_clip (hb_paint_funcs_t *funcs HB_UNUSED,
void *user_data HB_UNUSED)
{
hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data;
/* A rectangle clip pushed at this depth is popped before the
* glyph/path clip below it; its pop leaves clip_depth alone. */
unsigned &rects = c->rect_clips[hb_min (c->clip_depth, (unsigned) HB_GPU_PAINT_MAX_CLIP_DEPTH)];
if (rects)
{
rects--;
return;
}
if (likely (c->clip_depth > 0)) c->clip_depth--;
}
/* Clip rectangles are not encoded in v1 (see the design notes in
* hb-gpu-paint.hh). Only count the push so that the matching
* pop_clip does not pop the glyph/path clip below it. */
static void
hb_gpu_paint_push_clip_rectangle (hb_paint_funcs_t *funcs HB_UNUSED,
void *paint_data,
float xmin HB_UNUSED,
float ymin HB_UNUSED,
float xmax HB_UNUSED,
float ymax HB_UNUSED,
void *user_data HB_UNUSED)
{
hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data;
c->rect_clips[hb_min (c->clip_depth, (unsigned) HB_GPU_PAINT_MAX_CLIP_DEPTH)]++;
}
/* Arbitrary-path clip: hand the caller the same hb_gpu_draw
* funcs the encoder uses for glyph outlines, with scratch_draw
* as the accumulator. push_clip_path_end encodes the captured
@@ -1084,6 +1110,7 @@ static struct hb_gpu_paint_funcs_lazy_loader_t
hb_paint_funcs_set_push_transform_func (funcs, hb_gpu_paint_push_transform, nullptr, nullptr);
hb_paint_funcs_set_pop_transform_func (funcs, hb_gpu_paint_pop_transform, nullptr, nullptr);
hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_gpu_paint_push_clip_glyph, nullptr, nullptr);
hb_paint_funcs_set_push_clip_rectangle_func (funcs, hb_gpu_paint_push_clip_rectangle, nullptr, nullptr);
hb_paint_funcs_set_push_clip_path_start_func (funcs, hb_gpu_paint_push_clip_path_start, nullptr, nullptr);
hb_paint_funcs_set_push_clip_path_end_func (funcs, hb_gpu_paint_push_clip_path_end, nullptr, nullptr);
hb_paint_funcs_set_pop_clip_func (funcs, hb_gpu_paint_pop_clip, nullptr, nullptr);
@@ -1646,6 +1673,7 @@ hb_gpu_paint_clear (hb_gpu_paint_t *paint)
paint->sub_blobs.reset ();
paint->sub_bytes = 0;
paint->clip_depth = 0;
hb_memset (paint->rect_clips, 0, sizeof (paint->rect_clips));
paint->pending_clip_path = false;
paint->unsupported = false;
paint->recharge_budget ();
+8
View File
@@ -325,6 +325,14 @@ struct hb_gpu_paint_t
pending_clip_t clip_stack[3];
unsigned clip_depth = 0;
/* Rectangle clips are not encoded (see "Out of scope for v1"),
* but their pop_clip must not pop the glyph/path clip below them.
* rect_clips[d] counts the rectangle clips pushed while clip_depth
* was d; the paint walk pops them before anything at depth d, so
* a non-zero count at the current depth identifies a rectangle
* pop. The last slot collects everything pushed past the cap. */
unsigned rect_clips[3 + 1] = {};
/* Carries the cur_transform captured at push_clip_path_start
* through to push_clip_path_end, where the encoded clip is
* committed to clip_stack. */
+48
View File
@@ -741,6 +741,53 @@ test_paint_clip_path_depth_overflow (void)
hb_gpu_paint_destroy (p);
}
static void
test_paint_clip_rectangle_pop_balance (void)
{
/* Rectangle clips are not encoded, but their pop_clip must not
* pop the glyph clip below them. COLRv1 emits such a pair around
* every PaintColrGlyph that has a ClipBox; when that sits inside a
* PaintGlyph, an unbalanced pop un-clipped (or dropped) the sibling
* layers that followed. */
hb_face_t *face = hb_test_open_font_file (FONT_FILE);
g_assert_nonnull (face);
hb_font_t *font = hb_font_create (face);
hb_gpu_paint_t *p = hb_gpu_paint_create_or_fail ();
g_assert_nonnull (p);
hb_gpu_paint_set_scale (p, 1000, 1000);
hb_paint_funcs_t *pf = hb_gpu_paint_get_funcs (p);
hb_codepoint_t gid;
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &gid));
hb_paint_push_clip_glyph (pf, p, gid, font);
hb_paint_push_clip_rectangle (pf, p, 0.f, 0.f, 100.f, 100.f);
hb_paint_pop_clip (pf, p);
/* Still inside the glyph clip: this layer must be emitted. */
hb_paint_color (pf, p, false, HB_COLOR (0, 0, 255, 255));
hb_paint_pop_clip (pf, p);
/* Outside every clip: this layer must be dropped. */
hb_paint_color (pf, p, false, HB_COLOR (255, 0, 0, 255));
hb_blob_t *blob = hb_gpu_paint_encode (p, nullptr);
g_assert_nonnull (blob);
unsigned texel_count;
const hb_gpu_test_texel_t *texels = blob_as_texels (blob, &texel_count);
g_assert_cmpuint (texel_count, >, 0);
/* Header texel 0 .r holds num_ops. */
g_assert_cmpint (texels[0].r, ==, 1);
hb_blob_destroy (blob);
hb_gpu_paint_destroy (p);
hb_font_destroy (font);
hb_face_destroy (face);
}
static unsigned
repro_color_stops (hb_color_line_t *cl HB_UNUSED, void *cl_data HB_UNUSED,
unsigned start, unsigned *count,
@@ -1033,6 +1080,7 @@ main (int argc, char **argv)
hb_test_add (test_paint_reset);
hb_test_add (test_paint_recycle_blob);
hb_test_add (test_paint_clip_path_depth_overflow);
hb_test_add (test_paint_clip_rectangle_pop_balance);
hb_test_add (test_paint_gradient_overflow_transform);
hb_test_add (test_paint_empty_clip);