From patchwork Tue Jan 20 17:31:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Cody X-Patchwork-Id: 431204 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 7EED81402D9 for ; Wed, 21 Jan 2015 04:32:25 +1100 (AEDT) Received: from localhost ([::1]:44908 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YDcf9-0006zn-Fa for incoming@patchwork.ozlabs.org; Tue, 20 Jan 2015 12:32:23 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45495) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YDceb-00062H-Ir for qemu-devel@nongnu.org; Tue, 20 Jan 2015 12:31:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YDcea-0003Km-QS for qemu-devel@nongnu.org; Tue, 20 Jan 2015 12:31:49 -0500 Received: from mx1.redhat.com ([209.132.183.28]:53033) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YDcea-0003Kg-K3 for qemu-devel@nongnu.org; Tue, 20 Jan 2015 12:31:48 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t0KHVlgs031407 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Tue, 20 Jan 2015 12:31:47 -0500 Received: from localhost (ovpn-112-98.phx2.redhat.com [10.3.112.98]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t0KHVi6p026038 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Tue, 20 Jan 2015 12:31:46 -0500 From: Jeff Cody To: qemu-devel@nongnu.org Date: Tue, 20 Jan 2015 12:31:31 -0500 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, famz@redhat.com, jsnow@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v2 4/6] block: move string allocation from stack to the heap X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Reviewed-by: John Snow --- 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; }