[PATCH 1/7] virPCIDeviceAddressParseXML: Use virXMLNodeGetSubelement to find 'zpci'

Peter Krempa posted 7 patches 3 years ago
[PATCH 1/7] virPCIDeviceAddressParseXML: Use virXMLNodeGetSubelement to find 'zpci'
Posted by Peter Krempa 3 years ago
Use the helper designed to find the subelement. A slight semantic
difference after this patch is that the first <zpci> element will be
considered instead of the last, but only one is expected in a valid XML.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/conf/device_conf.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/src/conf/device_conf.c b/src/conf/device_conf.c
index 762efe5f0b..a116e39c75 100644
--- a/src/conf/device_conf.c
+++ b/src/conf/device_conf.c
@@ -199,8 +199,7 @@ int
 virPCIDeviceAddressParseXML(xmlNodePtr node,
                             virPCIDeviceAddress *addr)
 {
-    xmlNodePtr cur;
-    xmlNodePtr zpci = NULL;
+    xmlNodePtr zpci;

     memset(addr, 0, sizeof(*addr));

@@ -227,18 +226,11 @@ virPCIDeviceAddressParseXML(xmlNodePtr node,
     if (!virPCIDeviceAddressIsEmpty(addr) && !virPCIDeviceAddressIsValid(addr, true))
         return -1;

-    cur = node->children;
-    while (cur) {
-        if (cur->type == XML_ELEMENT_NODE &&
-            virXMLNodeNameEqual(cur, "zpci")) {
-            zpci = cur;
-        }
-        cur = cur->next;
+    if ((zpci = virXMLNodeGetSubelement(node, "zpci"))) {
+        if (virZPCIDeviceAddressParseXML(zpci, addr) < 0)
+            return -1;
     }

-    if (zpci && virZPCIDeviceAddressParseXML(zpci, addr) < 0)
-        return -1;
-
     return 0;
 }

-- 
2.39.1
Re: [PATCH 1/7] virPCIDeviceAddressParseXML: Use virXMLNodeGetSubelement to find 'zpci'
Posted by Martin Kletzander 3 years ago
On Thu, Feb 02, 2023 at 04:18:04PM +0100, Peter Krempa wrote:
>Use the helper designed to find the subelement. A slight semantic
>difference after this patch is that the first <zpci> element will be
>considered instead of the last, but only one is expected in a valid XML.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> src/conf/device_conf.c | 16 ++++------------
> 1 file changed, 4 insertions(+), 12 deletions(-)
>
>diff --git a/src/conf/device_conf.c b/src/conf/device_conf.c
>index 762efe5f0b..a116e39c75 100644
>--- a/src/conf/device_conf.c
>+++ b/src/conf/device_conf.c
>@@ -199,8 +199,7 @@ int
> virPCIDeviceAddressParseXML(xmlNodePtr node,
>                             virPCIDeviceAddress *addr)
> {
>-    xmlNodePtr cur;
>-    xmlNodePtr zpci = NULL;
>+    xmlNodePtr zpci;

You can initialise the variable right away here instead of ...

>
>     memset(addr, 0, sizeof(*addr));
>
>@@ -227,18 +226,11 @@ virPCIDeviceAddressParseXML(xmlNodePtr node,
>     if (!virPCIDeviceAddressIsEmpty(addr) && !virPCIDeviceAddressIsValid(addr, true))
>         return -1;
>
>-    cur = node->children;
>-    while (cur) {
>-        if (cur->type == XML_ELEMENT_NODE &&
>-            virXMLNodeNameEqual(cur, "zpci")) {
>-            zpci = cur;
>-        }
>-        cur = cur->next;
>+    if ((zpci = virXMLNodeGetSubelement(node, "zpci"))) {

... making this look like lisp ;)

in first 4 patches.  But I get it's a subjective preference.

>+        if (virZPCIDeviceAddressParseXML(zpci, addr) < 0)
>+            return -1;
>     }
>
>-    if (zpci && virZPCIDeviceAddressParseXML(zpci, addr) < 0)
>-        return -1;
>-
>     return 0;
> }
>
>-- 
>2.39.1
>