From fde0fd8592dd77a79cd428e71c6649c206e374dd Mon Sep 17 00:00:00 2001 From: nussaibah shaikh Date: Tue, 18 Aug 2026 01:20:20 +0530 Subject: [PATCH] Merge pull request #6164 from Nussu06/gpu-clamp-i16-nan-guard [gpu] handle NaN in clamp_i16 before int16 cast --- src/hb-gpu-paint.cc | 1 + test/api/test-gpu.cc | 70 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 90abb05da..9885a8a2a 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -291,6 +291,7 @@ clamp_i16 (float v) { if (v <= -32768.f) return -32768; if (v >= 32767.f) return 32767; + if (unlikely (std::isnan (v))) return 0; return (int16_t) v; } diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc index c18ed48e7..c637c9039 100644 --- a/test/api/test-gpu.cc +++ b/test/api/test-gpu.cc @@ -667,6 +667,75 @@ test_paint_clip_path_depth_overflow (void) hb_gpu_paint_destroy (p); } +static unsigned +repro_color_stops (hb_color_line_t *cl HB_UNUSED, void *cl_data HB_UNUSED, + unsigned start, unsigned *count, + hb_color_stop_t *stops, void *user_data HB_UNUSED) +{ + static const hb_color_stop_t all[2] = { + { 0.f, 0, HB_COLOR (255, 0, 0, 255) }, + { 1.f, 0, HB_COLOR (0, 0, 255, 255) }, + }; + if (count) + { + unsigned n = 0; + for (unsigned i = start; i < 2 && n < *count; i++) + stops[n++] = all[i]; + *count = n; + } + return 2; +} + +static hb_paint_extend_t +repro_color_extend (hb_color_line_t *cl HB_UNUSED, void *cl_data HB_UNUSED, + void *user_data HB_UNUSED) +{ + return HB_PAINT_EXTEND_PAD; +} + +static void +test_paint_gradient_overflow_transform (void) +{ + /* A COLRv1 paint tree can nest PaintScale/PaintTransform, which the + * GPU paint encoder composes multiplicatively into cur_transform. + * Deep enough nesting overflows the matrix entries to +inf. When a + * gradient encoder inverts that transform (linv_q10), a 1/inf + * determinant gives inv==0 and inf*0==NaN, which clamp_i16 then cast + * straight to int16 -- float-cast-overflow UB that aborts the + * asan-ubsan CI job (halt_on_error). */ + 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); + + /* A clip must be on the stack before a gradient is emitted. */ + void *draw_data = nullptr; + hb_draw_funcs_t *df = hb_paint_push_clip_path_start (pf, p, &draw_data); + g_assert_nonnull (df); + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + hb_draw_move_to (df, draw_data, &st, 0.f, 0.f); + hb_draw_line_to (df, draw_data, &st, 100.f, 0.f); + hb_draw_line_to (df, draw_data, &st, 100.f, 100.f); + hb_draw_close_path (df, draw_data, &st); + hb_paint_push_clip_path_end (pf, p); + + /* Compose large scales until the transform overflows to inf. */ + for (unsigned i = 0; i < 4; i++) + hb_paint_push_transform (pf, p, 1e19f, 0.f, 0.f, 1e19f, 0.f, 0.f); + + hb_color_line_t cl = {}; + cl.get_color_stops = repro_color_stops; + cl.get_extend = repro_color_extend; + + hb_paint_linear_gradient (pf, p, &cl, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f); + + hb_blob_t *blob = hb_gpu_paint_encode (p, nullptr); + hb_blob_destroy (blob); + + hb_gpu_paint_destroy (p); +} + static void test_shapes (void) @@ -784,6 +853,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_gradient_overflow_transform); return hb_test_run (); }