Files
harfbuzz/test/api/test-graphite2.c
T
Behdad EsfahbodandClaude Opus 4.8 b32bfc243d [graphite2] Initialize mask of glyphs the shaper adds
_hb_graphite2_shape() grows buffer->info with ensure() (a plain
realloc) and then fills each output glyph field by field, but never
assigned hb_glyph_info_t::mask.  Entries beyond the original text
length therefore kept uninitialised heap bytes (or, in a reused
buffer, the mask from a previous shaping run); clear_glyph_flags()
only rewrites the low three bits, so hb_buffer_get_glyph_infos()
handed the stale bits 3-31 to the caller.  Shaping output is correct
(every in-tree reader masks down to HB_GLYPH_FLAG_DEFINED), but
valgrind/MSan flag the uninitialised bytes and code reading the
private field sees values that vary run to run.

Set info->mask = 0 in the fill loop, next to the other fields.  That
matches what hb_buffer_t::add() starts from and what the OpenType
shaper returns after hb_propagate_flags(), so the subsequent
clear_glyph_flags()/unsafe_to_break() operate on a defined value and
shaping output is unchanged.  No other shaper needs the change.

Adds test_graphite2_inserted_glyph_masks, which first runs the
OpenType shaper so a removed default-ignorable leaves a non-zero mask
in the buffer storage, then checks that every glyph the Graphite
shaper adds for U+0622 comes back with nothing but glyph-flag bits.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 16:58:47 -06:00

125 lines
4.3 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);
}
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);
return hb_test_run ();
}