From nobody Sun Apr 28 13:14:24 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1551279693210262.5101720698783; Wed, 27 Feb 2019 07:01:33 -0800 (PST) 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 mx1.redhat.com (Postfix) with ESMTPS id 337A8309D074; Wed, 27 Feb 2019 15:01:31 +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 EEE1B619B5; Wed, 27 Feb 2019 15:01: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 347633FA45; Wed, 27 Feb 2019 15:01:30 +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 x1RF1T34019911 for ; Wed, 27 Feb 2019 10:01:29 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3C2B760464; Wed, 27 Feb 2019 15:01:29 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id A65ED63B8E for ; Wed, 27 Feb 2019 15:01:24 +0000 (UTC) From: Pavel Hrdina To: libvir-list@redhat.com Date: Wed, 27 Feb 2019 16:01:23 +0100 Message-Id: <792b73ced18326c4af96ae83510ddd2830d74f18.1551279469.git.phrdina@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] conf: fix title and description for virDomainSetMetadata API 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: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Wed, 27 Feb 2019 15:01:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" If we pass XML to virDomainDefineXML API with these two elements: ... ... libvirt correctly ignores these two elements and they will not appear in the parsed XML. However, if we use virDomainSetMetadata API and with "" as value for title or description we will end up with the parsed XML that contains these empty elements. Let's fix the behavior of this API to behave the same as virDomainDefineXML. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1518042 Signed-off-by: Pavel Hrdina Reviewed-by: John Ferlan --- src/conf/domain_conf.c | 6 +++--- tests/metadatatest.c | 23 ++++++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 477deb777e..2fba58e5c4 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -30363,7 +30363,7 @@ virDomainDefSetMetadata(virDomainDefPtr def, xmlDocPtr doc =3D NULL; xmlNodePtr old; xmlNodePtr new =3D NULL; - char *tmp; + char *tmp =3D NULL; int ret =3D -1; =20 if (type >=3D VIR_DOMAIN_METADATA_LAST) { @@ -30374,7 +30374,7 @@ virDomainDefSetMetadata(virDomainDefPtr def, =20 switch ((virDomainMetadataType) type) { case VIR_DOMAIN_METADATA_DESCRIPTION: - if (VIR_STRDUP(tmp, metadata) < 0) + if (STRNEQ_NULLABLE(metadata, "") && VIR_STRDUP(tmp, metadata) < 0) goto cleanup; =20 VIR_FREE(def->description); @@ -30382,7 +30382,7 @@ virDomainDefSetMetadata(virDomainDefPtr def, break; =20 case VIR_DOMAIN_METADATA_TITLE: - if (VIR_STRDUP(tmp, metadata) < 0) + if (STRNEQ_NULLABLE(metadata, "") && VIR_STRDUP(tmp, metadata) < 0) goto cleanup; =20 VIR_FREE(def->title); diff --git a/tests/metadatatest.c b/tests/metadatatest.c index 786c62e623..a9080b32d7 100644 --- a/tests/metadatatest.c +++ b/tests/metadatatest.c @@ -167,6 +167,7 @@ struct metadataTest { virDomainPtr dom; =20 const char *data; + const char *expect; int type; bool fail; }; @@ -232,7 +233,7 @@ testTextMetadata(const void *data) =20 actual =3D virDomainGetMetadata(test->dom, test->type, NULL, 0); =20 - if (STRNEQ_NULLABLE(test->data, actual)) { + if (STRNEQ_NULLABLE(test->expect, actual)) { virReportError(VIR_ERR_INTERNAL_ERROR, "expected metadata doesn't match actual: " "expected:'%s'\ngot: '%s'", @@ -248,10 +249,11 @@ testTextMetadata(const void *data) return ret; } =20 -#define TEST_TEXT_METADATA(INDEX, TYPE, DATA, FAIL) \ +#define TEST_TEXT_METADATA(INDEX, TYPE, DATA, EXPECT, FAIL) \ do { \ test.type =3D VIR_DOMAIN_METADATA_ ## TYPE; \ test.data =3D DATA; \ + test.expect =3D EXPECT; \ test.fail =3D FAIL; \ \ if (virTestRun("text metadata: " #TYPE " " INDEX " ", \ @@ -259,9 +261,16 @@ testTextMetadata(const void *data) ret =3D EXIT_FAILURE; \ } while (0) =20 -#define TEST_TITLE(INDEX, DATA) TEST_TEXT_METADATA(INDEX, TITLE, DATA, fal= se) -#define TEST_TITLE_FAIL(INDEX, DATA) TEST_TEXT_METADATA(INDEX, TITLE, DATA= , true) -#define TEST_DESCR(INDEX, DATA) TEST_TEXT_METADATA(INDEX, DESCRIPTION, DAT= A, false) +#define TEST_TITLE(INDEX, DATA) \ + TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, false) +#define TEST_TITLE_EXPECT(INDEX, DATA, EXPECT) \ + TEST_TEXT_METADATA(INDEX, TITLE, DATA, EXPECT, false) +#define TEST_TITLE_FAIL(INDEX, DATA) \ + TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, true) +#define TEST_DESCR(INDEX, DATA) \ + TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, DATA, false) +#define TEST_DESCR_EXPECT(INDEX, DATA, EXPECT) \ + TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, EXPECT, false) =20 static int mymain(void) @@ -290,7 +299,7 @@ mymain(void) TEST_TITLE("2", NULL); TEST_TITLE("3", "blah"); TEST_TITLE_FAIL("4", "qwe\nrt"); - TEST_TITLE("5", ""); + TEST_TITLE_EXPECT("5", "", NULL); TEST_TITLE_FAIL("6", "qwert\n"); TEST_TITLE_FAIL("7", "\n"); =20 @@ -298,7 +307,7 @@ mymain(void) TEST_DESCR("2", NULL); TEST_DESCR("3", "qwert"); TEST_DESCR("4", "\n"); - TEST_DESCR("5", ""); + TEST_DESCR_EXPECT("5", "", NULL); =20 virDomainFree(test.dom); virConnectClose(test.conn); --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list