]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
storagefile: Push extension_end calc to qcow2GetBackingStoreFormat
authorCole Robinson <crobinso@redhat.com>
Fri, 4 Oct 2019 23:57:55 +0000 (19:57 -0400)
committerCole Robinson <crobinso@redhat.com>
Fri, 11 Oct 2019 17:41:22 +0000 (13:41 -0400)
This is a step towards making this qcow2GetBackingStoreFormat into
a generic qcow2 extensions parser

Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Cole Robinson <crobinso@redhat.com>
src/util/virstoragefile.c

index 4a3c9df7a28d46ef5f551c5e8240f06b87b1c9dd..b8f7faf5805ab05898b3ab77bf4688982e35fb02 100644 (file)
@@ -429,11 +429,11 @@ cowGetBackingStore(char **res,
 static int
 qcow2GetBackingStoreFormat(int *format,
                            const char *buf,
-                           size_t buf_size,
-                           size_t extension_end)
+                           size_t buf_size)
 {
     size_t offset;
     size_t extension_start;
+    size_t extension_end;
     int version = virReadBufInt32BE(buf + QCOWX_HDR_VERSION);
 
     if (version < 2) {
@@ -447,6 +447,37 @@ qcow2GetBackingStoreFormat(int *format,
     else
         extension_start = virReadBufInt32BE(buf + QCOW2v3_HDR_SIZE);
 
+    /*
+     * Traditionally QCow2 files had a layout of
+     *
+     * [header]
+     * [backingStoreName]
+     *
+     * Although the backingStoreName typically followed
+     * the header immediately, this was not required by
+     * the format. By specifying a higher byte offset for
+     * the backing file offset in the header, it was
+     * possible to leave space between the header and
+     * start of backingStore.
+     *
+     * This hack is now used to store extensions to the
+     * qcow2 format:
+     *
+     * [header]
+     * [extensions]
+     * [backingStoreName]
+     *
+     * Thus the file region to search for extensions is
+     * between the end of the header (QCOW2_HDR_TOTAL_SIZE)
+     * and the start of the backingStoreName (offset)
+     *
+     * for qcow2 v3 images, the length of the header
+     * is stored at QCOW2v3_HDR_SIZE
+     */
+    extension_end = virReadBufInt64BE(buf + QCOWX_HDR_BACKING_FILE_OFFSET);
+    if (extension_end > buf_size)
+        return -1;
+
     /*
      * The extensions take format of
      *
@@ -506,6 +537,7 @@ qcowXGetBackingStore(char **res,
 
     if (buf_size < QCOWX_HDR_BACKING_FILE_OFFSET+8+4)
         return BACKING_STORE_INVALID;
+
     offset = virReadBufInt64BE(buf + QCOWX_HDR_BACKING_FILE_OFFSET);
     if (offset > buf_size)
         return BACKING_STORE_INVALID;
@@ -529,35 +561,7 @@ qcowXGetBackingStore(char **res,
     memcpy(*res, buf + offset, size);
     (*res)[size] = '\0';
 
-    /*
-     * Traditionally QCow2 files had a layout of
-     *
-     * [header]
-     * [backingStoreName]
-     *
-     * Although the backingStoreName typically followed
-     * the header immediately, this was not required by
-     * the format. By specifying a higher byte offset for
-     * the backing file offset in the header, it was
-     * possible to leave space between the header and
-     * start of backingStore.
-     *
-     * This hack is now used to store extensions to the
-     * qcow2 format:
-     *
-     * [header]
-     * [extensions]
-     * [backingStoreName]
-     *
-     * Thus the file region to search for extensions is
-     * between the end of the header (QCOW2_HDR_TOTAL_SIZE)
-     * and the start of the backingStoreName (offset)
-     *
-     * for qcow2 v3 images, the length of the header
-     * is stored at QCOW2v3_HDR_SIZE
-     */
-
-    if (qcow2GetBackingStoreFormat(format, buf, buf_size, offset) < 0)
+    if (qcow2GetBackingStoreFormat(format, buf, buf_size) < 0)
         return BACKING_STORE_INVALID;
 
     return BACKING_STORE_OK;