Files
harfbuzz/test/api
Wei-Lun Li 24ded4335b [font] Saturate synthetic bold and glyph origin arithmetic (#6585)
* [font] Saturate synthetic bold and glyph origin arithmetic

#6154 made hb_position_t arithmetic saturate and #6296 did the same
for synthetic_glyph_extents(), but the rest of hb_font_t still used
plain signed arithmetic:

- changed() computed the embolden strength with abs (x_scale), which
  overflows for INT32_MIN, and converted the float product to int
  without a range check. A strength of INT32_MIN could also not be
  negated later on, for negative scales.
- get_font_[hv]_extents(), the advance, origin and contour point
  getters added the strength with += and could overflow.
- add_offset(), subtract_offset() and apply_offset() overflow when
  they apply a large origin, and so do the origin fallbacks in
  apply_glyph_[hv]_origins_with_fallback().
- em_multf() converted its float result to hb_position_t without a
  range check, which is undefined for a value such as an ascender
  above the em at a scale close to INT32_MAX.

Use hb_saturate_add(), hb_saturate_sub() and hb_clamp_to() in these
places, and keep the strength away from INT32_MIN so that negating it
stays defined. Results that fit do not change.

The shift variables that were int are now hb_position_t, since the
saturating helpers deduce one type and int32_t is not int everywhere.

Tested with test-font under UBSan: the new tests fail on the base
commit at hb-font.hh:365, :848, :1154 and :1167, and pass here.

Assisted-by: Claude

* [test] Cover plural origins and synthetic slant saturation

Review feedback: the plural origin getters and the slant
clamp in get_glyph_contour_point were not covered by the
tests, so removing their saturation would still pass.

test_synthetic_bold_saturates now also calls
hb_font_get_glyph_h_origins and hb_font_get_glyph_v_origins
once each and checks both coordinates for INT32_MAX. A new
test_synthetic_slant_saturates sets a slant of 1 on a contour
point one short of INT32_MAX in both axes and checks that x
saturates to INT32_MAX.

Verified by mutation: replacing the plural hb_saturate_add
calls with plain addition makes the first test fail (the
value wraps), and dropping the hb_clamp_to in the slant path
makes the second fail (x comes out as -2). Full
meson test -C build: 270 ok, 0 fail, 7 skipped
(subset+repack, missing repacker dependency; same as the
baseline run for this PR).

Assisted-by: Claude

* [font] Apply the synthetic slant shift before clamping

get_glyph_contour_point clamped the slant shift to
hb_position_t before adding it to x. When x and the shift
have opposite signs and partly cancel, clamping first
changes the result: a contour point at x = -2000000000,
y = 2000000000 with a slant of 2 should land at
x = 2000000000, but came out as 147483647, because the
shift was clamped to INT32_MAX before the addition.

Add the shift first and clamp the sum instead. The
existing saturation case is unchanged: hb_clamp_to
widens to double to do the clamp, so an out-of-range sum
still saturates instead of hitting an undefined
float-to-int conversion.

The new test_synthetic_slant_cancelling covers the
cancelling case. Verified by mutation: restoring the
clamp-first order makes it fail with 147483647, the
reported value. Full meson test -C build: 270 ok,
0 fail, 7 skipped, same as baseline.

Assisted-by: Claude
2026-11-09 11:47:15 -06:00
..
2025-04-06 18:08:50 -06:00
2026-07-30 19:02:25 -06:00
2026-07-31 04:31:26 +03:00
2025-04-05 22:37:10 -06:00
2020-01-15 13:36:01 -08:00
2025-06-08 13:43:27 +03:00