[buffer] Document message callback lifetime across reset (#6354)

Buffer reset has retained message callbacks since their introduction,
although its documentation promised a freshly created buffer state.
Clarify that callbacks, their associated data, and attached user data
survive reset. Document explicit callback removal and destruction.

Add a regression test for callback-directed lookup skipping after
clear/reset, restoration of ligatures after removal, and exactly-once
destruction on replacement, removal, and final buffer destruction.

Tested with meson test -C build: 287 passed, 1 skipped, 0 failed.
The API test also compiles without buffer messages or OT shaping.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6341
Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-10 16:58:18 -06:00
1 parent 86c92a4735
commit 199cae8b28
2 files changed
+98 -2

No files matched your search

+9 -2
View File
@@ -816,8 +816,12 @@ hb_buffer_create_similar (const hb_buffer_t *src)
* hb_buffer_reset:
* @buffer: An #hb_buffer_t
*
* Resets the buffer to its initial status, as if it was just newly created
* with hb_buffer_create().
* Clears the buffer contents and segment properties, and restores the
* default Unicode functions and buffer settings.
*
* The message callback, its associated data, and any attached user data are
* preserved. To remove the message callback, call
* hb_buffer_set_message_func() with a `NULL` function.
*
* Since: 0.9.2
**/
@@ -2332,6 +2336,9 @@ hb_buffer_t::changed ()
* @destroy: (nullable): The function to call when @user_data is not needed anymore
*
* Sets the implementation function for #hb_buffer_message_func_t.
* The callback and its associated data are preserved by hb_buffer_reset()
* and hb_buffer_clear_contents(). The @destroy function is called when the
* callback is replaced or removed, or when @buffer is destroyed.
*
* Since: 1.1.3
**/
+89
View File
@@ -246,6 +246,92 @@ test_buffer_create_similar (void)
hb_buffer_destroy (b);
}
#if !defined(HB_NO_BUFFER_MESSAGE) && !defined(HB_NO_OT_SHAPE)
typedef struct
{
unsigned int calls;
unsigned int destroyed;
} message_data_t;
static hb_bool_t
buffer_message (hb_buffer_t *buffer HB_UNUSED, hb_font_t *font HB_UNUSED,
const char *message, void *user_data)
{
message_data_t *data = user_data;
data->calls++;
return strncmp (message, "start lookup", 12) != 0;
}
static void
buffer_message_destroy (void *user_data)
{
message_data_t *data = user_data;
data->destroyed++;
}
static void
shape_buffer_message (hb_buffer_t *buffer, hb_font_t *font,
unsigned int expected_length)
{
const char *shapers[] = {"ot", NULL};
hb_buffer_add_utf8 (buffer, "fi", -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), ==, expected_length);
}
static void
test_buffer_message_lifetime (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.gsub.fi.ttf");
hb_font_t *font = hb_font_create (face);
hb_buffer_t *buffer = hb_buffer_create ();
message_data_t first = {0, 0}, second = {0, 0}, last = {0, 0};
hb_user_data_key_t key = {0};
g_assert_true (hb_buffer_set_user_data (buffer, &key, &first, NULL, FALSE));
hb_buffer_set_message_func (buffer, buffer_message, &first, buffer_message_destroy);
shape_buffer_message (buffer, font, 2);
g_assert_cmpuint (first.calls, >, 0);
unsigned int calls = first.calls;
hb_buffer_clear_contents (buffer);
g_assert_cmpuint (first.destroyed, ==, 0);
shape_buffer_message (buffer, font, 2);
g_assert_cmpuint (first.calls, >, calls);
calls = first.calls;
hb_buffer_reset (buffer);
g_assert_cmpuint (first.destroyed, ==, 0);
g_assert_true (hb_buffer_get_user_data (buffer, &key) == &first);
shape_buffer_message (buffer, font, 2);
g_assert_cmpuint (first.calls, >, calls);
hb_buffer_set_message_func (buffer, buffer_message, &second, buffer_message_destroy);
g_assert_cmpuint (first.destroyed, ==, 1);
hb_buffer_clear_contents (buffer);
shape_buffer_message (buffer, font, 2);
g_assert_cmpuint (second.calls, >, 0);
calls = second.calls;
hb_buffer_set_message_func (buffer, NULL, NULL, NULL);
g_assert_cmpuint (second.destroyed, ==, 1);
hb_buffer_clear_contents (buffer);
shape_buffer_message (buffer, font, 1);
g_assert_cmpuint (second.calls, ==, calls);
hb_buffer_set_message_func (buffer, buffer_message, &last, buffer_message_destroy);
hb_buffer_reset (buffer);
g_assert_cmpuint (last.destroyed, ==, 0);
hb_buffer_destroy (buffer);
g_assert_cmpuint (first.destroyed, ==, 1);
g_assert_cmpuint (second.destroyed, ==, 1);
g_assert_cmpuint (last.destroyed, ==, 1);
hb_font_destroy (font);
hb_face_destroy (face);
}
#endif
static void
test_buffer_contents (gpointer fixture_, gconstpointer user_data)
{
@@ -1117,6 +1203,9 @@ main (int argc, char **argv)
hb_test_add (test_buffer_empty);
hb_test_add (test_buffer_diff_positions);
hb_test_add (test_buffer_create_similar);
#if !defined(HB_NO_BUFFER_MESSAGE) && !defined(HB_NO_OT_SHAPE)
hb_test_add (test_buffer_message_lifetime);
#endif
hb_test_add (test_buffer_serialize_deserialize);
hb_test_add (test_buffer_serialize_no_advances);