conf: Use g_auto* in virCPUDefParseXML

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Jiri Denemark 2020-05-29 14:41:26 +02:00
parent b934d5f42f
commit a08669c31b

View File

@ -324,29 +324,26 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
virCPUType type, virCPUType type,
virCPUDefPtr *cpu) virCPUDefPtr *cpu)
{ {
virCPUDefPtr def = NULL; g_autoptr(virCPUDef) def = NULL;
xmlNodePtr *nodes = NULL; g_autofree xmlNodePtr *nodes = NULL;
VIR_XPATH_NODE_AUTORESTORE(ctxt); VIR_XPATH_NODE_AUTORESTORE(ctxt);
int n; int n;
size_t i; size_t i;
char *cpuMode; g_autofree char *cpuMode = NULL;
char *fallback = NULL; g_autofree char *fallback = NULL;
char *vendor_id = NULL; g_autofree char *vendor_id = NULL;
char *tscScaling = NULL; g_autofree char *tscScaling = NULL;
virHostCPUTscInfoPtr tsc = NULL; virHostCPUTscInfoPtr tsc = NULL;
int ret = -1;
*cpu = NULL; *cpu = NULL;
if (xpath && !(ctxt->node = virXPathNode(xpath, ctxt))) { if (xpath && !(ctxt->node = virXPathNode(xpath, ctxt)))
ret = 0; return 0;
goto cleanup;
}
if (!virXMLNodeNameEqual(ctxt->node, "cpu")) { if (!virXMLNodeNameEqual(ctxt->node, "cpu")) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("XML does not contain expected 'cpu' element")); _("XML does not contain expected 'cpu' element"));
goto cleanup; return -1;
} }
def = virCPUDefNew(); def = virCPUDefNew();
@ -357,7 +354,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("'arch' element cannot be used inside 'cpu'" _("'arch' element cannot be used inside 'cpu'"
" element with 'match' attribute'")); " element with 'match' attribute'"));
goto cleanup; return -1;
} }
def->type = VIR_CPU_TYPE_HOST; def->type = VIR_CPU_TYPE_HOST;
} else { } else {
@ -369,10 +366,9 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
if ((cpuMode = virXMLPropString(ctxt->node, "mode"))) { if ((cpuMode = virXMLPropString(ctxt->node, "mode"))) {
if (def->type == VIR_CPU_TYPE_HOST) { if (def->type == VIR_CPU_TYPE_HOST) {
VIR_FREE(cpuMode);
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Attribute mode is only allowed for guest CPU")); _("Attribute mode is only allowed for guest CPU"));
goto cleanup; return -1;
} else { } else {
def->mode = virCPUModeTypeFromString(cpuMode); def->mode = virCPUModeTypeFromString(cpuMode);
@ -380,10 +376,8 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Invalid mode attribute '%s'"), _("Invalid mode attribute '%s'"),
cpuMode); cpuMode);
VIR_FREE(cpuMode); return -1;
goto cleanup;
} }
VIR_FREE(cpuMode);
} }
} else { } else {
if (def->type == VIR_CPU_TYPE_HOST) if (def->type == VIR_CPU_TYPE_HOST)
@ -393,67 +387,60 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
} }
if (def->type == VIR_CPU_TYPE_GUEST) { if (def->type == VIR_CPU_TYPE_GUEST) {
char *match = virXMLPropString(ctxt->node, "match"); g_autofree char *match = virXMLPropString(ctxt->node, "match");
char *check; g_autofree char *check = NULL;
if (match) { if (match) {
def->match = virCPUMatchTypeFromString(match); def->match = virCPUMatchTypeFromString(match);
VIR_FREE(match);
if (def->match < 0) { if (def->match < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid match attribute for CPU " _("Invalid match attribute for CPU "
"specification")); "specification"));
goto cleanup; return -1;
} }
} }
if ((check = virXMLPropString(ctxt->node, "check"))) { if ((check = virXMLPropString(ctxt->node, "check"))) {
int value = virCPUCheckTypeFromString(check); int value = virCPUCheckTypeFromString(check);
VIR_FREE(check);
if (value < 0) { if (value < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid check attribute for CPU " _("Invalid check attribute for CPU "
"specification")); "specification"));
goto cleanup; return -1;
} }
def->check = value; def->check = value;
} }
} }
if (def->type == VIR_CPU_TYPE_HOST) { if (def->type == VIR_CPU_TYPE_HOST) {
char *arch = virXPathString("string(./arch[1])", ctxt); g_autofree char *arch = virXPathString("string(./arch[1])", ctxt);
if (!arch) { if (!arch) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing CPU architecture")); _("Missing CPU architecture"));
goto cleanup; return -1;
} }
if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) { if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Unknown architecture %s"), arch); _("Unknown architecture %s"), arch);
VIR_FREE(arch); return -1;
goto cleanup;
} }
VIR_FREE(arch);
if (virXPathBoolean("boolean(./microcode[1]/@version)", ctxt) > 0 && if (virXPathBoolean("boolean(./microcode[1]/@version)", ctxt) > 0 &&
virXPathUInt("string(./microcode[1]/@version)", ctxt, virXPathUInt("string(./microcode[1]/@version)", ctxt,
&def->microcodeVersion) < 0) { &def->microcodeVersion) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("invalid microcode version")); _("invalid microcode version"));
goto cleanup; return -1;
} }
if (virXPathBoolean("boolean(./counter[@name='tsc'])", ctxt) > 0) { if (virXPathBoolean("boolean(./counter[@name='tsc'])", ctxt) > 0) {
if (VIR_ALLOC(tsc) < 0) tsc = g_new0(virHostCPUTscInfo, 1);
goto cleanup;
if (virXPathULongLong("string(./counter[@name='tsc']/@frequency)", if (virXPathULongLong("string(./counter[@name='tsc']/@frequency)",
ctxt, &tsc->frequency) < 0) { ctxt, &tsc->frequency) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid TSC frequency")); _("Invalid TSC frequency"));
goto cleanup; return -1;
} }
tscScaling = virXPathString("string(./counter[@name='tsc']/@scaling)", tscScaling = virXPathString("string(./counter[@name='tsc']/@scaling)",
@ -463,7 +450,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
if (scaling < 0) { if (scaling < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid TSC scaling attribute")); _("Invalid TSC scaling attribute"));
goto cleanup; return -1;
} }
tsc->scaling = scaling; tsc->scaling = scaling;
} }
@ -476,7 +463,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
def->type == VIR_CPU_TYPE_HOST) { def->type == VIR_CPU_TYPE_HOST) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing CPU model name")); _("Missing CPU model name"));
goto cleanup; return -1;
} }
if (def->type == VIR_CPU_TYPE_GUEST && if (def->type == VIR_CPU_TYPE_GUEST &&
@ -486,7 +473,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) { if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid fallback attribute")); _("Invalid fallback attribute"));
goto cleanup; return -1;
} }
} }
@ -496,18 +483,17 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("vendor_id must be exactly %d characters long"), _("vendor_id must be exactly %d characters long"),
VIR_CPU_VENDOR_ID_LENGTH); VIR_CPU_VENDOR_ID_LENGTH);
goto cleanup; return -1;
} }
/* ensure that the string can be passed to qemu */ /* ensure that the string can be passed to qemu */
if (strchr(vendor_id, ',')) { if (strchr(vendor_id, ',')) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("vendor id is invalid")); _("vendor id is invalid"));
goto cleanup; return -1;
} }
def->vendor_id = vendor_id; def->vendor_id = g_steal_pointer(&vendor_id);
vendor_id = NULL;
} }
} }
@ -515,7 +501,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
if (def->vendor && !def->model) { if (def->vendor && !def->model) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("CPU vendor specified without CPU model")); _("CPU vendor specified without CPU model"));
goto cleanup; return -1;
} }
if (virXPathNode("./topology[1]", ctxt)) { if (virXPathNode("./topology[1]", ctxt)) {
@ -524,7 +510,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
if (virXPathULong("string(./topology[1]/@sockets)", ctxt, &ul) < 0) { if (virXPathULong("string(./topology[1]/@sockets)", ctxt, &ul) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing 'sockets' attribute in CPU topology")); _("Missing 'sockets' attribute in CPU topology"));
goto cleanup; return -1;
} }
def->sockets = (unsigned int) ul; def->sockets = (unsigned int) ul;
@ -532,7 +518,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
if (virXPathULong("string(./topology[1]/@dies)", ctxt, &ul) < 0) { if (virXPathULong("string(./topology[1]/@dies)", ctxt, &ul) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Malformed 'dies' attribute in CPU topology")); _("Malformed 'dies' attribute in CPU topology"));
goto cleanup; return -1;
} }
def->dies = (unsigned int) ul; def->dies = (unsigned int) ul;
} else { } else {
@ -542,71 +528,69 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
if (virXPathULong("string(./topology[1]/@cores)", ctxt, &ul) < 0) { if (virXPathULong("string(./topology[1]/@cores)", ctxt, &ul) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing 'cores' attribute in CPU topology")); _("Missing 'cores' attribute in CPU topology"));
goto cleanup; return -1;
} }
def->cores = (unsigned int) ul; def->cores = (unsigned int) ul;
if (virXPathULong("string(./topology[1]/@threads)", ctxt, &ul) < 0) { if (virXPathULong("string(./topology[1]/@threads)", ctxt, &ul) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing 'threads' attribute in CPU topology")); _("Missing 'threads' attribute in CPU topology"));
goto cleanup; return -1;
} }
def->threads = (unsigned int) ul; def->threads = (unsigned int) ul;
if (!def->sockets || !def->cores || !def->threads || !def->dies) { if (!def->sockets || !def->cores || !def->threads || !def->dies) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Invalid CPU topology")); _("Invalid CPU topology"));
goto cleanup; return -1;
} }
} }
if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0) if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
goto cleanup; return -1;
if (n > 0) { if (n > 0) {
if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) { if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Non-empty feature list specified without " _("Non-empty feature list specified without "
"CPU model")); "CPU model"));
goto cleanup; return -1;
} }
if (VIR_RESIZE_N(def->features, def->nfeatures_max, if (VIR_RESIZE_N(def->features, def->nfeatures_max,
def->nfeatures, n) < 0) def->nfeatures, n) < 0)
goto cleanup; return -1;
def->nfeatures = n; def->nfeatures = n;
} }
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
char *name; g_autofree char *name = NULL;
int policy; /* enum virDomainCPUFeaturePolicy */ int policy; /* enum virDomainCPUFeaturePolicy */
size_t j; size_t j;
if (def->type == VIR_CPU_TYPE_GUEST) { if (def->type == VIR_CPU_TYPE_GUEST) {
char *strpolicy; g_autofree char *strpolicy = NULL;
strpolicy = virXMLPropString(nodes[i], "policy"); strpolicy = virXMLPropString(nodes[i], "policy");
if (strpolicy == NULL) if (strpolicy == NULL)
policy = VIR_CPU_FEATURE_REQUIRE; policy = VIR_CPU_FEATURE_REQUIRE;
else else
policy = virCPUFeaturePolicyTypeFromString(strpolicy); policy = virCPUFeaturePolicyTypeFromString(strpolicy);
VIR_FREE(strpolicy);
if (policy < 0) { if (policy < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid CPU feature policy")); _("Invalid CPU feature policy"));
goto cleanup; return -1;
} }
} else { } else {
policy = -1; policy = -1;
} }
if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) { if (!(name = virXMLPropString(nodes[i], "name")) || *name == 0) {
VIR_FREE(name);
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Invalid CPU feature name")); _("Invalid CPU feature name"));
goto cleanup; return -1;
} }
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
@ -614,24 +598,23 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("CPU feature '%s' specified more than once"), _("CPU feature '%s' specified more than once"),
name); name);
VIR_FREE(name); return -1;
goto cleanup;
} }
} }
def->features[i].name = name; def->features[i].name = g_steal_pointer(&name);
def->features[i].policy = policy; def->features[i].policy = policy;
} }
if (virXPathInt("count(./cache)", ctxt, &n) < 0) { if (virXPathInt("count(./cache)", ctxt, &n) < 0) {
goto cleanup; return -1;
} else if (n > 1) { } else if (n > 1) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("at most one CPU cache element may be specified")); _("at most one CPU cache element may be specified"));
goto cleanup; return -1;
} else if (n == 1) { } else if (n == 1) {
int level = -1; int level = -1;
char *strmode; g_autofree char *strmode = NULL;
int mode; int mode;
if (virXPathBoolean("boolean(./cache[1]/@level)", ctxt) == 1 && if (virXPathBoolean("boolean(./cache[1]/@level)", ctxt) == 1 &&
@ -639,36 +622,23 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
level < 1 || level > 3)) { level < 1 || level > 3)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("invalid CPU cache level, must be in range [1,3]")); _("invalid CPU cache level, must be in range [1,3]"));
goto cleanup; return -1;
} }
if (!(strmode = virXPathString("string(./cache[1]/@mode)", ctxt)) || if (!(strmode = virXPathString("string(./cache[1]/@mode)", ctxt)) ||
(mode = virCPUCacheModeTypeFromString(strmode)) < 0) { (mode = virCPUCacheModeTypeFromString(strmode)) < 0) {
VIR_FREE(strmode);
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing or invalid CPU cache mode")); _("missing or invalid CPU cache mode"));
goto cleanup; return -1;
} }
VIR_FREE(strmode);
if (VIR_ALLOC(def->cache) < 0)
goto cleanup;
def->cache = g_new0(virCPUCacheDef, 1);
def->cache->level = level; def->cache->level = level;
def->cache->mode = mode; def->cache->mode = mode;
} }
*cpu = g_steal_pointer(&def); *cpu = g_steal_pointer(&def);
ret = 0; return 0;
cleanup:
VIR_FREE(fallback);
VIR_FREE(vendor_id);
VIR_FREE(nodes);
VIR_FREE(tscScaling);
VIR_FREE(tsc);
virCPUDefFree(def);
return ret;
} }