From patchwork Tue May 3 02:27:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 617741 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 3qzQ830Dh0z9t4R for ; Tue, 3 May 2016 12:28:19 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3qzQ826ZdnzDqBW for ; Tue, 3 May 2016 12:28:18 +1000 (AEST) X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Received: from e23smtp09.au.ibm.com (e23smtp09.au.ibm.com [202.81.31.142]) (using TLSv1.2 with cipher CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3qzQ802H1PzDq6B for ; Tue, 3 May 2016 12:28:16 +1000 (AEST) Received: from localhost by e23smtp09.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 3 May 2016 12:28:14 +1000 Received: from d23dlp02.au.ibm.com (202.81.31.213) by e23smtp09.au.ibm.com (202.81.31.206) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 3 May 2016 12:28:12 +1000 X-IBM-Helo: d23dlp02.au.ibm.com X-IBM-MailFrom: andrew.donnellan@au1.ibm.com X-IBM-RcptTo: petitboot@lists.ozlabs.org Received: from d23relay07.au.ibm.com (d23relay07.au.ibm.com [9.190.26.37]) by d23dlp02.au.ibm.com (Postfix) with ESMTP id 07A9D2BB005A for ; Tue, 3 May 2016 12:28:12 +1000 (EST) Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay07.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u432S33e61866114 for ; Tue, 3 May 2016 12:28:11 +1000 Received: from d23av04.au.ibm.com (localhost [127.0.0.1]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u432Rde5013961 for ; Tue, 3 May 2016 12:27:39 +1000 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.192.253.14]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u432Rdng013419 for ; Tue, 3 May 2016 12:27:39 +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 52E0EA0117 for ; Tue, 3 May 2016 12:27:15 +1000 (AEST) From: Andrew Donnellan To: petitboot@lists.ozlabs.org Subject: [PATCH] lib/flash: fix resource leak in flash_setup_buffer() error paths Date: Tue, 3 May 2016 12:27:08 +1000 Message-Id: <1462242428-583-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: 16050302-0033-0000-0000-000005BA0DF3 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() don't free the flash_info struct allocated in that function before they return. Change them to goto the cleanup code at the end. Separate the cleanup code into separate labels for calling arch_flash_close() and just freeing memory. Signed-off-by: Andrew Donnellan --- Found by Coverity Scan. Build-tested only. sammj, please bikeshed :) --- lib/flash/flash.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/flash/flash.c b/lib/flash/flash.c index b6188a0..107ba9e 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_close; } 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_close; } rc = partition_info(info, partition); if (rc) { pb_log("Failed to retrieve partition info\n"); - goto out; + goto out_close; } /* Check if there is a second flash side. If there is not, or @@ -139,8 +139,9 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition) } return info; -out: +out_close: arch_flash_close(info->bl, NULL); +out: talloc_free(info); return NULL; }