[graphite2] Clear positions when the segment has no slots

Every shaper that returns true must leave the buffer with
have_positions set; the OpenType and fallback shapers call
clear_positions() unconditionally.  _hb_graphite2_shape() borrows
pos[] as scratch via get_scratch_buffer(), which clears
have_positions, and the normal path restores it through
hb_buffer_get_glyph_positions().  But when Graphite deletes every
glyph of the run, the zero-slot early-out set buffer->len = 0 and
returned true without restoring the flag, so a later
hb_buffer_normalize_glyphs() (hb-shape --normalize-glyphs) hit
assert (buffer->have_positions) and aborted.

Restore the invariant where it is broken, by calling
buffer->clear_positions() in the empty-segment return.

Adds test_graphite2_all_glyphs_deleted, with an embedded 500-byte
font whose Silf rule deletes 'a'; shaping "aaa" must now succeed with
length 0 and positions, while the "aba" control is unchanged.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-BG9QJRVN.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-10-30 16:59:32 -06:00
1 parent b32bfc243d
commit fca5fc4e6b
2 files changed
+83

No files matched your search

+1
View File
@@ -295,6 +295,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
if (feats) gr_featureval_destroy (feats);
gr_seg_destroy (seg);
buffer->len = 0;
buffer->clear_positions ();
return true;
}
+82
View File
@@ -112,6 +112,87 @@ test_graphite2_inserted_glyph_masks (void)
hb_font_destroy (font);
}
/* Three glyphs, cmap for 'a' and 'b', and a Silf table with a single
* substitution rule that deletes the glyph of 'a'. */
static const unsigned char deletes_a_font[] = {
0x00,0x01,0x00,0x00,0x00,0x08,0x00,0x80,0x00,0x03,0x00,0x00,
0x47,0x6C,0x61,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x8C,
0x00,0x00,0x00,0x10,0x47,0x6C,0x6F,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x9C,0x00,0x00,0x00,0x10,0x53,0x69,0x6C,0x66,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAC,0x00,0x00,0x00,0x94,
0x63,0x6D,0x61,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x40,
0x00,0x00,0x00,0x2C,0x68,0x65,0x61,0x64,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x6C,0x00,0x00,0x00,0x36,0x68,0x68,0x65,0x61,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xA4,0x00,0x00,0x00,0x24,
0x68,0x6D,0x74,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC8,
0x00,0x00,0x00,0x0C,0x6D,0x61,0x78,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0xD4,0x00,0x00,0x00,0x20,0x00,0x01,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x08,
0x00,0x0C,0x00,0x10,0x00,0x02,0x00,0x00,0x00,0x01,0x00,0x00,
0x00,0x00,0x00,0x0C,0x00,0x02,0x00,0x00,0x00,0x00,0x01,0x00,
0x01,0x01,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFF,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x88,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x08,
0x00,0x0A,0x00,0x02,0x00,0x01,0x01,0x00,0x00,0x01,0x00,0x00,
0x00,0x00,0x00,0x85,0x00,0x00,0x00,0x85,0x00,0x00,0x00,0x85,
0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0x00,0x01,0x00,0x01,
0x00,0x01,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x03,0x00,0x01,0x00,0x20,0x19,0x31,0x00,0x00,0x00,0x01,
0x00,0x03,0x00,0x01,0x00,0x00,0x00,0x0C,0x00,0x04,0x00,0x20,
0x00,0x00,0x00,0x04,0x00,0x04,0x00,0x01,0x00,0x00,0x00,0x62,
0xFF,0xFF,0x00,0x00,0x00,0x61,0xFF,0xFF,0xFF,0xA0,0x00,0x01,
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x5F,0x0F,0x3C,0xF5,0x00,0x00,0x03,0xE8,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x08,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x00,0x00,0x03,0x20,0xFF,0x38,0x00,0x00,0x01,0xF4,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0x01,0xF4,0x00,0x00,0x01,0xF4,0x00,0x00,0x01,0xF4,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
static void
test_graphite2_all_glyphs_deleted (void)
{
const char *shapers[] = {"graphite2", NULL};
hb_blob_t *blob;
hb_face_t *face;
hb_font_t *font;
hb_buffer_t *buffer;
blob = hb_blob_create ((const char *) deletes_a_font, sizeof (deletes_a_font),
HB_MEMORY_MODE_READONLY, NULL, NULL);
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
font = hb_font_create (face);
hb_face_destroy (face);
buffer = hb_buffer_create ();
hb_buffer_add_utf8 (buffer, "aba", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, shapers));
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 1);
g_assert_true (hb_buffer_has_positions (buffer));
hb_buffer_clear_contents (buffer);
hb_buffer_add_utf8 (buffer, "aaa", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, shapers));
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 0);
g_assert_true (hb_buffer_has_positions (buffer));
hb_buffer_destroy (buffer);
hb_font_destroy (font);
}
int
main (int argc, char **argv)
{
@@ -119,6 +200,7 @@ main (int argc, char **argv)
hb_test_add (test_graphite2_repeated_clusters);
hb_test_add (test_graphite2_inserted_glyph_masks);
hb_test_add (test_graphite2_all_glyphs_deleted);
return hb_test_run ();
}