提交 d64af6ce 编写于 作者: P Peter Krempa

virsh: modify vshStringToArray to duplicate the elements too

At a slightly larger memory expense allow stealing of items from the
string array returned from vshStringToArray and turn the result into a
string list compatible with virStringSplit. This will allow to use the
common dealloc function.

This patch also fixes a few forgotten checks of return from
vshStringToArray and one memory leak.
上级 a7f94a40
...@@ -161,10 +161,7 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd) ...@@ -161,10 +161,7 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
ret = true; ret = true;
cleanup: cleanup:
if (arr) { virStringFreeList(arr);
VIR_FREE(*arr);
VIR_FREE(arr);
}
virNodeDeviceFree(dev); virNodeDeviceFree(dev);
return ret; return ret;
} }
...@@ -409,7 +406,8 @@ cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -409,7 +406,8 @@ cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
vshError(ctl, "%s", _("Options --tree and --cap are incompatible")); vshError(ctl, "%s", _("Options --tree and --cap are incompatible"));
return false; return false;
} }
ncaps = vshStringToArray(cap_str, &caps); if ((ncaps = vshStringToArray(cap_str, &caps)) < 0)
return false;
} }
for (i = 0; i < ncaps; i++) { for (i = 0; i < ncaps; i++) {
...@@ -503,10 +501,7 @@ cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -503,10 +501,7 @@ cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
} }
cleanup: cleanup:
if (caps) { virStringFreeList(caps);
VIR_FREE(*caps);
VIR_FREE(caps);
}
vshNodeDeviceListFree(list); vshNodeDeviceListFree(list);
return ret; return ret;
} }
...@@ -574,10 +569,7 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd) ...@@ -574,10 +569,7 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
ret = true; ret = true;
cleanup: cleanup:
if (arr) { virStringFreeList(arr);
VIR_FREE(*arr);
VIR_FREE(arr);
}
VIR_FREE(xml); VIR_FREE(xml);
virNodeDeviceFree(device); virNodeDeviceFree(device);
return ret; return ret;
......
...@@ -995,12 +995,13 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -995,12 +995,13 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
char **poolTypes = NULL; char **poolTypes = NULL;
int npoolTypes = 0; int npoolTypes = 0;
npoolTypes = vshStringToArray(type, &poolTypes); if ((npoolTypes = vshStringToArray(type, &poolTypes)) < 0)
return false;
for (i = 0; i < npoolTypes; i++) { for (i = 0; i < npoolTypes; i++) {
if ((poolType = virStoragePoolTypeFromString(poolTypes[i])) < 0) { if ((poolType = virStoragePoolTypeFromString(poolTypes[i])) < 0) {
vshError(ctl, "%s", _("Invalid pool type")); vshError(ctl, "%s", _("Invalid pool type"));
VIR_FREE(poolTypes); virStringFreeList(poolTypes);
return false; return false;
} }
...@@ -1036,10 +1037,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1036,10 +1037,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
break; break;
} }
} }
if (poolTypes) { virStringFreeList(poolTypes);
VIR_FREE(*poolTypes);
VIR_FREE(poolTypes);
}
} }
if (!(list = vshStoragePoolListCollect(ctl, flags))) if (!(list = vshStoragePoolListCollect(ctl, flags)))
......
...@@ -261,10 +261,7 @@ vshParseSnapshotMemspec(vshControl *ctl, virBufferPtr buf, const char *str) ...@@ -261,10 +261,7 @@ vshParseSnapshotMemspec(vshControl *ctl, virBufferPtr buf, const char *str)
cleanup: cleanup:
if (ret < 0) if (ret < 0)
vshError(ctl, _("unable to parse memspec: %s"), str); vshError(ctl, _("unable to parse memspec: %s"), str);
if (array) { virStringFreeList(array);
VIR_FREE(*array);
VIR_FREE(array);
}
return ret; return ret;
} }
...@@ -313,10 +310,7 @@ vshParseSnapshotDiskspec(vshControl *ctl, virBufferPtr buf, const char *str) ...@@ -313,10 +310,7 @@ vshParseSnapshotDiskspec(vshControl *ctl, virBufferPtr buf, const char *str)
cleanup: cleanup:
if (ret < 0) if (ret < 0)
vshError(ctl, _("unable to parse diskspec: %s"), str); vshError(ctl, _("unable to parse diskspec: %s"), str);
if (array) { virStringFreeList(array);
VIR_FREE(*array);
VIR_FREE(array);
}
return ret; return ret;
} }
......
...@@ -163,10 +163,9 @@ vshPrettyCapacity(unsigned long long val, const char **unit) ...@@ -163,10 +163,9 @@ vshPrettyCapacity(unsigned long long val, const char **unit)
} }
/* /*
* Convert the strings separated by ',' into array. The caller * Convert the strings separated by ',' into array. The returned
* must free the first array element and the returned array after * array is a NULL terminated string list. The caller has to free
* use (all other array elements belong to the memory allocated * the array using virStringFreeList or a similar method.
* for the first array element).
* *
* Returns the length of the filled array on success, or -1 * Returns the length of the filled array on success, or -1
* on error. * on error.
...@@ -196,7 +195,8 @@ vshStringToArray(const char *str, ...@@ -196,7 +195,8 @@ vshStringToArray(const char *str,
str_tok++; str_tok++;
} }
if (VIR_ALLOC_N(arr, nstr_tokens) < 0) { /* reserve the NULL element at the end */
if (VIR_ALLOC_N(arr, nstr_tokens + 1) < 0) {
VIR_FREE(str_copied); VIR_FREE(str_copied);
return -1; return -1;
} }
...@@ -212,12 +212,13 @@ vshStringToArray(const char *str, ...@@ -212,12 +212,13 @@ vshStringToArray(const char *str,
continue; continue;
} }
*tmp++ = '\0'; *tmp++ = '\0';
arr[nstr_tokens++] = str_tok; arr[nstr_tokens++] = vshStrdup(NULL, str_tok);
str_tok = tmp; str_tok = tmp;
} }
arr[nstr_tokens++] = str_tok; arr[nstr_tokens++] = vshStrdup(NULL, str_tok);
*array = arr; *array = arr;
VIR_FREE(str_copied);
return nstr_tokens; return nstr_tokens;
} }
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
# include "virerror.h" # include "virerror.h"
# include "virthread.h" # include "virthread.h"
# include "virnetdevbandwidth.h" # include "virnetdevbandwidth.h"
# include "virstring.h"
# define VSH_MAX_XML_FILE (10*1024*1024) # define VSH_MAX_XML_FILE (10*1024*1024)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册