]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virNetDevVlanParse: Use virXMLProp* helpers instead of XPath lookups
authorPeter Krempa <pkrempa@redhat.com>
Wed, 5 Oct 2022 13:35:42 +0000 (15:35 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 2 Nov 2022 08:20:57 +0000 (09:20 +0100)
The loop inside virNetDevVlanParse fetches multiple attributes from the
element. Convert it to use the virXMLProp* helpers, which also
simplifies error reporting.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/netdev_vlan_conf.c

index 9d7cc732bab2a3fddf20e63259e67ae397f2c688..28a818ad857cb02c133efe136625a9d5a82f4d8d 100644 (file)
@@ -35,7 +35,6 @@ virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)
     int ret = -1;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
     char *trunk = NULL;
-    char *nativeMode = NULL;
     xmlNodePtr *tagNodes = NULL;
     int nTags;
     size_t i;
@@ -57,37 +56,34 @@ virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)
     def->nativeMode = 0;
     def->nativeTag = 0;
     for (i = 0; i < nTags; i++) {
-        unsigned long id;
+        unsigned int nativeMode = 0;
+        int rc;
 
-        ctxt->node = tagNodes[i];
-        if (virXPathULong("string(./@id)", ctxt, &id) < 0) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("missing or invalid vlan tag id attribute"));
+        if (virXMLPropUInt(tagNodes[i], "id", 10, VIR_XML_PROP_REQUIRED,
+                           &def->tag[i]) < 0)
             goto cleanup;
-        }
-        if (id > 4095) {
+
+        if (def->tag[i] > 4095) {
             virReportError(VIR_ERR_XML_ERROR,
-                           _("vlan tag id %lu too large (maximum 4095)"), id);
+                           _("vlan tag id %u too large (maximum 4095)"), def->tag[i]);
             goto cleanup;
         }
-        if ((nativeMode = virXPathString("string(./@nativeMode)", ctxt))) {
+
+        if ((rc = virXMLPropEnum(tagNodes[i], "nativeMode",
+                                 virNativeVlanModeTypeFromString,
+                                 VIR_XML_PROP_NONZERO, &nativeMode)) < 0)
+            return -1;
+
+        if (rc == 1) {
             if (def->nativeMode != 0) {
                 virReportError(VIR_ERR_XML_ERROR, "%s",
                                _("duplicate native vlan setting"));
                 goto cleanup;
             }
-            if ((def->nativeMode
-                 = virNativeVlanModeTypeFromString(nativeMode)) <= 0) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                               _("Invalid \"nativeMode='%s'\" "
-                                 "in vlan <tag> element"),
-                               nativeMode);
-                goto cleanup;
-            }
-            VIR_FREE(nativeMode);
-            def->nativeTag = id;
+
+            def->nativeMode = nativeMode;
+            def->nativeTag = def->tag[i];
         }
-        def->tag[i] = id;
     }
 
     def->nTags = nTags;
@@ -128,7 +124,6 @@ virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)
  cleanup:
     VIR_FREE(tagNodes);
     VIR_FREE(trunk);
-    VIR_FREE(nativeMode);
     if (ret < 0)
         virNetDevVlanClear(def);
     return ret;