arrfab / rpms / abrt

Forked from rpms/abrt 5 years ago
Clone

Blame SOURCES/0228-Fix-memory-leaks-in-abrt-dbus.patch

b225ea
From 1902735613a3cc4a1c87e8cbae83a7452bfd8327 Mon Sep 17 00:00:00 2001
b225ea
From: Jakub Filak <jfilak@redhat.com>
b225ea
Date: Sun, 1 May 2016 07:13:56 +0200
b225ea
Subject: [PATCH] Fix memory leaks in abrt-dbus
b225ea
b225ea
Fix several repeated leaks that were causing abrt-dbus to waste system
b225ea
memory.
b225ea
b225ea
I used this valgrind command:
b225ea
    valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all \
b225ea
             --track-origins=yes --suppressions=glib.supp \
b225ea
             --log-file=/tmp/leaks-$(date +%s).txt abrt-dbus -vvv -t 10
b225ea
b225ea
With suppressions from libsecret and NetworkManager:
b225ea
  * https://raw.githubusercontent.com/GNOME/libsecret/master/build/glib.supp
b225ea
  * https://raw.githubusercontent.com/NetworkManager/NetworkManager/master/valgrind.suppressions
b225ea
b225ea
The suppressions were needed because Glib allocates a lot of static
b225ea
stuff and does not free it at exit because it is useless.
b225ea
b225ea
Resolves: #1319704
b225ea
b225ea
Signed-off-by: Jakub Filak <jfilak@redhat.com>
b225ea
---
b225ea
 src/dbus/abrt-dbus.c   | 39 ++++++++++++++++++++++-----------------
b225ea
 src/dbus/abrt-polkit.c |  3 +++
b225ea
 src/lib/abrt_conf.c    |  3 +++
b225ea
 src/lib/abrt_glib.c    |  7 +++----
b225ea
 src/lib/problem_api.c  |  1 +
b225ea
 5 files changed, 32 insertions(+), 21 deletions(-)
