From nobody Wed May 14 20:48:15 2025 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 1539255715527301.422614155142; Thu, 11 Oct 2018 04:01:55 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4602832B66D; Thu, 11 Oct 2018 11:01:53 +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 9BE9B1057074; Thu, 11 Oct 2018 11:01:52 +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 15CC73D380; Thu, 11 Oct 2018 11:01:52 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9BB1o9i013284 for ; Thu, 11 Oct 2018 07:01:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id 034671057074; Thu, 11 Oct 2018 11:01:50 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7CCEA1062242 for ; Thu, 11 Oct 2018 11:01:46 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 11 Oct 2018 13:01:45 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/2] util: storage: Properly parse URIs with missing trailing slash 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 11 Oct 2018 11:01:54 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The URI parser used by libvirt does not populate uri->path if the trailing slash is missing. The code virStorageSourceParseBackingURI would then not populate src->path. As only NBD network disks are allowed to have the 'name' field in the XML defining the disk source omitted we'd generate an invalid XML which we'd not parse again. Fix it by populating src->path with an empty string if the uri is lacking slash. As pointed out above NBD is special in this case since we actually allow it being NULL. The URI path is used as export name. Since an empty export does not make sense the new approach clears the src->path if the trailing slash is present but nothing else. Add test cases now to cover all the various cases for NBD and non-NBD uris as there was to time only 1 test abusing the quirk witout slash for NBD and all other URIs contained the slash or in case of NBD also the export name. Signed-off-by: Peter Krempa --- src/util/virstoragefile.c | 22 ++++++++++++++++++---- tests/qemublocktest.c | 3 +++ tests/virstoragetest.c | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index eba82918c1..94c32d818e 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -2579,6 +2579,7 @@ virStorageSourceParseBackingURI(virStorageSourcePtr s= rc, const char *uristr) { virURIPtr uri =3D NULL; + const char *path =3D NULL; char **scheme =3D NULL; int ret =3D -1; @@ -2621,10 +2622,23 @@ virStorageSourceParseBackingURI(virStorageSourcePtr= src, /* XXX We currently don't support auth, so don't bother parsing it */ - /* possibly skip the leading slash */ - if (uri->path && - VIR_STRDUP(src->path, - *uri->path =3D=3D '/' ? uri->path + 1 : uri->path) < 0) + /* uri->path is NULL if the URI does not contain slash after host: + * transport://host:port */ + if (uri->path) + path =3D uri->path; + else + path =3D ""; + + /* possibly skip the leading slash */ + if (path[0] =3D=3D '/') + path++; + + /* NBD allows empty export name (path) */ + if (src->protocol =3D=3D VIR_STORAGE_NET_PROTOCOL_NBD && + path[0] =3D=3D '\0') + path =3D NULL; + + if (VIR_STRDUP(src->path, path) < 0) goto cleanup; if (src->protocol =3D=3D VIR_STORAGE_NET_PROTOCOL_GLUSTER) { diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c index 6d85718d92..5848f6b5b5 100644 --- a/tests/qemublocktest.c +++ b/tests/qemublocktest.c @@ -373,6 +373,9 @@ mymain(void) /* type VIR_STORAGE_TYPE_BLOCK is not tested since it parses back to '= file' */ /* type VIR_STORAGE_TYPE_DIR it is a 'format' driver in qemu */ + TEST_JSON_FORMAT_NET("\n" + " \n" + "\n"); TEST_JSON_FORMAT_NET("\n" " \n" "\n"); diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index 83680a63a0..be4f558035 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -1303,6 +1303,14 @@ mymain(void) TEST_BACKING_PARSE("path", "\n"); TEST_BACKING_PARSE("://", NULL); + TEST_BACKING_PARSE("http://example.com", + "\n" + " \n" + "\n"); + TEST_BACKING_PARSE("http://example.com/", + "\n" + " \n" + "\n"); TEST_BACKING_PARSE("http://example.com/file", "\n" " \n" @@ -1315,6 +1323,18 @@ mymain(void) "\n" " \n" "\n"); + TEST_BACKING_PARSE("nbd://example.org:1234", + "\n" + " \n" + "\n"); + TEST_BACKING_PARSE("nbd://example.org:1234/", + "\n" + " \n" + "\n"); + TEST_BACKING_PARSE("nbd://example.org:1234/exportname", + "\n" + " \n" + "\n"); #ifdef WITH_YAJL TEST_BACKING_PARSE("json:", NULL); --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list