a11y atspi: Don't use char count as byte count

As mentioned in

    commit 368f2af634
    Author: Matthias Clasen <mclasen@redhat.com>
    Date:   Mon Oct 2 08:47:53 2023 -0400

        a11y: Be safe against non-UTF8 text

, the string insertion APIs take string + length
and only insert up to `length` bytes of the
given string.

The AT-SPI "TextChanged" event however
is using a character count, and `emit_text_changed`
also gets called with the character count
along with the string.

However, `g_strndup` used in `emit_text_changed`
so far takes a byte count, not a character count.

Adapt `emit_text_changed` to just use the
passed text as is and make it the responsibility
of the callers to pass only the actually
inserted/removed string.

Most of the callers in `gtk/a11y/gtkatspitext.c`
already did that. Adapt two missing ones to do
likewise.

Fixes: #6151
This commit is contained in:
Michael Weghorn 2023-10-12 11:02:39 +02:00
parent 1297cc188d
commit e39ecbf16d
2 changed files with 8 additions and 3 deletions

View File

@ -715,7 +715,7 @@ emit_text_changed (GtkAtSpiContext *self,
"TextChanged",
g_variant_new ("(siiva{sv})",
kind, start, end,
g_variant_new_take_string (g_strndup (text, end)),
g_variant_new_string (text),
NULL),
NULL);
}

View File

@ -1614,7 +1614,10 @@ insert_text_cb (GtkEditable *editable,
return;
length = g_utf8_strlen (new_text, new_text_length);
changed->text_changed (changed->data, "insert", *position - length, length, new_text);
char *inserted_text = g_utf8_substring (new_text, 0, length);
changed->text_changed (changed->data, "insert", *position - length, length, inserted_text);
g_free (inserted_text);
}
static void
@ -1711,7 +1714,9 @@ insert_range_cb (GtkTextBuffer *buffer,
position = gtk_text_iter_get_offset (iter);
length = g_utf8_strlen (text, len);
changed->text_changed (changed->data, "insert", position - length, length, text);
char *inserted_text = g_utf8_substring (text, 0, length);
changed->text_changed (changed->data, "insert", position - length, length, inserted_text);
g_free (inserted_text);
update_cursor (buffer, changed);
}