From nobody Fri May 17 03:39:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621936138; cv=none; d=zohomail.com; s=zohoarc; b=EKgYXZ1TUpgvmTX+Xl/lGpyczFzdHz0Th8mmDrGDk+LaEU8z0fcGAEm1y5gBzi4ZDhZo/Doivz1xYq8ukHE59K7Li+HpPIp2ccnAp3PE2bv//DEvWt9LExxwrtva1BjR5pGwxb60U3hubsBzNc0zCq6XychVaz7qSuwmYt/8urA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621936138; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=MRpPj8M2wY9RcMQHwzrBjlb9Xy1AC7w+Ut1LgKn/l9Y=; b=eVYvDBqBi1ZMCnuKcJQ+Zi8HYBvGOrTcieOJmiKPdBtc5zRu49twthxIuWt5Jlvd+N6W9UIf2PgCincR3/YzJ/NZxVX29wV4OWohepT+4U0c7SdFBM3wzONFLdZ8N8ollgRbx/LdDVnkJjmafzeg/ZM8K/bHA8pTrzI3hfWCQKU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1621936138331260.6843748187882; Tue, 25 May 2021 02:48:58 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-570-o-khSElOMqOzQYqwRBIeQA-1; Tue, 25 May 2021 05:48:55 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 87359180FD69; Tue, 25 May 2021 09:48:50 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5F17F18026; Tue, 25 May 2021 09:48:50 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 282CC180B463; Tue, 25 May 2021 09:48:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14P9mYf3031349 for ; Tue, 25 May 2021 05:48:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id CD76B6E6F5; Tue, 25 May 2021 09:48:34 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.195.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 525C86C332 for ; Tue, 25 May 2021 09:48:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621936137; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=MRpPj8M2wY9RcMQHwzrBjlb9Xy1AC7w+Ut1LgKn/l9Y=; b=CaBaJWNT3hEEg9IVDl0Ul4eh09GQbvl26vHuvblkiwA3zRC4rROQt7AsZ0VVkc3YHWwEXS uX1d3kTh9U09uzCaBZoE762bakBEJFfeeYWDEP+2JfjwKrKL+Z5tZe1ehlrvUGZHBbIrem a7v2H0R7GQbrl+4eyj1QjU4qAU7V8C4= X-MC-Unique: o-khSElOMqOzQYqwRBIeQA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 1/3] virxml: Introduce and use virXMLFormatMetadata() Date: Tue, 25 May 2021 11:48:26 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" So far, we have to places where we format into XMLs: domain and network. Bot places share the same code. Move it into a helper function and just call it from those places. Signed-off-by: Michal Privoznik Reviewed-by: Pavel Hrdina --- src/conf/domain_conf.c | 23 ++------------------- src/conf/network_conf.c | 23 ++------------------- src/libvirt_private.syms | 1 + src/util/virxml.c | 43 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 3 +++ 5 files changed, 51 insertions(+), 42 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 4f78b7b43d..413c44ac61 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -27806,27 +27806,8 @@ virDomainDefFormatInternalSetRootName(virDomainDef= *def, virBufferEscapeString(buf, "%s\n", def->description); =20 - if (def->metadata) { - g_autoptr(xmlBuffer) xmlbuf =3D NULL; - int oldIndentTreeOutput =3D xmlIndentTreeOutput; - - /* Indentation on output requires that we previously set - * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2 - * spaces per level of indentation of intermediate elements, - * but no leading indentation before the starting element. - * Thankfully, libxml maps what looks like globals into - * thread-local uses, so we are thread-safe. */ - xmlIndentTreeOutput =3D 1; - xmlbuf =3D virXMLBufferCreate(); - - if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata, - virBufferGetIndent(buf) / 2, 1) < 0) { - xmlIndentTreeOutput =3D oldIndentTreeOutput; - return -1; - } - virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); - xmlIndentTreeOutput =3D oldIndentTreeOutput; - } + if (virXMLFormatMetadata(buf, def->metadata) < 0) + return -1; =20 if (virDomainDefHasMemoryHotplug(def)) { virBufferAsprintf(buf, diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index a9eadff29c..b10ff5c7a8 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -2486,27 +2486,8 @@ virNetworkDefFormatBuf(virBuffer *buf, virUUIDFormat(uuid, uuidstr); virBufferAsprintf(buf, "%s\n", uuidstr); =20 - if (def->metadata) { - g_autoptr(xmlBuffer) xmlbuf =3D NULL; - int oldIndentTreeOutput =3D xmlIndentTreeOutput; - - /* Indentation on output requires that we previously set - * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2 - * spaces per level of indentation of intermediate elements, - * but no leading indentation before the starting element. - * Thankfully, libxml maps what looks like globals into - * thread-local uses, so we are thread-safe. */ - xmlIndentTreeOutput =3D 1; - xmlbuf =3D virXMLBufferCreate(); - - if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata, - virBufferGetIndent(buf) / 2, 1) < 0) { - xmlIndentTreeOutput =3D oldIndentTreeOutput; - return -1; - } - virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); - xmlIndentTreeOutput =3D oldIndentTreeOutput; - } + if (virXMLFormatMetadata(buf, def->metadata) < 0) + return -1; =20 if (def->forward.type !=3D VIR_NETWORK_FORWARD_NONE) { const char *dev =3D NULL; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e1aef5267e..37515f80ec 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3555,6 +3555,7 @@ virXMLCheckIllegalChars; virXMLExtractNamespaceXML; virXMLFormatElement; virXMLFormatElementEmpty; +virXMLFormatMetadata; virXMLNewNode; virXMLNodeContentString; virXMLNodeNameEqual; diff --git a/src/util/virxml.c b/src/util/virxml.c index 8dcece704a..062a5402f6 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1707,6 +1707,49 @@ virXMLFormatElement(virBuffer *buf, } =20 =20 +/** + * virXMLFormatMetadata: + * @buf: the parent buffer where the element will be placed + * @metadata: pointer to metadata node + * + * Helper to format metadata element. If @metadata is NULL then + * this function is a NOP. + * + * Returns: 0 on success, + * -1 otherwise. + */ +int +virXMLFormatMetadata(virBuffer *buf, + xmlNodePtr metadata) +{ + g_autoptr(xmlBuffer) xmlbuf =3D NULL; + int oldIndentTreeOutput =3D xmlIndentTreeOutput; + + if (!metadata) + return 0; + + /* Indentation on output requires that we previously set + * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2 + * spaces per level of indentation of intermediate elements, + * but no leading indentation before the starting element. + * Thankfully, libxml maps what looks like globals into + * thread-local uses, so we are thread-safe. */ + xmlIndentTreeOutput =3D 1; + xmlbuf =3D virXMLBufferCreate(); + + if (xmlNodeDump(xmlbuf, metadata->doc, metadata, + virBufferGetIndent(buf) / 2, 1) < 0) { + xmlIndentTreeOutput =3D oldIndentTreeOutput; + return -1; + } + + virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); + xmlIndentTreeOutput =3D oldIndentTreeOutput; + + return 0; +} + + void virXPathContextNodeRestore(virXPathContextNodeSave *save) { diff --git a/src/util/virxml.h b/src/util/virxml.h index ed02abd2e9..0bb0d1c118 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -333,6 +333,9 @@ virXMLFormatElementEmpty(virBuffer *buf, virBuffer *attrBuf, virBuffer *childBuf); =20 +int +virXMLFormatMetadata(virBuffer *buf, + xmlNodePtr metadata); =20 struct _virXPathContextNodeSave { xmlXPathContextPtr ctxt; --=20 2.26.3 From nobody Fri May 17 03:39:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621936144; cv=none; d=zohomail.com; s=zohoarc; b=eGjJ/MYHP9b/hWYLavxEilmwi2NwaFbORbx99SMRaKTDV7uUE+4AWUcSN3gNWHkbeGAVjHwcvZcKYG5wwnsZao/ncSq136UwkEg1rjOMFrkxxcQSXfE7sk06tl7p2l48DENKKIBtPEUa8Q/AoEHkGFfErFB79bAt8wFx7Gt9J0g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621936144; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=1QsUg0BP8h//ATOoYuhqxjSJKt6kjTbYa0hkueQuv2I=; b=keuiF9B1uYAvUczhnlq3/TqsqUjL4EA+HTbIDCjc6raPDyY677rW4y/3XvihUUb+kFt+Ln985ghO/9RJiqCf3/9UOWu7b/1AMixv/1GCakpN9AeBNGRWDNabOXB75YSU6TJTf36m4gnRxfxfGz2zu9ZOSsn/7dweQiNXSCfBvFk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1621936144025887.5192610961184; Tue, 25 May 2021 02:49:04 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-335-xPLN7kAOPduQ-vt30f1yYQ-1; Tue, 25 May 2021 05:49:00 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 56AFE800FF0; Tue, 25 May 2021 09:48:54 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 36E4A70585; Tue, 25 May 2021 09:48:54 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D107655341; Tue, 25 May 2021 09:48:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14P9mZq8031358 for ; Tue, 25 May 2021 05:48:35 -0400 Received: by smtp.corp.redhat.com (Postfix) id A4A7919CB6; Tue, 25 May 2021 09:48:35 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.195.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 292286C332 for ; Tue, 25 May 2021 09:48:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621936143; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=1QsUg0BP8h//ATOoYuhqxjSJKt6kjTbYa0hkueQuv2I=; b=gdAF70HudmLX309GiTaA0KO2arhNy119DA+jtqnOBDAzsL7QBSZLBs3TXkZ8AA6Fzdukd8 NXpMjZGdSou8v8Hwdqrl28hP7aX3wojvpzBcAmgCZevhp5lW5PXJVG8cjMa01xxcHfan6K iVlKQC3OdTM1/hwNhNgObpStbXO2woM= X-MC-Unique: xPLN7kAOPduQ-vt30f1yYQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 2/3] virxml: Report error if virXMLFormatMetadata() fails Date: Tue, 25 May 2021 11:48:27 +0200 Message-Id: <5b5a49d66b8123fed529dc8f707c601411517699.1621936048.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" I guess this is more of an academic problem, because if content was problematic we would have caught the error during parsing. Anyway, as is this function returns -1 without any error reported. Fix it by reporting one. Signed-off-by: Michal Privoznik Reviewed-by: Pavel Hrdina --- src/util/virxml.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/util/virxml.c b/src/util/virxml.c index 062a5402f6..91c6f6b02e 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1740,6 +1740,8 @@ virXMLFormatMetadata(virBuffer *buf, if (xmlNodeDump(xmlbuf, metadata->doc, metadata, virBufferGetIndent(buf) / 2, 1) < 0) { xmlIndentTreeOutput =3D oldIndentTreeOutput; + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to format metadata element")); return -1; } =20 --=20 2.26.3 From nobody Fri May 17 03:39:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621936146; cv=none; d=zohomail.com; s=zohoarc; b=mwQGWrRvXMReAZZWOGk4dOz02GCpXoEn4eP0NBcgh1BUcZTutuSs9j05b74nM0qsNi3MHpOSxVNIpl47x/l+1AhA0jQWV3dQptfbM638eJFMPmM/fuHwz4UIJdem/6HjIytQQ7MaJ+xPPUbXuBWpKAckaztXKDWDGUD8uoWrrtI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621936146; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=BM3XsAz22WgS2PNBEKpEy3D9DIh2j3WaliQviBcspBQ=; b=mldLdbv0QlaCBFeAgxWLl4pcwqiyX2UKTdv4mzDTw19LCLUSneznaSQr9Zc9lJHQnjIJyw6W6eNide0G06MszISDQhXHkdsukGH15lhwtjhChsK7m69P9dcGdoEeB3xMVptTbLsHJ0stKuoa82sU+pny2QythfQnaS7QDkrkuvQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1621936146221924.4489506611367; Tue, 25 May 2021 02:49:06 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-220-it58kgz-MhyBhdWSDM0GrA-1; Tue, 25 May 2021 05:49:03 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6BAB8107ACE4; Tue, 25 May 2021 09:48:58 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 49DA07058A; Tue, 25 May 2021 09:48:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0FA4B55348; Tue, 25 May 2021 09:48:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14P9maAL031366 for ; Tue, 25 May 2021 05:48:36 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7A5A7690FA; Tue, 25 May 2021 09:48:36 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.195.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id F3BAA19CB6 for ; Tue, 25 May 2021 09:48:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621936145; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=BM3XsAz22WgS2PNBEKpEy3D9DIh2j3WaliQviBcspBQ=; b=cL7Cqz39jo0D/t1XEcUR5gVJdpPWM8b6PUlbtUtEU5kmQjn3gqwjetJZj2OnIqAse9xEUR B/MLJ/cWQF5iiJq6430wtmzqeYqAG0bgV0u+h8CvTitVKDYaVhq+r5olvXPZk7gxxUel7c KmnztR/jqENd7U7BS29u9NNgtlHUOgQ= X-MC-Unique: it58kgz-MhyBhdWSDM0GrA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 3/3] virxml: Avoid double indentation of element Date: Tue, 25 May 2021 11:48:28 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" There was a recent change in libxml2 that caused a trouble for us. To us, in domain or network XMLs are just opaque value where management application can store whatever data it finds fit. At XML parser/formatter level, we just make a copy of the element during parsing and then format it back. For formatting we use xmlNodeDump() which allows caller to specify level of indentation. Previously, the indentation was not applied onto the very first line, but as of v2.9.12-2-g85b1792e libxml2 is applying indentation also on the first line. This does not work well with out virBuffer because as soon as we call virBufferAsprintf() to append element, virBufferAsprintf() will apply another level of indentation. Instead of version checking, let's skip any indentation added by libxml2 before virBufferAsprintf() is called. Note, the problem is only when telling xmlNodeDump() to use indentation, i.e. level argument is not zero. Therefore, virXMLNodeToString() which also calls xmlNodeDump() is safe as it passes zero. Tested-by: Bjoern Walk Signed-off-by: Michal Privoznik Reviewed-by: Pavel Hrdina --- src/util/virxml.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index 91c6f6b02e..4360b15486 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1723,6 +1723,7 @@ virXMLFormatMetadata(virBuffer *buf, xmlNodePtr metadata) { g_autoptr(xmlBuffer) xmlbuf =3D NULL; + const char *xmlbufContent =3D NULL; int oldIndentTreeOutput =3D xmlIndentTreeOutput; =20 if (!metadata) @@ -1745,7 +1746,12 @@ virXMLFormatMetadata(virBuffer *buf, return -1; } =20 - virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); + /* After libxml2-v2.9.12-2-g85b1792e even the first line is indented. + * But virBufferAsprintf() also adds indentation. Skip one of them. */ + xmlbufContent =3D (const char *) xmlBufferContent(xmlbuf); + virSkipSpaces(&xmlbufContent); + + virBufferAsprintf(buf, "%s\n", xmlbufContent); xmlIndentTreeOutput =3D oldIndentTreeOutput; =20 return 0; --=20 2.26.3