[font] Fix signed integer overflow in scale_glyph_extents (#6275)

hb_font_t::scale_glyph_extents() computed
extents->x_bearing + extents->width (and the y equivalent) as a
plain hb_position_t (int32_t) addition before scaling to float.

glyf's contour_bounds_t::get_extents() clamps x_bearing and width
independently via hb_clamp_to<hb_position_t>(), so each can reach
INT32_MIN/INT32_MAX on its own for a malformed/adversarial glyf
outline. Their sum can then overflow a 32-bit int, which is
undefined behavior and was flagged by UBSan.

Fix by doing the addition in float, after each term is already
scaled, instead of adding the raw hb_position_t values first.

Reproduced with the OSS-Fuzz hb-raster-fuzzer testcase at
https://oss-fuzz.com/testcase?key=5782717883809792, built locally
with -Db_sanitize=undefined:

  ../src/hb-font.hh:205:28: runtime error: signed integer overflow:
  32 + 2147483647 cannot be represented in type 'int'

Confirmed the UBSan report no longer triggers on that testcase
after the fix, and `meson test -C build` passes in full (278/278,
1 pre-existing skip).

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Sonnet 5 authored and GitHub committed 2026-09-23 10:18:44 -06:00
1 parent b3bab62307
commit 873dbc1e32
1 file changed
+2 -2
+2 -2
View File
@@ -202,8 +202,8 @@ struct hb_font_t
{
float x1 = em_fscale_x (extents->x_bearing);
float y1 = em_fscale_y (extents->y_bearing);
float x2 = em_fscale_x (extents->x_bearing + extents->width);
float y2 = em_fscale_y (extents->y_bearing + extents->height);
float x2 = x1 + em_fscale_x (extents->width);
float y2 = y1 + em_fscale_y (extents->height);
extents->x_bearing = floorf (x1);
extents->y_bearing = floorf (y1);