render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
43fe83
From be34cccbd2e9ca0e756c8e40c34d01474e138c31 Mon Sep 17 00:00:00 2001
43fe83
Message-Id: <be34cccbd2e9ca0e756c8e40c34d01474e138c31.1377873640.git.jdenemar@redhat.com>
43fe83
From: Peter Krempa <pkrempa@redhat.com>
43fe83
Date: Wed, 21 Aug 2013 12:43:30 +0200
43fe83
Subject: [PATCH] virsh: modify vshStringToArray to duplicate the elements too
43fe83
43fe83
https://bugzilla.redhat.com/show_bug.cgi?id=999057
43fe83
43fe83
At a slightly larger memory expense allow stealing of items from the
43fe83
string array returned from vshStringToArray and turn the result into a
43fe83
string list compatible with virStringSplit. This will allow to use the
43fe83
common dealloc function.
43fe83
43fe83
This patch also fixes a few forgotten checks of return from
43fe83
vshStringToArray and one memory leak.
43fe83
43fe83
(cherry picked from commit d64af6ce3cce0d353238086c4e1ec0fd59217cf0)
43fe83
---
43fe83
 tools/virsh-nodedev.c  | 18 +++++-------------
43fe83
 tools/virsh-pool.c     | 10 ++++------
43fe83
 tools/virsh-snapshot.c | 10 ++--------
43fe83
 tools/virsh.c          | 15 ++++++++-------
43fe83
 tools/virsh.h          |  1 +
43fe83
 5 files changed, 20 insertions(+), 34 deletions(-)
43fe83
43fe83
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
43fe83
index 3255079..5522405 100644
43fe83
--- a/tools/virsh-nodedev.c
43fe83
+++ b/tools/virsh-nodedev.c
43fe83
@@ -161,10 +161,7 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
43fe83
 
43fe83
     ret = true;
43fe83
 cleanup:
43fe83
-    if (arr) {
43fe83
-        VIR_FREE(*arr);
43fe83
-        VIR_FREE(arr);
43fe83
-    }
43fe83
+    virStringFreeList(arr);
43fe83
     virNodeDeviceFree(dev);
43fe83
     return ret;
43fe83
 }
43fe83
@@ -409,7 +406,8 @@ cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
43fe83
             vshError(ctl, "%s", _("Options --tree and --cap are incompatible"));
43fe83
             return false;
43fe83
         }
43fe83
-        ncaps = vshStringToArray(cap_str, &caps);
43fe83
+        if ((ncaps = vshStringToArray(cap_str, &caps)) < 0)
43fe83
+            return false;
43fe83
     }
43fe83
 
43fe83
     for (i = 0; i < ncaps; i++) {
43fe83
@@ -503,10 +501,7 @@ cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
43fe83
     }
43fe83
 
43fe83
 cleanup:
43fe83
-    if (caps) {
43fe83
-        VIR_FREE(*caps);
43fe83
-        VIR_FREE(caps);
43fe83
-    }
43fe83
+    virStringFreeList(caps);
43fe83
     vshNodeDeviceListFree(list);
43fe83
     return ret;
43fe83
 }
43fe83
@@ -574,10 +569,7 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
43fe83
 
43fe83
     ret = true;
43fe83
 cleanup:
43fe83
-    if (arr) {
43fe83
-        VIR_FREE(*arr);
43fe83
-        VIR_FREE(arr);
43fe83
-    }
43fe83
+    virStringFreeList(arr);
43fe83
     VIR_FREE(xml);
43fe83
     virNodeDeviceFree(device);
43fe83
     return ret;
43fe83
diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c
43fe83
index 1622be2..b8fc8d7 100644
43fe83
--- a/tools/virsh-pool.c
43fe83
+++ b/tools/virsh-pool.c
43fe83
@@ -995,12 +995,13 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
43fe83
         char **poolTypes = NULL;
43fe83
         int npoolTypes = 0;
43fe83
 
43fe83
-        npoolTypes = vshStringToArray(type, &poolTypes);
43fe83
+        if ((npoolTypes = vshStringToArray(type, &poolTypes)) < 0)
43fe83
+            return false;
43fe83
 
43fe83
         for (i = 0; i < npoolTypes; i++) {
43fe83
             if ((poolType = virStoragePoolTypeFromString(poolTypes[i])) < 0) {
43fe83
                 vshError(ctl, "%s", _("Invalid pool type"));
43fe83
-                VIR_FREE(poolTypes);
43fe83
+                virStringFreeList(poolTypes);
43fe83
                 return false;
43fe83
             }
43fe83
 
43fe83
@@ -1036,10 +1037,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
43fe83
                 break;
43fe83
             }
