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