Blame SOURCES/kvm-block-vmdk-move-string-allocations-from-stack-to-the.patch

34b321
From a767838caf6c761d714a9466d008f8dddaf1a162 Mon Sep 17 00:00:00 2001
34b321
From: Fam Zheng <famz@redhat.com>
34b321
Date: Mon, 15 Feb 2016 09:28:22 +0100
34b321
Subject: [PATCH 09/18] block: vmdk - move string allocations from stack to the
34b321
 heap
34b321
34b321
RH-Author: Fam Zheng <famz@redhat.com>
34b321
Message-id: <1455528511-9357-10-git-send-email-famz@redhat.com>
34b321
Patchwork-id: 69175
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH 09/18] block: vmdk - move string allocations from stack to the heap
34b321
Bugzilla: 1299250
34b321
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
34b321
RH-Acked-by: Max Reitz <mreitz@redhat.com>
34b321
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
34b321
34b321
From: Jeff Cody <jcody@redhat.com>
34b321
34b321
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1299250
34b321
34b321
Functions 'vmdk_parse_extents' and 'vmdk_create' allocate several
34b321
PATH_MAX sized arrays on the stack.  Make these dynamically allocated.
34b321
34b321
Signed-off-by: Jeff Cody <jcody@redhat.com>
34b321
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
34b321
(cherry picked from commit fe2065629a9c256f836770ca54449ae77b22d188)
34b321
Signed-off-by: Fam Zheng <famz@redhat.com>
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
---
34b321
 block/vmdk.c | 40 ++++++++++++++++++++++++----------------
34b321
 1 file changed, 24 insertions(+), 16 deletions(-)
34b321
34b321
diff --git a/block/vmdk.c b/block/vmdk.c
34b321
index 3351782..45ecf02 100644
34b321
--- a/block/vmdk.c
34b321
+++ b/block/vmdk.c
34b321
@@ -795,12 +795,11 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
34b321
     const char *p = desc;
34b321
     int64_t sectors = 0;
34b321
     int64_t flat_offset;
34b321
-    char extent_path[PATH_MAX];
34b321
+    char *extent_path;
34b321
     BlockDriverState *extent_file;
34b321
     BDRVVmdkState *s = bs->opaque;
34b321
     VmdkExtent *extent;
34b321
 
34b321
-
34b321
     while (*p) {
34b321
         /* parse extent line in one of below formats:
34b321
          *
34b321
@@ -838,10 +837,13 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
34b321
             goto next_line;
34b321
         }
34b321
 
34b321
+        extent_path = g_malloc0(PATH_MAX);
34b321
         path_combine(extent_path, sizeof(extent_path),
34b321
                 desc_file_path, fname);
34b321
+        extent_file = NULL;
34b321
         ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
34b321
                              errp);
34b321
+        g_free(extent_path);
34b321
         if (ret) {
34b321
             return ret;
34b321
         }
34b321
@@ -1790,10 +1792,15 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
34b321
     int ret = 0;
34b321
     bool flat, split, compress;
34b321
     GString *ext_desc_lines;
34b321
-    char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
34b321
+    char *path = g_malloc0(PATH_MAX);
34b321
+    char *prefix = g_malloc0(PATH_MAX);
34b321
+    char *postfix = g_malloc0(PATH_MAX);
34b321
+    char *desc_line = g_malloc0(BUF_SIZE);
34b321
+    char *ext_filename = g_malloc0(PATH_MAX);
34b321
+    char *desc_filename = g_malloc0(PATH_MAX);
34b321
     const int64_t split_size = 0x80000000;  /* VMDK has constant split size */
34b321
     const char *desc_extent_line;
34b321
-    char parent_desc_line[BUF_SIZE] = "";
34b321
+    char *parent_desc_line = g_malloc0(BUF_SIZE);
34b321
     uint32_t parent_cid = 0xffffffff;
34b321
     uint32_t number_heads = 16;
34b321
     bool zeroed_grain = false;
34b321
@@ -1902,33 +1909,27 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
34b321
         }
34b321
         parent_cid = vmdk_read_cid(bs, 0);
34b321
         bdrv_unref(bs);
34b321
-        snprintf(parent_desc_line, sizeof(parent_desc_line),
34b321
+        snprintf(parent_desc_line, BUF_SIZE,
34b321
                 "parentFileNameHint=\"%s\"", backing_file);
34b321
     }
34b321
 
34b321
     /* Create extents */
34b321
     filesize = total_size;
34b321
     while (filesize > 0) {
34b321
-        char desc_line[BUF_SIZE];
34b321
-        char ext_filename[PATH_MAX];
34b321
-        char desc_filename[PATH_MAX];
34b321
         int64_t size = filesize;
34b321
 
34b321
         if (split && size > split_size) {
34b321
             size = split_size;
34b321
         }
34b321
         if (split) {
34b321
-            snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
34b321
+            snprintf(desc_filename, PATH_MAX, "%s-%c%03d%s",
34b321
                     prefix, flat ? 'f' : 's', ++idx, postfix);
34b321
         } else if (flat) {
34b321
-            snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
34b321
-                    prefix, postfix);
34b321
+            snprintf(desc_filename, PATH_MAX, "%s-flat%s", prefix, postfix);
34b321
         } else {
34b321
-            snprintf(desc_filename, sizeof(desc_filename), "%s%s",
34b321
-                    prefix, postfix);
34b321
+            snprintf(desc_filename, PATH_MAX, "%s%s", prefix, postfix);
34b321
         }
34b321
-        snprintf(ext_filename, sizeof(ext_filename), "%s%s",
34b321
-                path, desc_filename);
34b321
+        snprintf(ext_filename, PATH_MAX, "%s%s", path, desc_filename);
34b321
 
34b321
         if (vmdk_create_extent(ext_filename, size,
34b321
                                flat, compress, zeroed_grain, errp)) {
34b321
@@ -1938,7 +1939,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
34b321
         filesize -= size;
34b321
 
34b321
         /* Format description line */
34b321
-        snprintf(desc_line, sizeof(desc_line),
34b321
+        snprintf(desc_line, BUF_SIZE,
34b321
                     desc_extent_line, size / BDRV_SECTOR_SIZE, desc_filename);
34b321
         g_string_append(ext_desc_lines, desc_line);
34b321
     }
34b321
@@ -1988,6 +1989,13 @@ exit:
34b321
         bdrv_unref(new_bs);
34b321
     }
34b321
     g_free(desc);
34b321
+    g_free(path);
34b321
+    g_free(prefix);
34b321
+    g_free(postfix);
34b321
+    g_free(desc_line);
34b321
+    g_free(ext_filename);
34b321
+    g_free(desc_filename);
34b321
+    g_free(parent_desc_line);
34b321
     g_string_free(ext_desc_lines, true);
34b321
     return ret;
34b321
 }
34b321
-- 
34b321
1.8.3.1
34b321