From patchwork Tue Aug 25 23:37:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 7073541 Return-Path: X-Original-To: patchwork-linux-nvdimm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id A3D5AC05AC for ; Tue, 25 Aug 2015 23:37:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AEFF5207FE for ; Tue, 25 Aug 2015 23:37:39 +0000 (UTC) Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B74BD20764 for ; Tue, 25 Aug 2015 23:37:38 +0000 (UTC) Received: from ml01.vlan14.01.org (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id AB8EC18288E; Tue, 25 Aug 2015 16:37:38 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by ml01.01.org (Postfix) with ESMTP id 9E56B1827FC for ; Tue, 25 Aug 2015 16:37:37 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP; 25 Aug 2015 16:37:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,412,1437462000"; d="scan'208";a="790925209" Received: from vverma7-desk2.amr.corp.intel.com (HELO omniknight.lm.intel.com) ([10.232.112.76]) by orsmga002.jf.intel.com with ESMTP; 25 Aug 2015 16:37:37 -0700 From: Vishal Verma To: linux-nvdimm@lists.01.org Subject: [ndctl PATCH 1/2] ndctl: fix a double free in test-parent-uuid.c Date: Tue, 25 Aug 2015 17:37:13 -0600 Message-Id: <1440545834-10559-2-git-send-email-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1440545834-10559-1-git-send-email-vishal.l.verma@intel.com> References: <1440545834-10559-1-git-send-email-vishal.l.verma@intel.com> X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The test-parent-uuid test had double-frees with ndctl_unref being called both from the do_test function as well as from test_parent_uuid. Simplify the error handling in do_test. Use the _safe versions of the list walking helper macros for namespace/btt deletion. Also fix a minor style issue with goto labels. Reported-by: Ross Zwisler Signed-off-by: Vishal Verma --- lib/test-parent-uuid.c | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/test-parent-uuid.c b/lib/test-parent-uuid.c index 46e0060..9681a99 100644 --- a/lib/test-parent-uuid.c +++ b/lib/test-parent-uuid.c @@ -134,9 +134,9 @@ static int do_test(struct ndctl_ctx *ctx) { int rc; struct ndctl_bus *bus; - struct ndctl_btt *btt, *found = NULL; + struct ndctl_btt *btt, *found = NULL, *_btt; struct ndctl_region *region, *blk_region; - struct ndctl_namespace *ndns; + struct ndctl_namespace *ndns, *_ndns; unsigned long long ns_size = 18874368; uuid_t uuid = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16}; uuid_t btt_uuid; @@ -144,8 +144,7 @@ static int do_test(struct ndctl_ctx *ctx) bus = get_bus_by_provider(ctx, PROVIDER); if (!bus) { fprintf(stderr, "failed to find NFIT-provider: %s\n", PROVIDER); - rc = -ENODEV; - goto err_nobus; + return -ENODEV; } ndctl_region_foreach(bus, region) @@ -156,15 +155,14 @@ static int do_test(struct ndctl_ctx *ctx) if (!blk_region) { fprintf(stderr, "failed to find block region\n"); - rc = -ENODEV; - goto err_cleanup; + return -ENODEV; } /* create a blk namespace */ ndns = create_blk_namespace(1, blk_region, ns_size, uuid); if (!ndns) { fprintf(stderr, "failed to create block namespace\n"); - goto err_cleanup; + return -ENXIO; } /* create a btt for this namespace */ @@ -180,7 +178,7 @@ static int do_test(struct ndctl_ctx *ctx) rc = ndctl_btt_enable(btt); if (rc) { fprintf(stderr, "failed to create btt 0\n"); - goto err_cleanup; + return rc; } /* disable the btt */ @@ -191,15 +189,13 @@ static int do_test(struct ndctl_ctx *ctx) ndns = create_blk_namespace(1, blk_region, ns_size, uuid); if (!ndns) { fprintf(stderr, "failed to re-create block namespace\n"); - goto err_cleanup; + return -ENXIO; } /* Verify btt was auto-created */ found = check_valid_btt(blk_region, ndns, btt_uuid); - if (!found) { - rc = -ENXIO; - goto err_cleanup; - } + if (!found) + return -ENXIO; btt = found; /*disable the btt and namespace again */ @@ -211,31 +207,27 @@ static int do_test(struct ndctl_ctx *ctx) ndns = create_blk_namespace(1, blk_region, ns_size, uuid); if (!ndns) { fprintf(stderr, "failed to re-create block namespace\n"); - goto err_cleanup; + return -ENXIO; } /* make sure there is no btt on this namespace */ found = check_valid_btt(blk_region, ndns, btt_uuid); if (found) { fprintf(stderr, "found a stale btt\n"); - rc = -ENXIO; - goto err_cleanup; + return -ENXIO; } -err_cleanup: - ndctl_btt_foreach(blk_region, btt) + ndctl_btt_foreach_safe(blk_region, btt, _btt) ndctl_btt_delete(btt); - ndctl_namespace_foreach(blk_region, ndns) + ndctl_namespace_foreach_safe(blk_region, ndns, _ndns) if (ndctl_namespace_get_size(ndns) != 0) disable_blk_namespace(ndns); + ndctl_region_foreach(bus, region) ndctl_region_disable_invalidate(region); - - err_nobus: - ndctl_unref(ctx); - return rc; + return 0; } int test_parent_uuid(int loglevel) @@ -269,9 +261,9 @@ int test_parent_uuid(int loglevel) result = EXIT_SUCCESS; kmod_module_remove_module(mod, 0); -err_module: + err_module: kmod_unref(kmod_ctx); -err_kmod: + err_kmod: ndctl_unref(ctx); return result; }