[PATCH] location-gps-nmea: fix memory leaks

Ben Chan benchan at chromium.org
Thu Aug 3 20:25:35 UTC 2017


This patch fixes the following potential memory leaks in
MMLocationGpsNmea:

- When the `trace' string provided to location_gps_nmea_take_trace() isn't
  added to the hash table, its ownership is still considered transferred.
  It should thus be freed. Similarly, the `trace_type' string isn't
  added the hash table and should thus be freed.

- mm_location_gps_nmea_add_trace() duplicates a given trace string and
  then passes the trace copy to location_gps_nmea_take_trace(). When
  location_gps_nmea_take_trace() returns FALSE, the ownership of the
  copy isn't transferred. mm_location_gps_nmea_add_trace() should thus
  free the copy.
---
 libmm-glib/mm-location-gps-nmea.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/libmm-glib/mm-location-gps-nmea.c b/libmm-glib/mm-location-gps-nmea.c
index 08ec97ff..577a5d90 100644
--- a/libmm-glib/mm-location-gps-nmea.c
+++ b/libmm-glib/mm-location-gps-nmea.c
@@ -96,8 +96,11 @@ location_gps_nmea_take_trace (MMLocationGpsNmea *self,
             gchar *sequence;
 
             /* Skip the trace if we already have it there */
-            if (strstr (previous, trace))
+            if (strstr (previous, trace)) {
+                g_free (trace_type);
+                g_free (trace);
                 return TRUE;
+            }
 
             sequence = g_strdup_printf ("%s%s%s",
                                         previous,
@@ -118,7 +121,14 @@ gboolean
 mm_location_gps_nmea_add_trace (MMLocationGpsNmea *self,
                                 const gchar *trace)
 {
-    return location_gps_nmea_take_trace (self, g_strdup (trace));
+    char *trace_copy;
+
+    trace_copy = g_strdup (trace);
+    if (location_gps_nmea_take_trace (self, trace_copy))
+        return TRUE;
+
+    g_free (trace_copy);
+    return FALSE;
 }
 
 /*****************************************************************************/
-- 
2.14.0.rc1.383.gd1ce394fe2-goog



More information about the ModemManager-devel mailing list