From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40888) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YDdg5-0006bT-L1 for qemu-devel@nongnu.org; Tue, 20 Jan 2015 13:37:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YDdfz-000477-C8 for qemu-devel@nongnu.org; Tue, 20 Jan 2015 13:37:25 -0500 Received: from mx1.redhat.com ([209.132.183.28]:35906) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YDdfz-000471-3y for qemu-devel@nongnu.org; Tue, 20 Jan 2015 13:37:19 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t0KIbINm008070 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Tue, 20 Jan 2015 13:37:18 -0500 Message-ID: <54BEA05D.7090809@redhat.com> Date: Tue, 20 Jan 2015 13:37:17 -0500 From: John Snow MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2 4/6] block: move string allocation from stack to the heap List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jeff Cody , qemu-devel@nongnu.org Cc: kwolf@redhat.com, famz@redhat.com, stefanha@redhat.com 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 > --- > 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