[cairo] Map non-monotone buffer clusters to one cairo cluster

hb_cairo_glyphs_from_buffer() asserted that buffer clusters are monotone
in the buffer direction, but hb_shape() does not guarantee that:
HB_BUFFER_CLUSTER_LEVEL_CHARACTERS, shaping that hits the ops limit
mid-lookup, and Indic reordering corner cases can all produce
non-monotone clusters.  The function runs from HarfBuzz's own cairo
text_to_glyphs callback on attacker-controlled text and fonts, so the
assertion was an input-dependent abort (and, with NDEBUG, an unchecked
backward walk off the ends of the utf8 buffer).

The cluster-counting loop that already walks the buffer now detects a
step against the buffer's direction and falls back to a single cairo
cluster mapping all of the utf8 to all of the glyphs, which cairo
accepts.  The monotonicity assertions in the fill step then hold by
construction and glyph output is unchanged for monotone buffers.

A regression test in test/api/test-cairo.c feeds monotone and
non-monotone LTR/RTL buffers through both cluster modes and checks the
cluster counts and byte/glyph totals; it aborts on the pristine tree.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-T4PZ1H72.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-10-30 14:45:15 -06:00
1 parent 4e215a7ec8
commit f69c05ea38
2 files changed
+86 -2

No files matched your search

+23 -2
View File
@@ -1068,6 +1068,10 @@ hb_cairo_font_face_get_scale_factor (cairo_font_face_t *font_face)
* If the value @clusters points at has changed after the call, the caller
* will free the allocated cluster array using cairo_text_cluster_free().
*
* Cairo can only represent monotone clusters. If the cluster values in
* @buffer are not monotone in the buffer's direction, a single cluster
* mapping all of @utf8 to all of the glyphs is returned.
*
* See hb_cairo_font_face_set_scale_factor() for the details of
* the @scale_factor argument.
*
@@ -1102,13 +1106,26 @@ hb_cairo_glyphs_from_buffer (hb_buffer_t *buffer,
if (orig_num_glyphs < *num_glyphs + 1)
*glyphs = cairo_glyph_allocate (*num_glyphs + 1);
hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
bool monotone = true;
if (clusters && utf8)
{
unsigned orig_num_clusters = *num_clusters;
*num_clusters = *num_glyphs ? 1 : 0;
for (unsigned int i = 1; i < *num_glyphs; i++)
if (hb_glyph[i].cluster != hb_glyph[i-1].cluster)
{
if (unlikely ((hb_glyph[i].cluster < hb_glyph[i-1].cluster) != (bool) backward))
{
/* Cairo can only represent monotone clusters. Non-monotone
* cluster levels, shaper bugs, or shaping that failed midway
* can all get us here. Map all of the text to all of the glyphs. */
monotone = false;
*num_clusters = 1;
break;
}
(*num_clusters)++;
}
if (orig_num_clusters < *num_clusters)
*clusters = cairo_text_cluster_allocate (*num_clusters);
}
@@ -1134,12 +1151,16 @@ hb_cairo_glyphs_from_buffer (hb_buffer_t *buffer,
if (clusters && *num_clusters && utf8)
{
hb_memset ((void *) *clusters, 0, *num_clusters * sizeof ((*clusters)[0]));
hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
*cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0;
unsigned int cluster = 0;
const char *start = utf8, *end;
(*clusters)[cluster].num_glyphs++;
if (backward)
if (unlikely (!monotone))
{
(*clusters)[cluster].num_glyphs = *num_glyphs;
(*clusters)[cluster].num_bytes = utf8_len;
}
else if (backward)
{
for (i = *num_glyphs - 2; i >= 0; i--)
{
+63
View File
@@ -151,6 +151,68 @@ test_hb_cairo_face_lifecycle (void)
g_assert_true (destroyed);
}
static void
test_hb_cairo_glyphs_from_buffer_clusters (void)
{
/* Non-monotone clusters come out of hb_shape() for some inputs
* (HB_BUFFER_CLUSTER_LEVEL_CHARACTERS, shaping that fails midway,
* shaper bugs). Cairo can only represent monotone clusters; make
* sure such buffers map to one cluster instead of aborting. */
static const struct {
hb_direction_t direction;
unsigned clusters[4];
unsigned num_clusters;
} tests[] = {
{HB_DIRECTION_LTR, {0, 1, 1, 3}, 3},
{HB_DIRECTION_RTL, {3, 1, 1, 0}, 3},
/* Not monotone. */
{HB_DIRECTION_LTR, {0, 2, 0, 0}, 1},
{HB_DIRECTION_LTR, {3, 1, 1, 0}, 1},
{HB_DIRECTION_RTL, {0, 0, 2, 0}, 1},
{HB_DIRECTION_RTL, {0, 1, 1, 3}, 1},
};
const char *utf8 = "abcd";
for (unsigned t = 0; t < G_N_ELEMENTS (tests); t++)
for (unsigned utf8_clusters = 0; utf8_clusters < 2; utf8_clusters++)
{
hb_buffer_t *buffer = hb_buffer_create ();
hb_buffer_set_direction (buffer, tests[t].direction);
for (unsigned i = 0; i < 4; i++)
hb_buffer_add (buffer, i, tests[t].clusters[i]);
cairo_glyph_t *glyphs = NULL;
unsigned num_glyphs = 0;
cairo_text_cluster_t *clusters = NULL;
unsigned num_clusters = 0;
cairo_text_cluster_flags_t cluster_flags;
hb_cairo_glyphs_from_buffer (buffer, utf8_clusters,
1., 1., 0., 0.,
utf8, -1,
&glyphs, &num_glyphs,
&clusters, &num_clusters,
&cluster_flags);
g_assert_cmpuint (num_glyphs, ==, 4);
g_assert_cmpuint (num_clusters, ==, tests[t].num_clusters);
int total_bytes = 0, total_glyphs = 0;
for (unsigned i = 0; i < num_clusters; i++)
{
g_assert_cmpint (clusters[i].num_bytes, >=, 0);
g_assert_cmpint (clusters[i].num_glyphs, >, 0);
total_bytes += clusters[i].num_bytes;
total_glyphs += clusters[i].num_glyphs;
}
g_assert_cmpint (total_bytes, ==, 4);
g_assert_cmpint (total_glyphs, ==, 4);
cairo_glyph_free (glyphs);
cairo_text_cluster_free (clusters);
hb_buffer_destroy (buffer);
}
}
int
main (int argc, char **argv)
{
@@ -158,6 +220,7 @@ main (int argc, char **argv)
hb_test_add (test_hb_cairo_budget);
hb_test_add (test_hb_cairo_color_glyph_cycle);
hb_test_add (test_hb_cairo_face_lifecycle);
hb_test_add (test_hb_cairo_glyphs_from_buffer_clusters);
return hb_test_run ();
}