From 51e229759f4b3ce6cf08997d056b892a396da993 Mon Sep 17 00:00:00 2001 From: punishell <33149001+punishell@users.noreply.github.com> Date: Fri, 31 Jul 2026 09:08:01 +0200 Subject: [PATCH] Merge commit from fork Follow-up to #6090 (GHSA-h2xh-wmpf-pwp5). #6090 saturated the float->int conversions in emit_segment() via hb_clamp_to, but a saturated edge endpoint (INT32_MAX) then overflows the ceil-round in the auto-size path of hb_raster_draw_render(): int x1 = (xmax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS; With xmax == INT32_MAX this is INT32_MAX + 255, signed overflow (UB). UBSan fires on the accepted PoC (single spec-legal PaintTransform, scale 32767.99) at the first painted glyph. Widen the +MASK step to int64_t before the shift. Also saturate the four float->int corner conversions in hb_raster_paint_push_clip_rectangle() through hb_clamp_to, the same unguarded pattern #6090 clamped elsewhere in this file (reachable via PaintColrGlyph under a large PaintTransform); the subtraction is done in double to avoid overflow, and NaN maps to INT32_MIN. Verified with -fsanitize=address,undefined: the PoC (NEST=1 and NEST=9) and full-font render of the base font produce zero runtime errors; without the draw.cc change the PoC reports the signed-overflow above. --- src/hb-raster-draw.cc | 8 +++++--- src/hb-raster-paint.cc | 12 ++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index 3b77fea1b..ece9e068d 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -1289,11 +1289,13 @@ hb_raster_draw_render (hb_raster_draw_t *draw) ymax = hb_max (ymax, e.yH); } - /* Convert fixed-point → pixels (floor for min, ceil for max) */ + /* Convert fixed-point → pixels (floor for min, ceil for max). Edge + coordinates are saturated to int32 range in emit_segment, so the + +MASK ceil step must be widened to avoid signed overflow. */ int x0 = xmin >> HB_RASTER_PIXEL_BITS; int y0 = ymin >> HB_RASTER_PIXEL_BITS; - int x1 = (xmax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS; - int y1 = (ymax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS; + int x1 = (int) (((int64_t) xmax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS); + int y1 = (int) (((int64_t) ymax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS); ext.x_origin = x0; ext.y_origin = y0; diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 779c3d99c..edee5fd3a 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -433,10 +433,14 @@ hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED, fmax_x = hb_max (fmax_x, cx[i]); fmax_y = hb_max (fmax_y, cy[i]); } - int px0 = (int) floorf (fmin_x) - surf->extents.x_origin; - int py0 = (int) floorf (fmin_y) - surf->extents.y_origin; - int px1 = (int) ceilf (fmax_x) - surf->extents.x_origin; - int py1 = (int) ceilf (fmax_y) - surf->extents.y_origin; + /* A spec-legal but very large transform can push these bounds past the int + range or to non-finite values; a bare float->int conversion would then be + undefined. Saturate through hb_clamp_to<> (which also maps NaN to + INT32_MIN); the subtraction is done in double to avoid overflow. */ + int px0 = hb_clamp_to ((double) floorf (fmin_x) - surf->extents.x_origin); + int py0 = hb_clamp_to ((double) floorf (fmin_y) - surf->extents.y_origin); + int px1 = hb_clamp_to ((double) ceilf (fmax_x) - surf->extents.x_origin); + int py1 = hb_clamp_to ((double) ceilf (fmax_y) - surf->extents.y_origin); /* Clamp to surface bounds */ px0 = hb_max (px0, 0);