From 04891491daee1cfe70aa7652deb09595f379153d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 17 Mar 2012 14:42:05 -0500 Subject: fix variant leaks in menu_add_devices() --- src/indicator-power.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 6140c54..ff56fda 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -615,7 +615,6 @@ static gsize menu_add_devices (GtkMenu *menu, GVariant *devices) { - GVariant *device; gsize n_devices; guint i; @@ -627,8 +626,9 @@ menu_add_devices (GtkMenu *menu, for (i = 0; i < n_devices; i++) { - device = g_variant_get_child_value (devices, i); + GVariant * device = g_variant_get_child_value (devices, i); menu_add_device (menu, device); + g_variant_unref (device); } return n_devices; -- cgit v1.2.3 From a9ab1c3e64481d4ea4cd6d540b0dc75b055193d1 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 17 Mar 2012 14:45:06 -0500 Subject: Fix variant leak in count_batteries() -- the returned value of g_variant_get_child_value() needs to be freed with g_variant_unref() when we're done with it. --- src/indicator-power.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/indicator-power.c b/src/indicator-power.c index ff56fda..d97ecdd 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -1040,6 +1040,8 @@ count_batteries(GVariant *devices, int *total, int *inuse) if ((state == UP_DEVICE_STATE_CHARGING) || (state == UP_DEVICE_STATE_DISCHARGING)) ++*inuse; } + + g_variant_unref (device); } g_debug("count_batteries found %d batteries (%d are charging/discharging)", *total, *inuse); -- cgit v1.2.3 From 22a82f675f9552756ecac17b0a53a09bb20d1b46 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 17 Mar 2012 14:48:58 -0500 Subject: Fix memory leaks in get_primary_device(). 1. All the calls to g_variant_get_child_value() were leaked. Fixed by changing the use to g_variant_get_child() and keeping index values of the interesting children instead of pointers to them. 2. There were several paths where the local string "object_path" and "device_icon" were leaked. (For example, any non-battery entry given to us by upower). Fixed by making these const strings and peeking them from the variant with "&s" instead of "s". --- src/indicator-power.c | 60 ++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index d97ecdd..701415d 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -688,38 +688,34 @@ build_menu (IndicatorPower *self) static GVariant * get_primary_device (GVariant *devices) { - UpDeviceKind kind; - UpDeviceState state; - GVariant *device; - GVariant *primary_device_charging = NULL; - GVariant *primary_device_discharging = NULL; - GVariant *primary_device = NULL; + gint primary_device_charging_index = -1; + gint primary_device_discharging_index = -1; + gint primary_device_index = -1; gboolean charging = FALSE; gboolean discharging = FALSE; - gchar *object_path; - gchar *device_icon; - gdouble percentage; - guint64 time; guint64 min_discharging_time = G_MAXUINT64; guint64 max_charging_time = 0; - gsize n_devices; guint i; - n_devices = devices ? g_variant_n_children (devices) : 0; + const gsize n_devices = devices ? g_variant_n_children (devices) : 0; g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices); for (i = 0; i < n_devices; i++) { - time = 0; - device = g_variant_get_child_value (devices, i); - g_variant_get (device, - "(susdut)", - &object_path, - &kind, - &device_icon, - &percentage, - &state, - &time); + const gchar *object_path; + UpDeviceKind kind; + const gchar *device_icon; + gdouble percentage; + UpDeviceState state; + guint64 time = 0; + + g_variant_get_child (devices, i, "(&su&sdut)", + &object_path, + &kind, + &device_icon, + &percentage, + &state, + &time); g_debug ("%s: got data from object %s", G_STRFUNC, object_path); @@ -738,7 +734,7 @@ get_primary_device (GVariant *devices) if (time < min_discharging_time) { min_discharging_time = time; - primary_device_discharging = device; + primary_device_discharging_index = i; } } else if (state == UP_DEVICE_STATE_CHARGING) @@ -746,33 +742,33 @@ get_primary_device (GVariant *devices) charging = TRUE; if (time == 0) /* Battery broken */ { - primary_device_charging = device; + primary_device_charging_index = i; } if (time > max_charging_time) { max_charging_time = time; - primary_device_charging = device; + primary_device_charging_index = i; } } else { - primary_device = device; + primary_device_index = i; } - - g_free (device_icon); - g_free (object_path); } if (discharging) { - primary_device = primary_device_discharging; + primary_device_index = primary_device_discharging_index; } else if (charging) { - primary_device = primary_device_charging; + primary_device_index = primary_device_charging_index; } - return primary_device; + if (primary_device_index >= 0) + return g_variant_get_child_value (devices, primary_device_index); + + return NULL; } static void -- cgit v1.2.3 From fd5fac8de3af29f2d7a3ccd985092f9e14361026 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 15 Apr 2012 09:17:21 -0500 Subject: fix leaked strings in menu_add_device when (kind == UP_DEVICE_KIND_LINE_POWER) --- src/indicator-power.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 701415d..ea831e5 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -510,7 +510,7 @@ static GIcon* get_device_icon (UpDeviceKind kind, UpDeviceState state, guint64 time_sec, - gchar *device_icon) + const gchar *device_icon) { GIcon *gicon; @@ -553,8 +553,8 @@ menu_add_device (GtkMenu *menu, GtkWidget *details_label; GtkWidget *grid; GIcon *device_gicons; - gchar *device_icon = NULL; - gchar *object_path = NULL; + const gchar *device_icon = NULL; + const gchar *object_path = NULL; gdouble percentage; guint64 time; const gchar *device_name; @@ -566,7 +566,7 @@ menu_add_device (GtkMenu *menu, return; g_variant_get (device, - "(susdut)", + "(&su&sdut)", &object_path, &kind, &device_icon, @@ -607,8 +607,6 @@ menu_add_device (GtkMenu *menu, g_free (short_details); g_free (details); g_free (accessible_name); - g_free (device_icon); - g_free (object_path); } static gsize -- cgit v1.2.3 From 820cc22b429e6b89f107b9d7baf4414e4c49a338 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 15 Apr 2012 09:23:24 -0500 Subject: tweak: in put_primary_device(), peek at the variant's strings instead of dup'ing them --- src/indicator-power.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index cf16fbf..1b3794e 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -785,15 +785,15 @@ put_primary_device (IndicatorPower *self, gchar *short_details = NULL; gchar *details = NULL; gchar *accessible_name = NULL; - gchar *device_icon = NULL; - gchar *object_path = NULL; + const gchar *device_icon = NULL; + const gchar *object_path = NULL; gdouble percentage; guint64 time; const gchar *device_name; /* set the icon and text */ g_variant_get (device, - "(susdut)", + "(&su&sdut)", &object_path, &kind, &device_icon, @@ -825,8 +825,6 @@ put_primary_device (IndicatorPower *self, g_free (short_details); g_free (details); g_free (accessible_name); - g_free (device_icon); - g_free (object_path); } static void -- cgit v1.2.3