From patchwork Wed May 4 01:22:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 618202 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r00gf649sz9t6W for ; Wed, 4 May 2016 11:24:14 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3r00gf5JSFzDqBs for ; Wed, 4 May 2016 11:24:14 +1000 (AEST) X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Received: from e23smtp02.au.ibm.com (e23smtp02.au.ibm.com [202.81.31.144]) (using TLSv1.2 with cipher CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3r00gZ5yhjzDq7j for ; Wed, 4 May 2016 11:24:10 +1000 (AEST) Received: from localhost by e23smtp02.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 4 May 2016 11:24:09 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp02.au.ibm.com (202.81.31.208) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 4 May 2016 11:24:08 +1000 X-IBM-Helo: d23dlp01.au.ibm.com X-IBM-MailFrom: andrew.donnellan@au1.ibm.com X-IBM-RcptTo: petitboot@lists.ozlabs.org Received: from d23relay09.au.ibm.com (d23relay09.au.ibm.com [9.185.63.181]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id 447C32CE8046 for ; Wed, 4 May 2016 11:24:07 +1000 (EST) Received: from d23av02.au.ibm.com (d23av02.au.ibm.com [9.190.235.138]) by d23relay09.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u441NxLS65142898 for ; Wed, 4 May 2016 11:24:07 +1000 Received: from d23av02.au.ibm.com (localhost [127.0.0.1]) by d23av02.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u441NZLs026747 for ; Wed, 4 May 2016 11:23:35 +1000 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.192.253.14]) by d23av02.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u441NSjd026143 for ; Wed, 4 May 2016 11:23:32 +1000 Received: from ajd.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher AES128-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id F261BA01E9 for ; Wed, 4 May 2016 11:23:03 +1000 (AEST) From: Andrew Donnellan To: petitboot@lists.ozlabs.org Subject: [PATCH v2] lib/flash: fix resource leak in flash_setup_buffer() error paths Date: Wed, 4 May 2016 11:22:57 +1000 Message-Id: <1462324977-8936-1-git-send-email-andrew.donnellan@au1.ibm.com> X-Mailer: git-send-email 2.6.2 X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16050401-0005-0000-0000-000007993C6F X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" Some error paths in flash_setup_buffer() fail to free the flash_info struct or close the open ffs before they return. Change them to goto the cleanup code at the end. Separate the cleanup code into separate labels depending on whether we need to call ffs_close(), arch_flash_close() and talloc_free(). Signed-off-by: Andrew Donnellan --- Found by Coverity Scan. Build-tested only. sammj, please bikeshed :) V1->V2: - Added missing ffs_close() call, suggested by Cyril Bur --- lib/flash/flash.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/flash/flash.c b/lib/flash/flash.c index b6188a0..1384118 100644 --- a/lib/flash/flash.c +++ b/lib/flash/flash.c @@ -82,26 +82,26 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition) rc = arch_flash_init(&info->bl, NULL, true); if (rc) { pb_log("Failed to init mtd device\n"); - return NULL; + goto out; } rc = blocklevel_get_info(info->bl, &info->path, &info->size, &info->erase_granule); if (rc) { pb_log("Failed to retrieve blocklevel info\n"); - return NULL; + goto out_flash; } rc = ffs_init(0, info->size, info->bl, &info->ffs, 1); if (rc) { pb_log("%s: Failed to init ffs\n", __func__); - goto out; + goto out_flash; } rc = partition_info(info, partition); if (rc) { pb_log("Failed to retrieve partition info\n"); - goto out; + goto out_ffs; } /* Check if there is a second flash side. If there is not, or @@ -139,8 +139,11 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition) } return info; -out: +out_ffs: + ffs_close(info->ffs); +out_flash: arch_flash_close(info->bl, NULL); +out: talloc_free(info); return NULL; }