Blob Blame History Raw
From 8ce4a7267b3e22e1633f2cfdc1898e52c54fb254 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Fri, 11 Oct 2013 19:33:20 -0400
Subject: [PATCH] dbus-common: avoid leak in error path

src/shared/dbus-common.c:968:33: warning: Potential leak of memory pointed to by 'l'
                        return -EINVAL;
                                ^~~~~~
---
 src/shared/dbus-common.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/shared/dbus-common.c b/src/shared/dbus-common.c
index c727cae..3ba2d87 100644
--- a/src/shared/dbus-common.c
+++ b/src/shared/dbus-common.c
@@ -934,7 +934,7 @@ int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
 int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
         DBusMessageIter sub, sub2;
         unsigned n = 0, i = 0;
-        char **l;
+        _cleanup_strv_free_ char **l = NULL;
 
         assert(iter);
         assert(_l);
@@ -953,6 +953,7 @@ int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
         l = new(char*, n*2+1);
         if (!l)
                 return -ENOMEM;
+        l[0] = NULL; /* make sure that l is properly terminated at all times */
 
         dbus_message_iter_recurse(iter, &sub);
 
@@ -968,26 +969,25 @@ int bus_parse_strv_pairs_iter(DBusMessageIter *iter, char ***_l) {
                         return -EINVAL;
 
                 l[i] = strdup(a);
-                if (!l[i]) {
-                        strv_free(l);
+                if (!l[i])
                         return -ENOMEM;
-                }
+                i++;
 
-                l[++i] = strdup(b);
-                if (!l[i]) {
-                        strv_free(l);
+                l[i] = strdup(b);
+                if (!l[i])
                         return -ENOMEM;
-                }
-
                 i++;
+
                 dbus_message_iter_next(&sub);
         }
 
         assert(i == n*2);
         l[i] = NULL;
 
-        if (_l)
+        if (_l) {
                 *_l = l;
+                l = NULL; /* avoid freeing */
+        }
 
         return 0;
 }