[PATCH] ui/gtk: Fix GTK assertion failure introduced with clipboard fixes

Jindřich Makovička posted 1 patch 4 weeks, 1 day ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260501-clipboard-assert-fix-v1-1-e549243e4583@gmail.com
Maintainers: "Marc-André Lureau" <marcandre.lureau@redhat.com>
ui/gtk-clipboard.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] ui/gtk: Fix GTK assertion failure introduced with clipboard fixes
Posted by Jindřich Makovička 4 weeks, 1 day ago
gtk_clipboard_request_targets actually returns n_targets == -1
when targets ==  NULL instead of zero. This result in failed assertion
within GTK:

qemu: Gtk: gtk_targets_include_text:
assertion 'targets != NULL || n_targets == 0' failed

Extend the check to require non-null targets and positive n_targets.

Signed-off-by: Jindrich Makovicka <makovick@gmail.com>
---
Hi,

with my latest gtk-clipboard changes, I introduced the following GTK
assertion failures due to unexpected n_targets value returned to
callback from gtk_clipboard_request_targets

qemu: Gtk: gtk_targets_include_text:
assertion 'targets != NULL || n_targets == 0' failed

from

gboolean 
gtk_targets_include_text (GdkAtom *targets,
                          gint     n_targets)
{
  gint i;
  gboolean result = FALSE;

  g_return_val_if_fail (targets != NULL || n_targets == 0, FALSE);

  /* Keep in sync with gtk_target_list_add_text_targets()
   */
 
  init_atoms ();
 
  for (i = 0; i < n_targets; i++)
...

GTK actually returns -1 instead of zero when there are no targets,
triggering the assert. The following patch extends the check to
non-null targets and positive n_targets to be on the safe side.
---
 ui/gtk-clipboard.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ui/gtk-clipboard.c b/ui/gtk-clipboard.c
index 463ed4e905..ea9444be70 100644
--- a/ui/gtk-clipboard.c
+++ b/ui/gtk-clipboard.c
@@ -175,7 +175,7 @@ static void gd_clipboard_owner_change_targets_received_callback(
 {
     QemuClipboardInfo *info = (QemuClipboardInfo *)data;
 
-    if (n_targets) {
+    if (targets && n_targets > 0) {
         if (gtk_targets_include_text(targets, n_targets)) {
             info->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
         }

---
base-commit: 3d626609ccae61a2e552bccd59c7a0931bab8261
change-id: 20260501-clipboard-assert-fix-cc2bf8672eba

Best regards,
-- 
Jindrich Makovicka
Re: [PATCH] ui/gtk: Fix GTK assertion failure introduced with clipboard fixes
Posted by Marc-André Lureau 4 weeks, 1 day ago
Hi

On Fri, May 1, 2026 at 12:10 PM Jindřich Makovička <makovick@gmail.com> wrote:
>
> gtk_clipboard_request_targets actually returns n_targets == -1
> when targets ==  NULL instead of zero. This result in failed assertion
> within GTK:
>
> qemu: Gtk: gtk_targets_include_text:
> assertion 'targets != NULL || n_targets == 0' failed
>
> Extend the check to require non-null targets and positive n_targets.
>
> Signed-off-by: Jindrich Makovicka <makovick@gmail.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
> Hi,
>
> with my latest gtk-clipboard changes, I introduced the following GTK
> assertion failures due to unexpected n_targets value returned to
> callback from gtk_clipboard_request_targets
>
> qemu: Gtk: gtk_targets_include_text:
> assertion 'targets != NULL || n_targets == 0' failed
>
> from
>
> gboolean
> gtk_targets_include_text (GdkAtom *targets,
>                           gint     n_targets)
> {
>   gint i;
>   gboolean result = FALSE;
>
>   g_return_val_if_fail (targets != NULL || n_targets == 0, FALSE);
>
>   /* Keep in sync with gtk_target_list_add_text_targets()
>    */
>
>   init_atoms ();
>
>   for (i = 0; i < n_targets; i++)
> ...
>
> GTK actually returns -1 instead of zero when there are no targets,
> triggering the assert. The following patch extends the check to
> non-null targets and positive n_targets to be on the safe side.
> ---
>  ui/gtk-clipboard.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/ui/gtk-clipboard.c b/ui/gtk-clipboard.c
> index 463ed4e905..ea9444be70 100644
> --- a/ui/gtk-clipboard.c
> +++ b/ui/gtk-clipboard.c
> @@ -175,7 +175,7 @@ static void gd_clipboard_owner_change_targets_received_callback(
>  {
>      QemuClipboardInfo *info = (QemuClipboardInfo *)data;
>
> -    if (n_targets) {
> +    if (targets && n_targets > 0) {
>          if (gtk_targets_include_text(targets, n_targets)) {
>              info->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
>          }
>
> ---
> base-commit: 3d626609ccae61a2e552bccd59c7a0931bab8261
> change-id: 20260501-clipboard-assert-fix-cc2bf8672eba
>
> Best regards,
> --
> Jindrich Makovicka
>