]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
virsh: Don't motify the const string
authorOsier Yang <jyang@redhat.com>
Fri, 14 Sep 2012 16:21:07 +0000 (00:21 +0800)
committerOsier Yang <jyang@redhat.com>
Mon, 17 Sep 2012 03:29:31 +0000 (11:29 +0800)
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
tools/virsh-pool.c
tools/virsh.c
tools/virsh.h

index c6695b3514226f20a44bf4bd770a3832df9779a3..887a1560d672d8b093a0ffad0aebb92a7d5ebd17 100644 (file)
@@ -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;
@@ -2605,8 +2603,10 @@ cleanup:
     }
     VIR_FREE(vlist);
 
-    VIR_FREE(volumes);
-    VIR_FREE(volume_tokens);
+    if (volume_tokens) {
+        VIR_FREE(*volume_tokens);
+        VIR_FREE(volume_tokens);
+    }
     VIR_FREE(def);
     VIR_FREE(vol_nodes);
     xmlFreeDoc(doc);
index bc10f76ce0200f2cc5e45ad342be5ec4601458bf..f15c573f1f15b3a72fdcabaa4f976bd66f514242 100644 (file)
@@ -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,7 +895,10 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
                 break;
             }
         }
-        VIR_FREE(poolTypes);
+        if (poolTypes) {
+            VIR_FREE(*poolTypes);
+            VIR_FREE(poolTypes);
+        }
     }
 
     if (!(list = vshStoragePoolListCollect(ctl, flags)))
index 242f7892c16c4c958037e9d5291304e4423a5fef..d0b302a537b28755e1324610b10aa74d4d0ae1ac 100644 (file)
@@ -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++;
index 30eff4bf83d71f98f539ecee9db7a3ebad37417d..1220079420e1220fc993fa8d884f900c66659550 100644 (file)
@@ -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