572a44
From 10725a4ee35e5d1d97c2f9bc72010c5c0210cd6b Mon Sep 17 00:00:00 2001
a4b143
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
a4b143
Date: Fri, 11 Oct 2013 19:33:20 -0400
a4b143
Subject: [PATCH] dbus-common: avoid leak in error path
a4b143
a4b143
src/shared/dbus-common.c:968:33: warning: Potential leak of memory pointed to by 'l'
a4b143
                        return -EINVAL;
a4b143
                                ^~~~~~
a4b143
---
a4b143
 src/shared/dbus-common.c | 20 ++++++++++----------
a4b143
 1 file changed, 10 insertions(+), 10 deletions(-)
a4b143
a4b143
diff --git a/src/shared/dbus-common.c b/src/shared/dbus-common.c
a4b143
index c727cae..3ba2d87 100644
a4b143
--- a/src/shared/dbus-common.c
a4b143
+++ b/src/shared/dbus-common.c
a4b143
@@ -934,7 +934,7 @@ int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
a4b143
 int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
a4b143
         DBusMessageIter sub, sub2;
a4b143
         unsigned n = 0, i = 0;
a4b143
-        char **l;
a4b143
+        _cleanup_strv_free_ char **l = NULL;
a4b143
 
a4b143
         assert(iter);
a4b143
         assert(_l);
a4b143
@@ -953,6 +953,7 @@ int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
a4b143
         l = new(char*, n*2+1);
a4b143
         if (!l)
a4b143
                 return -ENOMEM;
a4b143
+        l[0] = NULL; /* make sure that l is properly terminated at all times */
a4b143
 
a4b143
         dbus_message_iter_recurse(iter, &sub);
a4b143
 
a4b143
@@ -968,26 +969,25 @@ int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
a4b143
                         return -EINVAL;
a4b143
 
a4b143
                 l[i] = strdup(a);
a4b143
-                if (!l[i]) {
a4b143
-                        strv_free(l);
a4b143
+                if (!l[i])
a4b143
                         return -ENOMEM;
a4b143
-                }
a4b143
+                i++;
a4b143
 
a4b143
-                l[++i] = strdup(b);
a4b143
-                if (!l[i]) {
a4b143
-                        strv_free(l);
a4b143
+                l[i] = strdup(b);
a4b143
+                if (!l[i])
a4b143
                         return -ENOMEM;
a4b143
-                }
a4b143
-
a4b143
                 i++;
a4b143
+
a4b143
                 dbus_message_iter_next(&sub);
a4b143
         }
a4b143
 
a4b143
         assert(i == n*2);
a4b143
         l[i] = NULL;
a4b143
 
a4b143
-        if (_l)
a4b143
+        if (_l) {
a4b143
                 *_l = l;
a4b143
+                l = NULL; /* avoid freeing */
a4b143
+        }
a4b143
 
a4b143
         return 0;
a4b143
 }