dryang / rpms / systemd

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