[graphite2] Saturate shaped positions

Graphite coordinates and cluster advances can exceed the position
range after scaling or accumulation. Clamp backend-produced values and
saturate cluster and pen arithmetic before storing glyph positions.

This is part of the complete position-overflow fix for issue #6151.

Testing: ninja -C build-graphite2 src/libharfbuzz.so.0.61431.0
Testing: meson test -C build --print-errorlogs

Assisted-by: Codex <codex@openai.com>
This commit is contained in:
Behdad Esfahbod committed 2026-08-12 18:01:41 -06:00
1 parent 2a2a3792a5
commit 2e2a77a60f
1 file changed
+27 -23
+27 -23
View File
@@ -219,7 +219,7 @@ struct hb_graphite2_cluster_t {
unsigned int base_glyph;
unsigned int num_glyphs;
unsigned int cluster;
int advance;
hb_position_t advance;
};
hb_bool_t
@@ -262,7 +262,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
gr_segment *seg = nullptr;
const gr_slot *is;
unsigned int ci = 0, ic = 0;
int curradvx = 0, curradvy = 0;
hb_position_t curradvx = 0, curradvy = 0;
unsigned int scratch_size;
hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
@@ -327,11 +327,11 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
float xscale = (float) font->x_scale / upem;
float yscale = (float) font->y_scale / upem;
yscale *= yscale / xscale;
unsigned int curradv = 0;
hb_position_t curradv = 0;
if (HB_DIRECTION_IS_BACKWARD (direction))
{
curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale;
clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv;
curradv = hb_clamp_to<hb_position_t> (gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale);
clusters[0].advance = hb_clamp_to<hb_position_t> ((double) (gr_seg_advance_X(seg) * xscale) - curradv);
}
else
clusters[0].advance = 0;
@@ -345,7 +345,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
{
clusters[ci-1].num_chars += clusters[ci].num_chars;
clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
clusters[ci-1].advance += clusters[ci].advance;
clusters[ci-1].advance = hb_saturate_add (clusters[ci-1].advance, clusters[ci].advance);
ci--;
}
@@ -359,14 +359,16 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
c->num_glyphs = 0;
if (HB_DIRECTION_IS_BACKWARD (direction))
{
c->advance = curradv - gr_slot_origin_X(is) * xscale;
curradv -= c->advance;
c->advance = hb_clamp_to<hb_position_t> ((double) curradv -
(double) (gr_slot_origin_X(is) * xscale));
curradv = hb_saturate_sub (curradv, c->advance);
}
else
{
auto origin_X = gr_slot_origin_X (is) * xscale;
hb_position_t origin_X = hb_clamp_to<hb_position_t> (gr_slot_origin_X (is) * xscale);
c->advance = 0;
clusters[ci].advance += origin_X - curradv;
clusters[ci].advance = hb_saturate_add (clusters[ci].advance,
hb_saturate_sub (origin_X, curradv));
curradv = origin_X;
}
ci++;
@@ -378,9 +380,10 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
}
if (HB_DIRECTION_IS_BACKWARD (direction))
clusters[ci].advance += curradv;
clusters[ci].advance = hb_saturate_add (clusters[ci].advance, curradv);
else
clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv;
clusters[ci].advance = hb_clamp_to<hb_position_t> ((double) clusters[ci].advance +
(double) (gr_seg_advance_X(seg) * xscale) - curradv);
ci++;
for (unsigned int i = 0; i < ci; ++i)
@@ -404,36 +407,37 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
curradvx = 0;
for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is))
{
pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
pPos->x_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_X (is) * xscale) - curradvx);
pPos->y_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_Y (is) * yscale) - curradvy);
if (info->cluster != currclus) {
pPos->x_advance = info->var1.i32;
curradvx += pPos->x_advance;
curradvx = hb_saturate_add (curradvx, pPos->x_advance);
currclus = info->cluster;
} else
pPos->x_advance = 0.;
pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
curradvy += pPos->y_advance;
pPos->y_advance = hb_clamp_to<hb_position_t> (gr_slot_advance_Y (is, grface, nullptr) * yscale);
curradvy = hb_saturate_add (curradvy, pPos->y_advance);
}
}
else
{
curradvx = gr_seg_advance_X(seg) * xscale;
curradvx = hb_clamp_to<hb_position_t> (gr_seg_advance_X(seg) * xscale);
for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is))
{
if (info->cluster != currclus)
{
pPos->x_advance = info->var1.i32;
curradvx -= pPos->x_advance;
curradvx = hb_saturate_sub (curradvx, pPos->x_advance);
currclus = info->cluster;
} else
pPos->x_advance = 0.;
pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
curradvy -= pPos->y_advance;
pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1.i32 - curradvx + pPos->x_advance;
pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
pPos->y_advance = hb_clamp_to<hb_position_t> (gr_slot_advance_Y (is, grface, nullptr) * yscale);
curradvy = hb_saturate_sub (curradvy, pPos->y_advance);
pPos->x_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_X (is) * xscale) -
info->var1.i32 - curradvx + pPos->x_advance);
pPos->y_offset = hb_clamp_to<hb_position_t> ((double) (gr_slot_origin_Y (is) * yscale) - curradvy);
}
hb_buffer_reverse_clusters (buffer);
}