Files
harfbuzz/test/api/test-graphite2.c
T
Behdad EsfahbodandClaude Opus 4.8 fca5fc4e6b [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>
2026-10-30 16:59:32 -06:00

207 lines
8.1 KiB
C

/*
* Copyright © 2026 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Behdad Esfahbod
*/
#include "hb-test.h"
static void
test_graphite2_repeated_clusters (void)
{
const char *shapers[] = {"graphite2", NULL};
hb_face_t *face;
hb_font_t *font;
hb_buffer_t *buffer;
hb_glyph_info_t *info;
hb_glyph_position_t *position;
unsigned int length;
hb_position_t advance = 0;
face = hb_test_open_font_file ("../shape/data/in-house/fonts/2f652d30fd15eea6be23fd9bdc1099836975aa83.ttf");
font = hb_font_create (face);
hb_face_destroy (face);
buffer = hb_buffer_create ();
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
hb_buffer_set_direction (buffer, HB_DIRECTION_RTL);
hb_buffer_set_script (buffer, HB_SCRIPT_ARABIC);
hb_buffer_add (buffer, 0x0660, 0);
hb_buffer_add (buffer, 0x0600, 3);
hb_buffer_add (buffer, 0x0600, 3);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, shapers));
info = hb_buffer_get_glyph_infos (buffer, &length);
position = hb_buffer_get_glyph_positions (buffer, NULL);
g_assert_cmpuint (length, ==, 3);
for (unsigned int i = 0; i < length; i++)
{
g_assert_cmpuint (info[i].cluster, ==, 0);
advance += position[i].x_advance;
}
g_assert_cmpint (advance, ==, 3200);
hb_buffer_destroy (buffer);
hb_font_destroy (font);
}
static void
test_graphite2_inserted_glyph_masks (void)
{
const char *ot_shapers[] = {"ot", NULL};
const char *graphite2_shapers[] = {"graphite2", NULL};
hb_face_t *face;
hb_font_t *font;
hb_buffer_t *buffer;
hb_glyph_info_t *info;
unsigned int length;
face = hb_test_open_font_file ("../subset/data/fonts/Harmattan-Regular.ttf");
font = hb_font_create (face);
hb_face_destroy (face);
buffer = hb_buffer_create ();
/* Leave stale masks in the buffer storage beyond its length: the OpenType
* shaper sets every mask during shaping and only cleans the glyphs that
* survive, so a removed default-ignorable leaves its mask behind. */
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
hb_buffer_set_flags (buffer, HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES);
hb_buffer_add (buffer, 0x0628, 0);
hb_buffer_add (buffer, 0x200B, 1);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, ot_shapers));
info = hb_buffer_get_glyph_infos (buffer, &length);
g_assert_cmpuint (length, ==, 1);
/* Now let Graphite produce more glyphs than there were characters; the
* added glyphs must come back with nothing but glyph flags in their mask. */
hb_buffer_clear_contents (buffer);
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
hb_buffer_set_flags (buffer, HB_BUFFER_FLAG_DEFAULT);
hb_buffer_add (buffer, 0x0622, 0);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, graphite2_shapers));
info = hb_buffer_get_glyph_infos (buffer, &length);
g_assert_cmpuint (length, ==, 2);
for (unsigned int i = 0; i < length; i++)
g_assert_cmphex (info[i].mask & ~HB_GLYPH_FLAG_DEFINED, ==, 0);
hb_buffer_destroy (buffer);
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)
{
hb_test_init (&argc, &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 ();
}