[libvirt] [PATCH 6/7 v6] virsh: Don't motify the const string

Osier Yang jyang at redhat.com
Fri Sep 14 16:21:07 UTC 2012


This improve helper vshStringToArray to accept const string as
argument instead. To not convert the const string when using
vshStringToArray, and thus avoid motifying it.
---
 tools/virsh-domain.c |    7 +++----
 tools/virsh-pool.c   |    3 ++-
 tools/virsh.c        |   10 ++++++----
 tools/virsh.h        |    2 +-
 4 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index c6695b3..3ad02eb 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2336,8 +2336,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
     /* list of volumes to remove along with this domain */
     vshUndefineVolume *vlist = NULL;
     int nvols = 0;
-    const char *volumes_arg = NULL;
-    char *volumes = NULL;
+    const char *volumes = NULL;
     char **volume_tokens = NULL;
     char *volume_tok = NULL;
     int nvolume_tokens = 0;
@@ -2352,8 +2351,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
     int nvolumes = 0;
     bool vol_not_found = false;
 
-    ignore_value(vshCommandOptString(cmd, "storage", &volumes_arg));
-    volumes = vshStrdup(ctl, volumes_arg);
+    ignore_value(vshCommandOptString(cmd, "storage", &volumes));
 
     if (managed_save) {
         flags |= VIR_DOMAIN_UNDEFINE_MANAGED_SAVE;
@@ -2606,6 +2604,7 @@ cleanup:
     VIR_FREE(vlist);
 
     VIR_FREE(volumes);
+    VIR_FREE(*volume_tokens);
     VIR_FREE(volume_tokens);
     VIR_FREE(def);
     VIR_FREE(vol_nodes);
diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c
index bc10f76..f41d01e 100644
--- a/tools/virsh-pool.c
+++ b/tools/virsh-pool.c
@@ -854,7 +854,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
         char **poolTypes = NULL;
         int npoolTypes = 0;
 
-        npoolTypes = vshStringToArray((char *)type, &poolTypes);
+        npoolTypes = vshStringToArray(type, &poolTypes);
 
         for (i = 0; i < npoolTypes; i++) {
             if ((poolType = virStoragePoolTypeFromString(poolTypes[i])) < 0) {
@@ -895,6 +895,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
                 break;
             }
         }
+        VIR_FREE(*poolTypes);
         VIR_FREE(poolTypes);
     }
 
diff --git a/tools/virsh.c b/tools/virsh.c
index 242f789..d0b302a 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -174,19 +174,20 @@ vshPrettyCapacity(unsigned long long val, const char **unit)
  * on error.
  */
 int
-vshStringToArray(char *str,
+vshStringToArray(const char *str,
                  char ***array)
 {
+    char *str_copied = vshStrdup(NULL, str);
     char *str_tok = NULL;
     unsigned int nstr_tokens = 0;
     char **arr = NULL;
 
     /* tokenize the string from user and save it's parts into an array */
-    if (str) {
+    if (str_copied) {
         nstr_tokens = 1;
 
         /* count the delimiters */
-        str_tok = str;
+        str_tok = str_copied;
         while (*str_tok) {
             if (*str_tok == ',')
                 nstr_tokens++;
@@ -195,12 +196,13 @@ vshStringToArray(char *str,
 
         if (VIR_ALLOC_N(arr, nstr_tokens) < 0) {
             virReportOOMError();
+            VIR_FREE(str_copied);
             return -1;
         }
 
         /* tokenize the input string */
         nstr_tokens = 0;
-        str_tok = str;
+        str_tok = str_copied;
         do {
             arr[nstr_tokens] = strsep(&str_tok, ",");
             nstr_tokens++;
diff --git a/tools/virsh.h b/tools/virsh.h
index 30eff4b..1220079 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -330,7 +330,7 @@ int vshAskReedit(vshControl *ctl, const char *msg);
 int vshStreamSink(virStreamPtr st, const char *bytes, size_t nbytes,
                   void *opaque);
 double vshPrettyCapacity(unsigned long long val, const char **unit);
-int vshStringToArray(char *str, char ***array);
+int vshStringToArray(const char *str, char ***array);
 
 /* Typedefs, function prototypes for job progress reporting.
  * There are used by some long lingering commands like
-- 
1.7.7.3




More information about the libvir-list mailing list