[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v2 4/6] block: move string allocation from stack to
From: |
Jeff Cody |
Subject: |
[Qemu-devel] [PATCH v2 4/6] block: move string allocation from stack to the heap |
Date: |
Tue, 20 Jan 2015 12:31:31 -0500 |
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;
}
--
1.9.3
- [Qemu-devel] [PATCH v2 0/6] RESEND - Update filename string sizes in block layer, Jeff Cody, 2015/01/20
- [Qemu-devel] [PATCH v2 2/6] block: vmdk - move string allocations from stack to the heap, Jeff Cody, 2015/01/20
- [Qemu-devel] [PATCH v2 3/6] block: qapi - move string allocation from stack to the heap, Jeff Cody, 2015/01/20
- [Qemu-devel] [PATCH v2 4/6] block: move string allocation from stack to the heap,
Jeff Cody <=
- [Qemu-devel] [PATCH v2 5/6] block: mirror - change string allocation to 2-bytes, Jeff Cody, 2015/01/20
- [Qemu-devel] [PATCH v2 1/6] block: vmdk - make ret variable usage clear, Jeff Cody, 2015/01/20
- [Qemu-devel] [PATCH v2 6/6] block: update string sizes for filename, backing_file, exact_filename, Jeff Cody, 2015/01/20