diff mbox

[v2,3/6] block: qapi - move string allocation from stack to the heap

Message ID a4f5aa8f94a835092d1a95547e5b9a9be10aa70d.1421768887.git.jcody@redhat.com
State New
Headers show

Commit Message

Jeff Cody Jan. 20, 2015, 5:31 p.m. UTC
Rather than declaring 'backing_filename2' on the stack in
bdrv_quiery_image_info(), dynamically allocate it on the heap.

Signed-off-by: Jeff Cody <jcody@redhat.com>
---
 block/qapi.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

John Snow Jan. 20, 2015, 6:37 p.m. UTC | #1
On 01/20/2015 12:31 PM, Jeff Cody wrote:
> Rather than declaring 'backing_filename2' on the stack in
> bdrv_quiery_image_info(), dynamically allocate it on the heap.
>
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>   block/qapi.c | 8 +++++---
>   1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/block/qapi.c b/block/qapi.c
> index a6fd6f7..e51bade 100644
> --- a/block/qapi.c
> +++ b/block/qapi.c
> @@ -175,7 +175,7 @@ void bdrv_query_image_info(BlockDriverState *bs,
>   {
>       int64_t size;
>       const char *backing_filename;
> -    char backing_filename2[1024];
> +    char *backing_filename2 = NULL;
>       BlockDriverInfo bdi;
>       int ret;
>       Error *err = NULL;
> @@ -211,13 +211,14 @@ void bdrv_query_image_info(BlockDriverState *bs,
>
>       backing_filename = bs->backing_file;
>       if (backing_filename[0] != '\0') {
> +        backing_filename2 = g_malloc0(1024);
>           info->backing_filename = g_strdup(backing_filename);
>           info->has_backing_filename = true;
> -        bdrv_get_full_backing_filename(bs, backing_filename2,
> -                                       sizeof(backing_filename2), &err);
> +        bdrv_get_full_backing_filename(bs, backing_filename2, 1024, &err);
>           if (err) {
>               error_propagate(errp, err);
>               qapi_free_ImageInfo(info);
> +            g_free(backing_filename2);
>               return;
>           }
>
> @@ -231,6 +232,7 @@ void bdrv_query_image_info(BlockDriverState *bs,
>               info->backing_filename_format = g_strdup(bs->backing_format);
>               info->has_backing_filename_format = true;
>           }
> +        g_free(backing_filename2);
>       }
>
>       ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
>

Reviewed-by: John Snow <jsnow@redhat.com>
Stefan Hajnoczi Jan. 22, 2015, 11:24 a.m. UTC | #2
On Tue, Jan 20, 2015 at 12:31:30PM -0500, Jeff Cody wrote:
> Rather than declaring 'backing_filename2' on the stack in
> bdrv_quiery_image_info(), dynamically allocate it on the heap.

s/quiery/query/

> 
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>  block/qapi.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/block/qapi.c b/block/qapi.c
> index a6fd6f7..e51bade 100644
> --- a/block/qapi.c
> +++ b/block/qapi.c
> @@ -175,7 +175,7 @@ void bdrv_query_image_info(BlockDriverState *bs,
>  {
>      int64_t size;
>      const char *backing_filename;
> -    char backing_filename2[1024];
> +    char *backing_filename2 = NULL;
>      BlockDriverInfo bdi;
>      int ret;
>      Error *err = NULL;
> @@ -211,13 +211,14 @@ void bdrv_query_image_info(BlockDriverState *bs,
>  
>      backing_filename = bs->backing_file;
>      if (backing_filename[0] != '\0') {
> +        backing_filename2 = g_malloc0(1024);

backing_filename2 is only used inside the body of this if statement.
Please move the declaration in here to avoid initializing with NULL
(that value is never used but I had to check the surrounding code to
figure that out).
diff mbox

Patch

diff --git a/block/qapi.c b/block/qapi.c
index a6fd6f7..e51bade 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -175,7 +175,7 @@  void bdrv_query_image_info(BlockDriverState *bs,
 {
     int64_t size;
     const char *backing_filename;
-    char backing_filename2[1024];
+    char *backing_filename2 = NULL;
     BlockDriverInfo bdi;
     int ret;
     Error *err = NULL;
@@ -211,13 +211,14 @@  void bdrv_query_image_info(BlockDriverState *bs,
 
     backing_filename = bs->backing_file;
     if (backing_filename[0] != '\0') {
+        backing_filename2 = g_malloc0(1024);
         info->backing_filename = g_strdup(backing_filename);
         info->has_backing_filename = true;
-        bdrv_get_full_backing_filename(bs, backing_filename2,
-                                       sizeof(backing_filename2), &err);
+        bdrv_get_full_backing_filename(bs, backing_filename2, 1024, &err);
         if (err) {
             error_propagate(errp, err);
             qapi_free_ImageInfo(info);
+            g_free(backing_filename2);
             return;
         }
 
@@ -231,6 +232,7 @@  void bdrv_query_image_info(BlockDriverState *bs,
             info->backing_filename_format = g_strdup(bs->backing_format);
             info->has_backing_filename_format = true;
         }
+        g_free(backing_filename2);
     }
 
     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);