qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v2 4/6] block: move string allocation from stack


From: John Snow
Subject: Re: [Qemu-devel] [PATCH v2 4/6] block: move string allocation from stack to the heap
Date: Tue, 20 Jan 2015 13:37:17 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0



On 01/20/2015 12:31 PM, Jeff Cody wrote:
Rather than allocate PATH_MAX bytes on the stack, use g_strndup() to
dynamically allocate the string, and add an exit label for cleanup.

Signed-off-by: Jeff Cody <address@hidden>
---
  block.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/block.c b/block.c
index cbe4a32..39cd7a6 100644
--- a/block.c
+++ b/block.c
@@ -2207,7 +2207,7 @@ int bdrv_commit(BlockDriverState *bs)
      int n, ro, open_flags;
      int ret = 0;
      uint8_t *buf = NULL;
-    char filename[PATH_MAX];
+    char *filename = NULL;

      if (!drv)
          return -ENOMEDIUM;
@@ -2222,13 +2222,14 @@ int bdrv_commit(BlockDriverState *bs)
      }

      ro = bs->backing_hd->read_only;
-    /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */
-    pstrcpy(filename, sizeof(filename), bs->backing_hd->filename);
+    /* filename must be NUL-terminated. */
+    filename = g_strndup(bs->backing_hd->filename, PATH_MAX - 1);
      open_flags =  bs->backing_hd->open_flags;

      if (ro) {
          if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {
-            return -EACCES;
+            ret = -EACCES;
+            goto exit;
          }
      }

@@ -2307,6 +2308,8 @@ ro_cleanup:
          bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);
      }

+exit:
+    g_free(filename);
      return ret;
  }



Reviewed-by: John Snow <address@hidden>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]