[libvirt] [PATCH 3/4] conf: cpu: Refactor parsing of vendor_id and fallback attributes

Peter Krempa pkrempa at redhat.com
Mon Dec 17 11:22:04 UTC 2012


This patch simplifies the code that parses the fallback and vendor_id
attributes from the domain xml cpu definition.

Changes done:
- free temp variables in the cleanup section instead of local use
- remove checking for presence of the attribute to directly getting the
value (saving call to virXPathBoolean)
- remove loop used to check for ',' in the vendor_id string with strchr
---
 src/conf/cpu_conf.c | 43 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 26 deletions(-)

diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index 7528980..e5695f4 100644
--- a/src/conf/cpu_conf.c
+++ b/src/conf/cpu_conf.c
@@ -195,6 +195,8 @@ virCPUDefParseXML(const xmlNodePtr node,
     int n;
     unsigned int i;
     char *cpuMode;
+    char *fallback = NULL;
+    char *vendor_id = NULL;

     if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -288,45 +290,32 @@ virCPUDefParseXML(const xmlNodePtr node,
     if (def->type == VIR_CPU_TYPE_GUEST &&
         def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {

-        if (virXPathBoolean("boolean(./model[1]/@fallback)", ctxt)) {
-            const char *fallback;
-
-            fallback = virXPathString("string(./model[1]/@fallback)", ctxt);
-            if (fallback) {
-                def->fallback = virCPUFallbackTypeFromString(fallback);
-                VIR_FREE(fallback);
-                if (def->fallback < 0) {
-                    virReportError(VIR_ERR_XML_ERROR, "%s",
-                                   _("Invalid fallback attribute"));
-                    goto error;
-                }
+        if ((fallback = virXPathString("string(./model[1]/@fallback)", ctxt))) {
+            if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
+                virReportError(VIR_ERR_XML_ERROR, "%s",
+                               _("Invalid fallback attribute"));
+                goto error;
             }
         }

-        if (virXPathBoolean("boolean(./model[1]/@vendor_id)", ctxt)) {
-            char *vendor_id;
-
-            vendor_id = virXPathString("string(./model[1]/@vendor_id)",
-                                       ctxt);
-            if (!vendor_id ||
-                strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
+        if ((vendor_id = virXPathString("string(./model[1]/@vendor_id)",
+                                        ctxt))) {
+            if (strlen(vendor_id) != VIR_CPU_VENDOR_ID_LENGTH) {
                 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_FREE(vendor_id);
                 goto error;
             }
+
             /* ensure that the string can be passed to qemu*/
-            for (i = 0; i < strlen(vendor_id); i++) {
-                if (vendor_id[i]==',') {
+            if (strchr(vendor_id, ',')) {
                     virReportError(VIR_ERR_XML_ERROR, "%s",
                                    _("vendor id is invalid"));
-                    VIR_FREE(vendor_id);
                     goto error;
-                }
             }
+
             def->vendor_id = vendor_id;
+            vendor_id = NULL;
         }
     }

@@ -490,6 +479,8 @@ virCPUDefParseXML(const xmlNodePtr node,
     }

 cleanup:
+    VIR_FREE(fallback);
+    VIR_FREE(vendor_id);
     VIR_FREE(nodes);
     return def;

-- 
1.8.0.2




More information about the libvir-list mailing list