9119d9
From c0e6c7fa7cd8fb4bb4ee26552c7ab35352c47ed0 Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <c0e6c7fa7cd8fb4bb4ee26552c7ab35352c47ed0@dist-git>
9119d9
From: Peter Krempa <pkrempa@redhat.com>
9119d9
Date: Tue, 24 Feb 2015 10:32:21 +0100
9119d9
Subject: [PATCH] util: storage: Fix parsing of nbd:// URI without path
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=1195156
9119d9
9119d9
If a storage file would be backed with a NBD device without path
9119d9
(nbd://localhost) libvirt would crash when parsing the backing path for
9119d9
the disk as the URI structure's path element is NULL in such case but
9119d9
the NBD parser would access it shamelessly.
9119d9
9119d9
(cherry picked from commit fdb80ed4f6563928b9942a0d1450e0c725aa6c06)
9119d9
9119d9
    Conflicts: tests/virstoragetest.c - test case numbering refactor was
9119d9
                                        not backported and thus I
9119d9
                                        manually numbered this test as
9119d9
                                        12.5
9119d9
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 src/util/virstoragefile.c |  3 ++-
9119d9
 tests/virstoragetest.c    | 14 ++++++++++++++
9119d9
 2 files changed, 16 insertions(+), 1 deletion(-)
9119d9
9119d9
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
9119d9
index 580ad20..3ab20a4 100644
9119d9
--- a/src/util/virstoragefile.c
9119d9
+++ b/src/util/virstoragefile.c
9119d9
@@ -2150,7 +2150,8 @@ virStorageSourceParseBackingURI(virStorageSourcePtr src,
9119d9
     /* XXX We currently don't support auth, so don't bother parsing it */
9119d9
 
9119d9
     /* possibly skip the leading slash */
9119d9
-    if (VIR_STRDUP(src->path,
9119d9
+    if (uri->path &&
9119d9
+        VIR_STRDUP(src->path,
9119d9
                    *uri->path == '/' ? uri->path + 1 : uri->path) < 0)
9119d9
         goto cleanup;
9119d9
 
9119d9
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
9119d9
index 2601edc..fc37489 100644
9119d9
--- a/tests/virstoragetest.c
9119d9
+++ b/tests/virstoragetest.c
9119d9
@@ -870,6 +870,20 @@ mymain(void)
9119d9
                (&qcow2, &nbd2), EXP_PASS,
9119d9
                (&qcow2, &nbd2), ALLOW_PROBE | EXP_PASS);
9119d9
 
9119d9
+    /* Rewrite qcow2 to use an nbd: protocol without path as backend */
9119d9
+    virCommandFree(cmd);
9119d9
+    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
9119d9
+                               "-F", "raw", "-b", "nbd://example.org",
9119d9
+                               "qcow2", NULL);
9119d9
+    if (virCommandRun(cmd, NULL) < 0)
9119d9
+        ret = -1;
9119d9
+    qcow2.expBackingStoreRaw = "nbd://example.org";
9119d9
+
9119d9
+    nbd2.path = NULL;
9119d9
+    TEST_CHAIN(12.5, absqcow2, VIR_STORAGE_FILE_QCOW2,
9119d9
+               (&qcow2, &nbd2), EXP_PASS,
9119d9
+               (&qcow2, &nbd2), ALLOW_PROBE | EXP_PASS);
9119d9
+
9119d9
     /* qed file */
9119d9
     testFileData qed = {
9119d9
         .expBackingStoreRaw = absraw,
9119d9
-- 
9119d9
2.3.0
9119d9