File libvirt-virNetDev-Replace-Restore-MacAddress-Fix-memory-leak.patch of Package libvirt

From 69230763e119871100a026d812557f88a9b85420 Mon Sep 17 00:00:00 2001
Message-Id: <69230763e119871100a026d812557f88a9b85420@dist-git>
From: "Wangrui (K)" <moon.wangrui@huawei.com>
Date: Tue, 29 Sep 2015 14:26:19 -0400
Subject: [PATCH] virNetDev{Replace, Restore}MacAddress: Fix memory leak

Functions virNetDevRestoreMacAddress() and virNetDevRestoreMacAddress()
allocate memory for variable @path using virAsprintf(), but they
haven't freed that memory before returning out.

Prerequisite for the fix for:

  https://bugzilla.redhat.com/show_bug.cgi?id=1251532

Signed-off-by: Zhang bo <oscar.zhangbo@huawei.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit 79bef03a2ca8a8fd570f3f8bfd4aa35c68527f4a)
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/util/virnetdev.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 123c262..5dda6b2 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -250,11 +250,11 @@ virNetDevReplaceMacAddress(const char *linkdev,
     virMacAddr oldmac;
     char *path = NULL;
     char macstr[VIR_MAC_STRING_BUFLEN];
+    int ret = -1;
 
     if (virNetDevGetMAC(linkdev, &oldmac) < 0)
         return -1;
 
-
     if (virAsprintf(&path, "%s/%s",
                     stateDir,
                     linkdev) < 0) {
@@ -265,13 +265,17 @@ virNetDevReplaceMacAddress(const char *linkdev,
     if (virFileWriteStr(path, macstr, O_CREAT|O_TRUNC|O_WRONLY) < 0) {
         virReportSystemError(errno, _("Unable to preserve mac for %s"),
                              linkdev);
-        return -1;
+        goto cleanup;
     }
 
     if (virNetDevSetMAC(linkdev, macaddress) < 0)
-        return -1;
+        goto cleanup;
 
-    return 0;
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(path);
+    return ret;
 }
 
 /**
@@ -286,7 +290,7 @@ static int
 virNetDevRestoreMacAddress(const char *linkdev,
                            const char *stateDir)
 {
-    int rc;
+    int rc = -1;
     char *oldmacname = NULL;
     char *macstr = NULL;
     char *path = NULL;
@@ -300,21 +304,22 @@ virNetDevRestoreMacAddress(const char *linkdev,
     }
 
     if (virFileReadAll(path, VIR_MAC_STRING_BUFLEN, &macstr) < 0)
-        return -1;
+        goto cleanup;
 
     if (virMacAddrParse(macstr, &oldmac) != 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Cannot parse MAC address from '%s'"),
                        oldmacname);
-        VIR_FREE(macstr);
-        return -1;
+        goto cleanup;
     }
 
     /*reset mac and remove file-ignore results*/
     rc = virNetDevSetMAC(linkdev, &oldmac);
     ignore_value(unlink(path));
+
+ cleanup:
     VIR_FREE(macstr);
-
+    VIR_FREE(path);
     return rc;
 }
 
-- 
2.6.2

openSUSE Build Service is sponsored by