From patchwork Mon Mar 4 22:39:58 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 2214721 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id DBE09DF2F2 for ; Mon, 4 Mar 2013 21:41:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932639Ab3CDVky (ORCPT ); Mon, 4 Mar 2013 16:40:54 -0500 Received: from nat-pool-rdu.redhat.com ([66.187.233.202]:64532 "EHLO bp-05.lab.msp.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932565Ab3CDVkc (ORCPT ); Mon, 4 Mar 2013 16:40:32 -0500 Received: by bp-05.lab.msp.redhat.com (Postfix, from userid 0) id 0E9471E0AB3; Mon, 4 Mar 2013 16:40:07 -0600 (CST) From: Eric Sandeen To: linux-btrfs@vger.kernel.org Cc: Eric Sandeen Subject: [PATCH 08/14] btrfs-progs: Free resources when returning error from cmd_snapshot() Date: Mon, 4 Mar 2013 16:39:58 -0600 Message-Id: <1362436804-16766-9-git-send-email-sandeen@redhat.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1362436804-16766-1-git-send-email-sandeen@redhat.com> References: <1362436804-16766-1-git-send-email-sandeen@redhat.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org cmd_snapshot() currently returns without freeing resources in almost every error case. Switch to a goto arrangement so all cleanup can be done in one place. Signed-off-by: Eric Sandeen --- cmds-subvolume.c | 41 ++++++++++++++++++++++++----------------- 1 files changed, 24 insertions(+), 17 deletions(-) diff --git a/cmds-subvolume.c b/cmds-subvolume.c index a4d88a1..96f7cbd 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -489,7 +489,9 @@ static const char * const cmd_snapshot_usage[] = { static int cmd_snapshot(int argc, char **argv) { char *subvol, *dst; - int res, fd, fddst, len, e, readonly = 0; + int res, retval; + int fd = -1, fddst = -1; + int len, readonly = 0; char *newname; char *dstdir; struct btrfs_ioctl_vol_args_v2 args; @@ -532,20 +534,21 @@ static int cmd_snapshot(int argc, char **argv) subvol = argv[optind]; dst = argv[optind + 1]; + retval = 1; /* failure */ res = test_issubvolume(subvol); if (res < 0) { fprintf(stderr, "ERROR: error accessing '%s'\n", subvol); - return 1; + goto out; } if (!res) { fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol); - return 1; + goto out; } res = test_isdir(dst); if (res == 0) { fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst); - return 1; + goto out; } if (res > 0) { @@ -563,27 +566,26 @@ static int cmd_snapshot(int argc, char **argv) strchr(newname, '/') ){ fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n", newname); - return 1; + goto out; } len = strlen(newname); if (len == 0 || len >= BTRFS_VOL_NAME_MAX) { fprintf(stderr, "ERROR: snapshot name too long ('%s)\n", newname); - return 1; + goto out; } fddst = open_file_or_dir(dstdir); if (fddst < 0) { fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir); - return 1; + goto out; } fd = open_file_or_dir(subvol); if (fd < 0) { - close(fddst); fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir); - return 1; + goto out; } if (readonly) { @@ -602,20 +604,25 @@ static int cmd_snapshot(int argc, char **argv) args.qgroup_inherit = inherit; } strncpy_null(args.name, newname); - res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args); - e = errno; - close(fd); - close(fddst); - free(inherit); + res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args); if (res < 0) { fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n", - subvol, strerror(e)); - return 1; + subvol, strerror(errno)); + goto out; } - return 0; + retval = 0; /* success */ + +out: + if (fd != -1) + close(fd); + if (fddst != -1) + close(fddst); + free(inherit); + + return retval; } static const char * const cmd_subvol_get_default_usage[] = {