b225ea
b225ea
diff --git a/src/dbus/abrt-dbus.c b/src/dbus/abrt-dbus.c
b225ea
index 173cec4..0a459cd 100644
b225ea
--- a/src/dbus/abrt-dbus.c
b225ea
+++ b/src/dbus/abrt-dbus.c
b225ea
@@ -97,22 +97,21 @@ static uid_t get_caller_uid(GDBusConnection *connection, GDBusMethodInvocation *
b225ea
     GError *error = NULL;
b225ea
     guint caller_uid;
b225ea
 
b225ea
-    GDBusProxy * proxy = g_dbus_proxy_new_sync(connection,
b225ea
-                                     G_DBUS_PROXY_FLAGS_NONE,
b225ea
-                                     NULL,
b225ea
-                                     "org.freedesktop.DBus",
b225ea
-                                     "/org/freedesktop/DBus",
b225ea
-                                     "org.freedesktop.DBus",
b225ea
-                                     NULL,
b225ea
-                                     &error);
b225ea
-
b225ea
-    GVariant *result = g_dbus_proxy_call_sync(proxy,
b225ea
-                                     "GetConnectionUnixUser",
b225ea
-                                     g_variant_new ("(s)", caller),
b225ea
-                                     G_DBUS_CALL_FLAGS_NONE,
b225ea
-                                     -1,
b225ea
-                                     NULL,
b225ea
-                                     &error);
b225ea
+    /* Proxy isn't necessary if only need to call a single method.  By default
b225ea
+     * GDBusProxy connects to signals and downloads property values. It
b225ea
+     * suppressed by passing flags argument, but not-creating proxy at all is
b225ea
+     * much faster and safer. */
b225ea
+    GVariant *result = g_dbus_connection_call_sync(connection,
b225ea
+                                                   "org.freedesktop.DBus",
b225ea
+                                                   "/org/freedesktop/DBus",
b225ea
+                                                   "org.freedesktop.DBus",
b225ea
+                                                   "GetConnectionUnixUser",
b225ea
+                                                   g_variant_new ("(s)", caller),
b225ea
+                                                   /* reply_type */  NULL,
b225ea
+                                                   G_DBUS_CALL_FLAGS_NONE,
b225ea
+                                                   /* timeout */     -1,
b225ea
+                                                   /* cancellable */ NULL,
b225ea
+                                                   &error);
b225ea
 
b225ea
     if (result == NULL)
b225ea
     {
b225ea
@@ -940,7 +939,11 @@ static void handle_method_call(GDBusConnection *connection,
b225ea
 static gboolean on_timeout_cb(gpointer user_data)
b225ea
 {
b225ea
     g_main_loop_quit(loop);
b225ea
-    return TRUE;
b225ea
+
b225ea
+    /* FALSE -> remove and destroy this source. Without it, the timeout source
b225ea
+     * will be leaked at exit - that isn't a problem but it makes valgrind out
b225ea
+     * less readable. */
b225ea
+    return FALSE;
b225ea
 }
b225ea
 
b225ea
 static const GDBusInterfaceVTable interface_vtable =
b225ea
@@ -1059,6 +1062,8 @@ int main(int argc, char *argv[])
b225ea
 
b225ea
     g_dbus_node_info_unref(introspection_data);
b225ea
 
b225ea
+    g_main_loop_unref(loop);
b225ea
+
b225ea
     free_abrt_conf_data();
b225ea
 
b225ea
     return 0;
b225ea
diff --git a/src/dbus/abrt-polkit.c b/src/dbus/abrt-polkit.c
b225ea
index 39880e5..34af8a4 100644
b225ea
--- a/src/dbus/abrt-polkit.c
b225ea
+++ b/src/dbus/abrt-polkit.c
b225ea
@@ -59,8 +59,11 @@ static PolkitResult do_check(PolkitSubject *subject, const char *action_id)
b225ea
                 POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
b225ea
                 cancellable,
b225ea
                 &error);
b225ea
+
b225ea
+    g_object_unref(cancellable);
b225ea
     g_object_unref(authority);
b225ea
     g_source_remove(cancel_timeout);
b225ea
+    g_object_unref(subject);
b225ea
     if (error)
b225ea
     {
b225ea
         g_error_free(error);
b225ea
diff --git a/src/lib/abrt_conf.c b/src/lib/abrt_conf.c
b225ea
index 4a49032..5ae64c5 100644
b225ea
--- a/src/lib/abrt_conf.c
b225ea
+++ b/src/lib/abrt_conf.c
b225ea
@@ -37,6 +37,9 @@ void free_abrt_conf_data()
b225ea
 
b225ea
     free(g_settings_dump_location);
b225ea
     g_settings_dump_location = NULL;
b225ea
+
b225ea
+    free(g_settings_autoreporting_event);
b225ea
+    g_settings_autoreporting_event = NULL;
b225ea
 }
b225ea
 
b225ea
 static void ParseCommon(map_string_t *settings, const char *conf_filename)
b225ea
diff --git a/src/lib/abrt_glib.c b/src/lib/abrt_glib.c
b225ea
index f7c128e..60e104f 100644
b225ea
--- a/src/lib/abrt_glib.c
b225ea
+++ b/src/lib/abrt_glib.c
b225ea
@@ -22,15 +22,14 @@
b225ea
 GList *string_list_from_variant(GVariant *variant)
b225ea
 {
b225ea
     GList *list = NULL;
b225ea
-    GVariantIter *iter;
b225ea
+    GVariantIter iter;
b225ea
+    g_variant_iter_init(&iter, variant);
b225ea
     gchar *str;
b225ea
-    g_variant_get(variant, "as", &iter);
b225ea
-    while (g_variant_iter_loop(iter, "s", &str))
b225ea
+    while (g_variant_iter_loop(&iter, "s", &str))
b225ea
     {
b225ea
         log_notice("adding: %s", str);
b225ea
         list = g_list_prepend(list, xstrdup(str));
b225ea
     }
b225ea
-    g_variant_unref(variant);
b225ea
 
b225ea
     /* we were prepending items, so we should reverse the list to not confuse people
b225ea
      * by returning items in reversed order than it's in the variant
b225ea
diff --git a/src/lib/problem_api.c b/src/lib/problem_api.c
b225ea
index b343882..9fedb3d 100644
b225ea
--- a/src/lib/problem_api.c
b225ea
+++ b/src/lib/problem_api.c
b225ea
@@ -51,6 +51,7 @@ int for_each_problem_in_dir(const char *path,
b225ea
         if (dir_fd < 0)
b225ea
         {
b225ea
             VERB2 perror_msg("can't open problem directory '%s'", full_name);
b225ea
+            free(full_name);
b225ea
             continue;
b225ea
         }
b225ea
 
b225ea
-- 
b225ea
1.8.3.1
b225ea