From: Dan Carpenter <dan.carpenter@oracle.com> To: Yunfeng Ye <yeyunfeng@huawei.com> Cc: mhiramat@kernel.org, rostedt@goodmis.org, linux-kernel@vger.kernel.org, Markus.Elfring@web.de, kernel-janitors@vger.kernel.org, Shiyuan Hu <hushiyuan@huawei.com>, Hewenliang <hewenliang4@huawei.com> Subject: Re: [PATCH v2] tools/bootconfig: fix resource leak in apply_xbc() Date: Fri, 8 May 2020 12:30:59 +0300 [thread overview] Message-ID: <20200508093059.GF9365@kadam> (raw) In-Reply-To: <189d719f-a8b8-6e10-ae2f-8120c3d2b7a9@huawei.com> 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. > } 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
WARNING: multiple messages have this Message-ID (diff)
From: Dan Carpenter <dan.carpenter@oracle.com> To: Yunfeng Ye <yeyunfeng@huawei.com> Cc: mhiramat@kernel.org, rostedt@goodmis.org, linux-kernel@vger.kernel.org, Markus.Elfring@web.de, kernel-janitors@vger.kernel.org, Shiyuan Hu <hushiyuan@huawei.com>, Hewenliang <hewenliang4@huawei.com> Subject: Re: [PATCH v2] tools/bootconfig: fix resource leak in apply_xbc() Date: Fri, 08 May 2020 09:30:59 +0000 [thread overview] Message-ID: <20200508093059.GF9365@kadam> (raw) In-Reply-To: <189d719f-a8b8-6e10-ae2f-8120c3d2b7a9@huawei.com> 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. > } 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
next prev parent reply other threads:[~2020-05-08 9:31 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-05-08 6:51 [PATCH v2] tools/bootconfig: fix resource leak in apply_xbc() Yunfeng Ye 2020-05-08 6:51 ` Yunfeng Ye 2020-05-08 8:23 ` [PATCH v2] tools/bootconfig: Completion of error handling Markus Elfring 2020-05-08 8:23 ` Markus Elfring 2020-05-08 9:30 ` Dan Carpenter [this message] 2020-05-08 9:30 ` [PATCH v2] tools/bootconfig: fix resource leak in apply_xbc() Dan Carpenter 2020-05-08 10:17 ` [v2] " Markus Elfring 2020-05-08 10:17 ` Markus Elfring 2020-05-08 10:32 ` [PATCH v2] " Yunfeng Ye 2020-05-08 10:32 ` Yunfeng Ye
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20200508093059.GF9365@kadam \ --to=dan.carpenter@oracle.com \ --cc=Markus.Elfring@web.de \ --cc=hewenliang4@huawei.com \ --cc=hushiyuan@huawei.com \ --cc=kernel-janitors@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=mhiramat@kernel.org \ --cc=rostedt@goodmis.org \ --cc=yeyunfeng@huawei.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.