[libvirt] [PATCH 2/2] conf: fix formatting of smartcard devices

Ján Tomko jtomko at redhat.com
Thu Aug 3 12:55:31 UTC 2017


My commit 0c1d863 broke formatting of passthrough smartcard devices:
<smartcard mode='passthrough' type='spicevmc'/>

resulted in invalid XML:
    <smartcard mode='passthrough'>
       type='spicevmc'>
      <address type='ccid' controller='0' slot='0'/>
    </smartcard>

Split out chardev source formatting function into two -
one formatting the attributes and other formatting the subelements.

Reported-by: Cole Robinson <crobinso at redhat.com>
---
 src/conf/domain_conf.c | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index eb7052303..640f29d3e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -22804,10 +22804,9 @@ virDomainNetDefFormat(virBufferPtr buf,
 /* Assumes that "<device" has already been generated, and starts
  * output at " type='type'>". */
 static int
-virDomainChrSourceDefFormat(virBufferPtr buf,
-                            virDomainChrSourceDefPtr def,
-                            bool tty_compat,
-                            unsigned int flags)
+virDomainChrAttrsDefFormat(virBufferPtr buf,
+                           virDomainChrSourceDefPtr def,
+                           bool tty_compat)
 {
     const char *type = virDomainChrTypeToString(def->type);
 
@@ -22823,7 +22822,14 @@ virDomainChrSourceDefFormat(virBufferPtr buf,
         virBufferEscapeString(buf, " tty='%s'",
                               def->data.file.path);
     }
-    virBufferAddLit(buf, ">\n");
+    return 0;
+}
+
+static void
+virDomainChrSourceDefFormat(virBufferPtr buf,
+                            virDomainChrSourceDefPtr def,
+                            unsigned int flags)
+{
 
     switch ((virDomainChrType)def->type) {
     case VIR_DOMAIN_CHR_TYPE_NULL:
@@ -22924,7 +22930,7 @@ virDomainChrSourceDefFormat(virBufferPtr buf,
         virBufferAddLit(buf, "/>\n");
     }
 
-    return 0;
+    return;
 }
 
 static int
@@ -22953,8 +22959,10 @@ virDomainChrDefFormat(virBufferPtr buf,
                   def->source->type == VIR_DOMAIN_CHR_TYPE_PTY &&
                   !(flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE) &&
                   def->source->data.file.path);
-    if (virDomainChrSourceDefFormat(buf, def->source, tty_compat, flags) < 0)
+    if (virDomainChrAttrsDefFormat(buf, def->source, tty_compat) < 0)
         return -1;
+    virBufferAddLit(buf, ">\n");
+    virDomainChrSourceDefFormat(buf, def->source, flags);
 
     /* Format <target> block */
     switch (def->deviceType) {
@@ -23067,9 +23075,7 @@ virDomainSmartcardDefFormat(virBufferPtr buf,
         break;
 
     case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
-        if (virDomainChrSourceDefFormat(&childBuf, def->data.passthru, false,
-                                        flags) < 0)
-            return -1;
+        virDomainChrSourceDefFormat(&childBuf, def->data.passthru, flags);
         break;
 
     default:
@@ -23083,6 +23089,10 @@ virDomainSmartcardDefFormat(virBufferPtr buf,
         return -1;
 
     virBufferAsprintf(buf, "<smartcard mode='%s'", mode);
+    if (def->type == VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH &&
+        virDomainChrAttrsDefFormat(buf, def->data.passthru, false) < 0)
+        return -1;
+
     if (virBufferUse(&childBuf)) {
         virBufferAddLit(buf, ">\n");
         virBufferAddBuffer(buf, &childBuf);
@@ -23390,10 +23400,11 @@ virDomainRNGDefFormat(virBufferPtr buf,
         break;
 
     case VIR_DOMAIN_RNG_BACKEND_EGD:
-        virBufferAdjustIndent(buf, 2);
-        if (virDomainChrSourceDefFormat(buf, def->source.chardev,
-                                        false, flags) < 0)
+        if (virDomainChrAttrsDefFormat(buf, def->source.chardev, false) < 0)
             return -1;
+        virBufferAddLit(buf, ">\n");
+        virBufferAdjustIndent(buf, 2);
+        virDomainChrSourceDefFormat(buf, def->source.chardev, flags);
         virBufferAdjustIndent(buf, -2);
         virBufferAddLit(buf, "</backend>\n");
 
@@ -24234,9 +24245,11 @@ virDomainRedirdevDefFormat(virBufferPtr buf,
     bus = virDomainRedirdevBusTypeToString(def->bus);
 
     virBufferAsprintf(buf, "<redirdev bus='%s'", bus);
-    virBufferAdjustIndent(buf, 2);
-    if (virDomainChrSourceDefFormat(buf, def->source, false, flags) < 0)
+    if (virDomainChrAttrsDefFormat(buf, def->source, false) < 0)
         return -1;
+    virBufferAddLit(buf, ">\n");
+    virBufferAdjustIndent(buf, 2);
+    virDomainChrSourceDefFormat(buf, def->source, flags);
     virDomainDeviceInfoFormat(buf, &def->info,
                               flags | VIR_DOMAIN_DEF_FORMAT_ALLOW_BOOT);
     virBufferAdjustIndent(buf, -2);
-- 
2.13.0




More information about the libvir-list mailing list