From nobody Sun Feb 8 19:21:08 2026 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=1641287139; cv=none; d=zohomail.com; s=zohoarc; b=EbEIQdnGStXZdaHamz39fuqrgeRuJJtLkNsBP1fShzDMsi8hQP8XhU8Bs9LbrCo0zD2XmISOrRYfZpE4ZoSjMyIrIF92MiMfJsnIDg8kRuNKJv3baQUfRKnzXefF5LzfsC6Dz6iHka5EKrYMU863ntx5A9UolQst84UQv2i6NI8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1641287139; 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=YlKbErQxns1JHI7M1vN+TwmWtRb9FayyOMY+mROEGEM=; b=kbH3apjZzNOu7m6vCcj4cKOpcx7R1iTtMD2KLIa1oC7Je6gKv7l3iDHcs9VmmzyReiZ5S2os+DPBOAoWZHuJrSx8a2rkeCrcUzABowQP/IuAVRB7uXtaNzrQnl0EMnvYU8+0ZhynVynvJArns/ev81Tc9HLTGVmb1f6efQ8mDFI= 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) 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 1641287139047577.9135987343969; Tue, 4 Jan 2022 01:05:39 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-140-Qt_UbYjWNx2ZgN4CXfDyXg-1; Tue, 04 Jan 2022 04:05:34 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 68528185302C; Tue, 4 Jan 2022 09:05:30 +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 48A4A5F91F; Tue, 4 Jan 2022 09:05:30 +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 8F79D4CA93; Tue, 4 Jan 2022 09:05:29 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 20495RNw031363 for ; Tue, 4 Jan 2022 04:05:27 -0500 Received: by smtp.corp.redhat.com (Postfix) id DF5B37CAF6; Tue, 4 Jan 2022 09:05:27 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6A7687CD64 for ; Tue, 4 Jan 2022 09:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641287138; 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=YlKbErQxns1JHI7M1vN+TwmWtRb9FayyOMY+mROEGEM=; b=ZLq+Pf+6aJwiu63u9YbKpY7dt5Hqwmh4F3KqiYNnTs0AGtHA3QRl0kb1Av/iQNKsY2bU2H djae/nnDGJt+Gqy1hXrNNGhBw+s87NCO1G+kvGv6e1Y6j+czVvW457x6sJtxo5N3JBXZfB BESVFjhqT+kTsSfzQFFJjzqQFnRZCxI= X-MC-Unique: Qt_UbYjWNx2ZgN4CXfDyXg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 1/3] conf: Rework formatting Date: Tue, 4 Jan 2022 10:04:58 +0100 Message-Id: <5e2a368c618d645a0bc08f172052a410ff2d56ca.1641287042.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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.14 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) X-ZM-MESSAGEID: 1641287140755100001 Content-Type: text/plain; charset="utf-8" The element formatting is handled in virDomainTPMDefFormat() which uses the "old style" - appending strings directly into the output buffer. With this, it's easy to get conditions that tell when an element has ended wrong. In this particular case, if both and are to be formatted the current code puts a stray '>' into the output buffer, resulting in invalid XML. Rewrite the function to use virXMLFormatElement() which is more clever. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D2016599#c15 Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/conf/domain_conf.c | 53 ++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index bba662bf4c..9e854d031e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -25495,63 +25495,54 @@ virDomainTPMDefFormat(virBuffer *buf, virDomainTPMDef *def, unsigned int flags) { - virBufferAsprintf(buf, "\n", + g_auto(virBuffer) attrBuf =3D VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) childBuf =3D VIR_BUFFER_INIT_CHILD(buf); + g_auto(virBuffer) backendAttrBuf =3D VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) backendChildBuf =3D VIR_BUFFER_INIT_CHILD(&childBuf); + + virBufferAsprintf(&attrBuf, " model=3D'%s'", virDomainTPMModelTypeToString(def->model)); - virBufferAdjustIndent(buf, 2); - virBufferAsprintf(buf, "type)); =20 switch (def->type) { case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); - virBufferEscapeString(buf, "\n", + virBufferEscapeString(&backendChildBuf, "\n", def->data.passthrough.source->data.file.path= ); - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); break; case VIR_DOMAIN_TPM_TYPE_EMULATOR: - virBufferAsprintf(buf, " version=3D'%s'", + virBufferAsprintf(&backendAttrBuf, " version=3D'%s'", virDomainTPMVersionTypeToString(def->version)); if (def->data.emulator.persistent_state) - virBufferAddLit(buf, " persistent_state=3D'yes'"); + virBufferAddLit(&backendAttrBuf, " persistent_state=3D'yes'"); if (def->data.emulator.hassecretuuid) { char uuidstr[VIR_UUID_STRING_BUFLEN]; - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); - virBufferAsprintf(buf, "\n", - virUUIDFormat(def->data.emulator.secretuuid, uuidstr)); - virBufferAdjustIndent(buf, -2); + + virBufferAsprintf(&backendChildBuf, "\n", + virUUIDFormat(def->data.emulator.secretuuid,= uuidstr)); } if (def->data.emulator.activePcrBanks) { + g_auto(virBuffer) activePcrBanksBuf =3D VIR_BUFFER_INIT_CHILD(= &backendChildBuf); size_t i; - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); - virBufferAddLit(buf, "\n"); - virBufferAdjustIndent(buf, 2); + for (i =3D VIR_DOMAIN_TPM_PCR_BANK_SHA1; i < VIR_DOMAIN_TPM_PC= R_BANK_LAST; i++) { if ((def->data.emulator.activePcrBanks & (1 << i))) - virBufferAsprintf(buf, "<%s/>\n", + virBufferAsprintf(&activePcrBanksBuf, "<%s/>\n", virDomainTPMPcrBankTypeToString(i)); } - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - virBufferAdjustIndent(buf, -2); + + virXMLFormatElement(&backendChildBuf, "active_pcr_banks", NULL= , &activePcrBanksBuf); } - if (def->data.emulator.hassecretuuid || - def->data.emulator.activePcrBanks) - virBufferAddLit(buf, "\n"); - else - virBufferAddLit(buf, "/>\n"); break; case VIR_DOMAIN_TPM_TYPE_LAST: break; } =20 - virDomainDeviceInfoFormat(buf, &def->info, flags); + virXMLFormatElement(&childBuf, "backend", &backendAttrBuf, &backendChi= ldBuf); + virDomainDeviceInfoFormat(&childBuf, &def->info, flags); =20 - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); + virXMLFormatElement(buf, "tpm", &attrBuf, &childBuf); =20 return 0; } --=20 2.34.1