From a3b8846e54c7132056411605f815b67e831833d2 Mon Sep 17 00:00:00 2001
From: Philip Withnall <pwithnall@endlessos.org>
Date: Thu, 17 Mar 2022 19:04:42 +0000
Subject: [PATCH] gdbusmethodinvocation: Fix a leak on an early return path

When doing an early return from `g_dbus_method_invocation_return_*()`
due to passing in the wrong type (or no return value when one was
expected), the parameters were not correctly sunk and were leaked.

Fix that. A unit test will be added in a following commit.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Coverity CID: #1474536

Conflict:NA
Reference:https://gitlab.gnome.org/GNOME/glib/-/commit/a3b8846e54c7132056411605f815b67e831833d2

---
 gio/gdbusmethodinvocation.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/gio/gdbusmethodinvocation.c b/gio/gdbusmethodinvocation.c
index c22e19ef0d..c15d83ec84 100644
--- a/gio/gdbusmethodinvocation.c
+++ b/gio/gdbusmethodinvocation.c
@@ -397,14 +397,7 @@ g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocatio
   g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
 
   if (g_dbus_message_get_flags (invocation->message) & G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED)
-    {
-      if (parameters != NULL)
-        {
-          g_variant_ref_sink (parameters);
-          g_variant_unref (parameters);
-        }
-      goto out;
-    }
+    goto out;
 
   if (parameters == NULL)
     parameters = g_variant_new_tuple (NULL, 0);
@@ -508,7 +501,7 @@ g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocatio
     }
 
   reply = g_dbus_message_new_method_reply (invocation->message);
-  g_dbus_message_set_body (reply, parameters);
+  g_dbus_message_set_body (reply, g_steal_pointer (&parameters));
 
 #ifdef G_OS_UNIX
   if (fd_list != NULL)
@@ -525,6 +518,12 @@ g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocatio
   g_object_unref (reply);
 
  out:
+  if (parameters != NULL)
+    {
+      g_variant_ref_sink (parameters);
+      g_variant_unref (parameters);
+    }
+
   g_object_unref (invocation);
 }
 
-- 
GitLab

