From nobody Sun Feb 8 21:48:15 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=1641284087; cv=none; d=zohomail.com; s=zohoarc; b=fgUu//O0DVT3+WKoBU3b2TN2USGQ934BtvvpEE4LuQcJc/lsyCotgZXyhvs9GUGV8sfqPAVCPUQJO91JtRtYn7gegUe9VVr9r0jxEn+Ohi+YbXgt/yRUaO1FN8+AxOVlRoaA7KZGFupkebezyNXiRAHn365HLhD/mlkCQHFB0YU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1641284087; 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=w9OndzZHA5ajoeB6TBDYGBTekjJmn439OE6halbuxFc=; b=BLrkiBbG7tfgx5xYQYTGmzIxM+ww/H2F0lYwbNFUhp38R9KOQ870g13ntJAmWi5/8UUoQgD/oS9owK2uXDhvdqToRimj3ils8xzQPiSrw2Qhwky/EgYfAbbpXhLepyHCAKxal7bux1rOd1Id6Nf7euZiY9c8CHt1fuNzMVmyEjk= 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 1641284087401573.7664882251752; Tue, 4 Jan 2022 00:14:47 -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-484-WHSrMvgfNsCtShClDSxVnA-1; Tue, 04 Jan 2022 03:14:44 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 270F381CCD4; Tue, 4 Jan 2022 08:14:40 +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 031BB6F12A; Tue, 4 Jan 2022 08:14:40 +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 C59294A707; Tue, 4 Jan 2022 08:14:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 2048EYl9027105 for ; Tue, 4 Jan 2022 03:14:34 -0500 Received: by smtp.corp.redhat.com (Postfix) id 6C85A5F936; Tue, 4 Jan 2022 08:14:34 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB9365ED4D for ; Tue, 4 Jan 2022 08:14:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641284086; 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=w9OndzZHA5ajoeB6TBDYGBTekjJmn439OE6halbuxFc=; b=V3/OtMKrC2lzViyKMAi9pINFAiwi5ch9Fg72Zp27mU7OujNhu+Q1WgsB+fC7aTlOuag6Ac 363hSUFHNJ4eVpIvRLBEl9d5Ok2RdqjFWKr02ExIvZWa2mvb9xAeEw38qt8PKhoI8L+Y9E /wov0izlGMP0z2qcPJnUNzpHOScHZJg= X-MC-Unique: WHSrMvgfNsCtShClDSxVnA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/4] conf: Rework formatting Date: Tue, 4 Jan 2022 09:14:29 +0100 Message-Id: <34acdf27b39ce3a71dce259d65b330e4ce97a8c3.1641283978.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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.15 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: 1641284087884100003 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. 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 ++++++++------------ tests/qemuxml2argvdata/tpm-emulator-tpm2.xml | 1 - 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 716c6d2240..b8fef8586c 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -25481,63 +25481,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) backendBuf =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(&backendBuf, "\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(&backendBuf, "\n", + virUUIDFormat(def->data.emulator.secretuuid,= uuidstr)); } if (def->data.emulator.activePcrBanks) { + g_auto(virBuffer) activePcrBanksBuf =3D VIR_BUFFER_INIT_CHILD(= &backendBuf); 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(&backendBuf, "active_pcr_banks", NULL, &ac= tivePcrBanksBuf); } - 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, &backendBuf= ); + virDomainDeviceInfoFormat(&childBuf, &def->info, flags); =20 - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); + virXMLFormatElement(buf, "tpm", &attrBuf, &childBuf); =20 return 0; } diff --git a/tests/qemuxml2argvdata/tpm-emulator-tpm2.xml b/tests/qemuxml2a= rgvdata/tpm-emulator-tpm2.xml index 59dd68311f..79acde218b 100644 --- a/tests/qemuxml2argvdata/tpm-emulator-tpm2.xml +++ b/tests/qemuxml2argvdata/tpm-emulator-tpm2.xml @@ -30,7 +30,6 @@ - > --=20 2.34.1