]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: storage: Copy hosts of a storage file only if they exist
authorPeter Krempa <pkrempa@redhat.com>
Thu, 30 Oct 2014 10:52:17 +0000 (11:52 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 21 Nov 2014 13:37:02 +0000 (14:37 +0100)
If there are no hosts for a storage source virStorageSourceCopy and
virStorageSourceNewFromBackingRelative would try to copy them anyways.
As the success of virStorageNetHostDefCopy is determined by returning
a pointer and malloc of 0 elements might return NULL according to the
implementation, the result of the copy function may vary.

Fix this by copying the hosts array only if there are hosts defined.

src/util/virstoragefile.c

index 1c437a9b0a380fa7461ad1a06907f2262dd9eb32..8ca2d8d9f95db37ce08df92c5018100a29ce98aa 100644 (file)
@@ -1851,9 +1851,12 @@ virStorageSourceCopy(const virStorageSource *src,
         VIR_STRDUP(ret->compat, src->compat) < 0)
         goto error;
 
-    if (!(ret->hosts = virStorageNetHostDefCopy(src->nhosts, src->hosts)))
-        goto error;
-    ret->nhosts = src->nhosts;
+    if (src->nhosts) {
+        if (!(ret->hosts = virStorageNetHostDefCopy(src->nhosts, src->hosts)))
+            goto error;
+
+        ret->nhosts = src->nhosts;
+    }
 
     if (src->srcpool &&
         !(ret->srcpool = virStorageSourcePoolDefCopy(src->srcpool)))
@@ -2092,9 +2095,13 @@ virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent,
 
         /* copy the host network part */
         ret->protocol = parent->protocol;
-        if (!(ret->hosts = virStorageNetHostDefCopy(parent->nhosts, parent->hosts)))
-            goto error;
-        ret->nhosts = parent->nhosts;
+        if (parent->nhosts) {
+            if (!(ret->hosts = virStorageNetHostDefCopy(parent->nhosts,
+                                                        parent->hosts)))
+                goto error;
+
+            ret->nhosts = parent->nhosts;
+        }
 
         if (VIR_STRDUP(ret->volume, parent->volume) < 0)
             goto error;