From 4da0da572e8776c111b91cd47ff52e746721e8c1 Mon Sep 17 00:00:00 2001
From: Kevin Wolf <kwolf@redhat.com>
Date: Thu, 29 Apr 2010 13:27:50 -0300
Subject: [PATCH 1/2] qemu-img: use the heap instead of the huge stack array for win32

RH-Author: Kevin Wolf <kwolf@redhat.com>
Message-id: <1272547671-14461-2-git-send-email-kwolf@redhat.com>
Patchwork-id: 8895
O-Subject: [RHEL-6 qemu-kvm PATCH 1/2] qemu-img: use the heap instead of the
	huge stack array for win32
Bugzilla: 585837
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
RH-Acked-by: Christoph Hellwig <chellwig@redhat.com>
RH-Acked-by: Juan Quintela <quintela@redhat.com>

From: TeLeMan <geleman@gmail.com>

Bugzilla: 585837

The default stack size of PE is 1MB on win32 and IO_BUF_SIZE in
img_convert() & img_rebase() is 2MB, so qemu-img will crash when doing
"convert" & "rebase" on win32.
Although we can improve the stack size of PE to resolve it, I think we
should avoid using the huge stack variables.

Signed-off-by: TeLeMan <geleman@gmail.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
(cherry picked from commit d6771bfa52744eb4f959198b4b0e59451463eebf)

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qemu-img.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 qemu-img.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 3744c24..f109509 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -576,7 +576,7 @@ static int img_convert(int argc, char **argv)
     BlockDriverState **bs, *out_bs;
     int64_t total_sectors, nb_sectors, sector_num, bs_offset;
     uint64_t bs_sectors;
-    uint8_t buf[IO_BUF_SIZE];
+    uint8_t * buf;
     const uint8_t *buf1;
     BlockDriverInfo bdi;
     QEMUOptionParameter *param = NULL;
@@ -695,6 +695,7 @@ static int img_convert(int argc, char **argv)
     bs_i = 0;
     bs_offset = 0;
     bdrv_get_geometry(bs[0], &bs_sectors);
+    buf = qemu_malloc(IO_BUF_SIZE);
 
     if (flags & BLOCK_FLAG_COMPRESS) {
         if (bdrv_get_info(out_bs, &bdi) < 0)
@@ -827,6 +828,7 @@ static int img_convert(int argc, char **argv)
             }
         }
     }
+    qemu_free(buf);
     bdrv_delete(out_bs);
     for (bs_i = 0; bs_i < bs_n; bs_i++)
         bdrv_delete(bs[bs_i]);
@@ -1195,8 +1197,11 @@ static int img_rebase(int argc, char **argv)
         uint64_t num_sectors;
         uint64_t sector;
         int n, n1;
-        uint8_t buf_old[IO_BUF_SIZE];
-        uint8_t buf_new[IO_BUF_SIZE];
+        uint8_t * buf_old;
+        uint8_t * buf_new;
+
+        buf_old = qemu_malloc(IO_BUF_SIZE);
+        buf_new = qemu_malloc(IO_BUF_SIZE);
 
         bdrv_get_geometry(bs, &num_sectors);
 
@@ -1243,6 +1248,9 @@ static int img_rebase(int argc, char **argv)
                 written += pnum;
             }
         }
+
+        qemu_free(buf_old);
+        qemu_free(buf_new);
     }
 
     /*
-- 
1.7.0.3

