GTK_TEXT_VIEW_UNDO failed when delete more than 23 chars by press key<backspace>
Steps to reproduce
- gtk4-demo --run=textundo
- press key more then 23 times
- press Ctrl+z
Current behavior
undo failed! add get error output on terminal: (gtk4-demo:34840): Gtk-CRITICAL **: 23:09:41.127: gtk_text_buffer_emit_insert: assertion 'g_utf8_validate (text, len, NULL)' failed
Expected outcome
Version information
GTK 4.14.3 linux(fedora 40)
Additional information
I had fixed the bug:
diff --git a/gtk/gtkistringprivate.h b/gtk/gtkistringprivate.h
index 9d5045316a..052d39cddb 100644
--- a/gtk/gtkistringprivate.h
+++ b/gtk/gtkistringprivate.h
@@ -117,25 +117,25 @@ static inline void
istring_prepend (IString *str,
IString *other)
{
- if G_LIKELY (str->n_bytes + other->n_bytes < sizeof str->u.buf - 1)
+ if G_LIKELY (str->n_bytes + other->n_bytes < sizeof str->u.buf)
{
memmove (str->u.buf + other->n_bytes, str->u.buf, str->n_bytes);
memcpy (str->u.buf, other->u.buf, other->n_bytes);
str->n_bytes += other->n_bytes;
str->n_chars += other->n_chars;
str->u.buf[str->n_bytes] = 0;
}
else
{
char *old = NULL;
if (!istring_is_inline (str))
old = str->u.str;
str->u.str = g_strconcat (istring_str (other), istring_str (str), NULL);
str->n_bytes += other->n_bytes;
str->n_chars += other->n_chars;
g_free (old);
}
}
----------------------------------------------diff end ---------------------------------------------------
Root Cause:
When ((str->n_bytes== (si2eof str->u.buf - 2) AND (other->n_bytes == 1)),
the str->u.str was created, and the str->n_byts be set to (si2eof str->u.buf - 1).
But (str->n_byts <= (si2eof str->u.buf - 1)),str is inline status.
Then next key was pressed,the function[istring_prepend] is called again.
at the moment, istring_str (str) return str->u.buf but the str->u.str,
becase of str is inline status.And the buf is changed by pre step action.
Edited by Xulong Cao