43fe83
         }
43fe83
-        if (poolTypes) {
43fe83
-            VIR_FREE(*poolTypes);
43fe83
-            VIR_FREE(poolTypes);
43fe83
-        }
43fe83
+        virStringFreeList(poolTypes);
43fe83
     }
43fe83
 
43fe83
     if (!(list = vshStoragePoolListCollect(ctl, flags)))
43fe83
diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c
43fe83
index db9715b..e37a5b3 100644
43fe83
--- a/tools/virsh-snapshot.c
43fe83
+++ b/tools/virsh-snapshot.c
43fe83
@@ -261,10 +261,7 @@ vshParseSnapshotMemspec(vshControl *ctl, virBufferPtr buf, const char *str)
43fe83
 cleanup:
43fe83
     if (ret < 0)
43fe83
         vshError(ctl, _("unable to parse memspec: %s"), str);
43fe83
-    if (array) {
43fe83
-        VIR_FREE(*array);
43fe83
-        VIR_FREE(array);
43fe83
-    }
43fe83
+    virStringFreeList(array);
43fe83
     return ret;
43fe83
 }
43fe83
 
43fe83
@@ -313,10 +310,7 @@ vshParseSnapshotDiskspec(vshControl *ctl, virBufferPtr buf, const char *str)
43fe83
 cleanup:
43fe83
     if (ret < 0)
43fe83
         vshError(ctl, _("unable to parse diskspec: %s"), str);
43fe83
-    if (array) {
43fe83
-        VIR_FREE(*array);
43fe83
-        VIR_FREE(array);
43fe83
-    }
43fe83
+    virStringFreeList(array);
43fe83
     return ret;
43fe83
 }
43fe83
 
43fe83
diff --git a/tools/virsh.c b/tools/virsh.c
43fe83
index f65dc79..15f529e 100644
43fe83
--- a/tools/virsh.c
43fe83
+++ b/tools/virsh.c
43fe83
@@ -163,10 +163,9 @@ vshPrettyCapacity(unsigned long long val, const char **unit)
43fe83
 }
43fe83
 
43fe83
 /*
43fe83
- * Convert the strings separated by ',' into array. The caller
43fe83
- * must free the first array element and the returned array after
43fe83
- * use (all other array elements belong to the memory allocated
43fe83
- * for the first array element).
43fe83
+ * Convert the strings separated by ',' into array. The returned
43fe83
+ * array is a NULL terminated string list. The caller has to free
43fe83
+ * the array using virStringFreeList or a similar method.
43fe83
  *
43fe83
  * Returns the length of the filled array on success, or -1
43fe83
  * on error.
43fe83
@@ -196,7 +195,8 @@ vshStringToArray(const char *str,
43fe83
         str_tok++;
43fe83
     }
43fe83
 
43fe83
-    if (VIR_ALLOC_N(arr, nstr_tokens) < 0) {
43fe83
+    /* reserve the NULL element at the end */
43fe83
+    if (VIR_ALLOC_N(arr, nstr_tokens + 1) < 0) {
43fe83
         VIR_FREE(str_copied);
43fe83
         return -1;
43fe83
     }
43fe83
@@ -212,12 +212,13 @@ vshStringToArray(const char *str,
43fe83
             continue;
43fe83
         }
43fe83
         *tmp++ = '\0';
43fe83
-        arr[nstr_tokens++] = str_tok;
43fe83
+        arr[nstr_tokens++] = vshStrdup(NULL, str_tok);
43fe83
         str_tok = tmp;
43fe83
     }
43fe83
-    arr[nstr_tokens++] = str_tok;
43fe83
+    arr[nstr_tokens++] = vshStrdup(NULL, str_tok);
43fe83
 
43fe83
     *array = arr;
43fe83
+    VIR_FREE(str_copied);
43fe83
     return nstr_tokens;
43fe83
 }
43fe83
 
43fe83
diff --git a/tools/virsh.h b/tools/virsh.h
43fe83
index a407428..466ca2d 100644
43fe83
--- a/tools/virsh.h
43fe83
+++ b/tools/virsh.h
43fe83
@@ -37,6 +37,7 @@
43fe83
 # include "virerror.h"
43fe83
 # include "virthread.h"
43fe83
 # include "virnetdevbandwidth.h"
43fe83
+# include "virstring.h"
43fe83
 
43fe83
 # define VSH_MAX_XML_FILE (10*1024*1024)
43fe83
 
43fe83
-- 
43fe83
1.8.3.2
43fe83