Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1531255ybj; Fri, 8 May 2020 03:34:36 -0700 (PDT) X-Google-Smtp-Source: APiQypITqEoJ1AnYEUl/MmyyVA1zbh863xjuPnv9FWzACNKLG/F264AcL+S54ENJ6j/Fqcc9yV9u X-Received: by 2002:a05:6402:30ad:: with SMTP id df13mr1539976edb.339.1588934076146; Fri, 08 May 2020 03:34:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588934076; cv=none; d=google.com; s=arc-20160816; b=dee7Qbv+x3t+WZaV0pBwCt6SmzZeaLqIghOtKqZIfQjrH07NClcsphBTB0RVG41kRH 1eh7dltdTabtEwg/P0CIgUrWdOYdTZ5sD5bMUNBfnZ6VPsOX03UL9B7FRzNSUpewR+tL 4HZ8MGpFwc49J3pNs2quwIzwc0NQb8QyYpYaTBcNn7jRHxfgduWaJ/5GOrhyNOu/SqKf SIAsPRbNFVbDO1hTU+turGY7uZO7/GoiufnlQlmnpYL4zv5hd7pZHBAdCAhIcTRRnhgg CFtMSUboiwfnLShSyHLFeJ2ZdMFO/1N7Kn/kOA6N0azf09kGfH0o1+qgSlTWp8Yckf/s I3hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=9eQbxP/rJrOaFCy+ksRARimGXrk1i6TZxDMuPHbpMyE=; b=XiuoNEYUMa0vPDc9I1IEsdPHAYL1mjavab87MSzNW0vVQRz1SInqbeoh0u7ULin51s nVyjztj3bGXxjgLG7+1SXOkAALG1yi+D5Z/kdXm6/btDK6YMuDaNHkasy0taKoI0CY8K p4B1jnLA3JGR3sXUHrlCvPdD+gtV1xLjY4JgNkmFyw9Ix00jOlpRSHE8BqIMPn9xJ+zu G5sd/2NnknIRmxXnC9wusKippT1YXcVE3AZgiSCQwmMUXzoidx4mLqjR5wjADwm8Tjml lGZeKAl5Y2NWRbCA8NCL127fgq1lM1NpDYbn3ZUVGIGV0N+5+hGyb99KUSZrf+CFrCDg Omww== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f8si788874edk.160.2020.05.08.03.34.12; Fri, 08 May 2020 03:34:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726771AbgEHKco (ORCPT + 99 others); Fri, 8 May 2020 06:32:44 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:60028 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725825AbgEHKco (ORCPT ); Fri, 8 May 2020 06:32:44 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id B9288C16EDEDE9957474; Fri, 8 May 2020 18:32:40 +0800 (CST) Received: from [127.0.0.1] (10.166.215.237) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Fri, 8 May 2020 18:32:31 +0800 Subject: Re: [PATCH v2] tools/bootconfig: fix resource leak in apply_xbc() To: Dan Carpenter CC: , , , , , Shiyuan Hu , Hewenliang References: <189d719f-a8b8-6e10-ae2f-8120c3d2b7a9@huawei.com> <20200508093059.GF9365@kadam> From: Yunfeng Ye Message-ID: <5e8e9e2e-e7a0-a9f7-79d7-1b48d5f7a6ae@huawei.com> Date: Fri, 8 May 2020 18:32:19 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20200508093059.GF9365@kadam> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.166.215.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020/5/8 17:30, Dan Carpenter wrote: > On Fri, May 08, 2020 at 02:51:15PM +0800, Yunfeng Ye wrote: >> diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c >> index 16b9a420e6fd..d034f86022b7 100644 >> --- a/tools/bootconfig/main.c >> +++ b/tools/bootconfig/main.c >> @@ -314,31 +314,33 @@ int apply_xbc(const char *path, const char *xbc_path) >> ret = delete_xbc(path); >> if (ret < 0) { >> pr_err("Failed to delete previous boot config: %d\n", ret); >> - return ret; >> + goto free_data; >> } >> >> /* Apply new one */ >> fd = open(path, O_RDWR | O_APPEND); >> if (fd < 0) { >> pr_err("Failed to open %s: %d\n", path, fd); >> - return fd; >> + ret = fd; >> + goto free_data; >> } >> /* TODO: Ensure the @path is initramfs/initrd image */ >> ret = write(fd, data, size + 8); >> if (ret < 0) { >> pr_err("Failed to apply a boot config: %d\n", ret); >> - return ret; >> + goto close_fd; >> } >> /* Write a magic word of the bootconfig */ >> ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); > > write returns the number of bytes written on success > >> - if (ret < 0) { >> + if (ret < 0) >> pr_err("Failed to apply a boot config magic: %d\n", ret); >> - return ret; >> - } >> + >> +close_fd: >> close(fd); >> +free_data: >> free(data); >> >> - return 0; >> + return ret; > > But we want to return zero on success. > yes, I should set 'ret' to 0 before returning on success. thanks. >> } > > Btw, these leaks are totally harmless. This is a short running user > space program with is going to immediately exit on error so the memory > will be freed anyway. But the benifit is to silence static checker > warnings so that's useful. > > regards, > dan carpenter > > > . >