From e363fcca3c4ad24ffe41b5026e0d5e2c71e72408 Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Thu, 22 Feb 2024 17:19:09 +0100 Subject: [PATCH] a11y: Report empty string as such While the text data returned by the `get_contents` function from the `GtkAccessibleTextInterface` does not have to be NUL-terminated, `gtk_accessible_text_get_contents` returns the text contents as NUL-terminated UTF-8 data. An empty string (returned as empty, i.e. size = 0, but not NULL GBytes* data by `get_contents`) is valid, and therefore also needs to be NUL-terminated, so do this. Without this, e.g. querying the text of an empty paragraph in the Gtk 4 variant of LibreOffice with the newly added GtkAccessibleInterface implementation [1] gives an incorrect result. Previous sample use in Accerciser's IPython console: In [24]: acc.queryText().getText(0, -1) Out[24]: '[Invalid UTF-8]' With this change in place, it now returns an empty string as expected: In [25]: acc.queryText().getText(0, -1) Out[25]: '' [1] https://git.libreoffice.org/core/commit/e268efd612d12ae9a459d6b9d0cb23220f025163 --- gtk/gtkaccessibletext.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gtk/gtkaccessibletext.c b/gtk/gtkaccessibletext.c index 6a74529069..5bcb712341 100644 --- a/gtk/gtkaccessibletext.c +++ b/gtk/gtkaccessibletext.c @@ -80,12 +80,13 @@ nul_terminate_contents (GBytes *bytes) gsize size; data = g_bytes_get_data (bytes, &size); - if (size > 0 && data[size - 1] != '\0') + if (size == 0 || (size > 0 && data[size - 1] != '\0')) { guchar *copy; copy = g_new (guchar, size + 1); - memcpy (copy, data, size); + if (size > 0) + memcpy (copy, data, size); copy[size] = '\0'; g_bytes_unref (bytes);