From: "Verma, Vishal L" <vishal.l.verma@intel.com>
To: "jmoyer@redhat.com" <jmoyer@redhat.com>,
"nvdimm@lists.linux.dev" <nvdimm@lists.linux.dev>
Subject: Re: [PATCH ndctl 1/2] ndctl/keys.c: don't leak fd in error cases
Date: Mon, 6 May 2024 19:11:30 +0000 [thread overview]
Message-ID: <02a2425a8576ff86b5f1741521ccf7c25714b7d7.camel@intel.com> (raw)
In-Reply-To: <20240503205456.80004-2-jmoyer@redhat.com>
On Fri, 2024-05-03 at 16:54 -0400, jmoyer@redhat.com wrote:
> From: Jeff Moyer <jmoyer@redhat.com>
>
> Static analysis points out that fd is leaked in some cases. The
> change to the while loop is optional. I only did that to make the
> code consistent.
>
> Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
Looks good,
Reviewed-by: Vishal Verma <vishal.l.verma@intel.com>
> ---
> ndctl/keys.c | 16 ++++++++--------
> 1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/ndctl/keys.c b/ndctl/keys.c
> index 2c1f474..cc55204 100644
> --- a/ndctl/keys.c
> +++ b/ndctl/keys.c
> @@ -108,7 +108,7 @@ char *ndctl_load_key_blob(const char *path, int
> *size, const char *postfix,
> struct stat st;
> ssize_t read_bytes = 0;
> int rc, fd;
> - char *blob, *pl, *rdptr;
> + char *blob = NULL, *pl, *rdptr;
> char prefix[] = "load ";
> bool need_prefix = false;
>
> @@ -125,16 +125,16 @@ char *ndctl_load_key_blob(const char *path, int
> *size, const char *postfix,
> rc = fstat(fd, &st);
> if (rc < 0) {
> fprintf(stderr, "stat: %s\n", strerror(errno));
> - return NULL;
> + goto out_close;
> }
> if ((st.st_mode & S_IFMT) != S_IFREG) {
> fprintf(stderr, "%s not a regular file\n", path);
> - return NULL;
> + goto out_close;
> }
>
> if (st.st_size == 0 || st.st_size > 4096) {
> fprintf(stderr, "Invalid blob file size\n");
> - return NULL;
> + goto out_close;
> }
>
> *size = st.st_size;
> @@ -166,15 +166,13 @@ char *ndctl_load_key_blob(const char *path, int
> *size, const char *postfix,
> fprintf(stderr, "Failed to read from blob
> file: %s\n",
> strerror(errno));
> free(blob);
> - close(fd);
> - return NULL;
> + blob = NULL;
> + goto out_close;
> }
> read_bytes += rc;
> rdptr += rc;
> } while (read_bytes != st.st_size);
>
> - close(fd);
> -
> if (postfix) {
> pl += read_bytes;
> *pl = ' ';
> @@ -182,6 +180,8 @@ char *ndctl_load_key_blob(const char *path, int
> *size, const char *postfix,
> rc = sprintf(pl, "keyhandle=%s", postfix);
> }
>
> +out_close:
> + close(fd);
> return blob;
> }
>
next prev parent reply other threads:[~2024-05-06 19:11 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-03 20:54 [PATCH ndctl 0/2] fix errors pointed out by static analysis jmoyer
2024-05-03 20:54 ` [PATCH ndctl 1/2] ndctl/keys.c: don't leak fd in error cases jmoyer
2024-05-06 19:11 ` Verma, Vishal L [this message]
2024-05-03 20:54 ` [PATCH ndctl 2/2] libndctl.c: major and minor numbers are unsigned jmoyer
2024-05-06 19:11 ` Verma, Vishal L
-- strict thread matches above, loose matches on Subject: below --
2024-08-20 18:26 address some open scan hub warnings jmoyer
2024-08-20 18:26 ` [PATCH ndctl 1/2] ndctl/keys.c: don't leak fd in error cases jmoyer
2024-08-20 19:59 ` Dave Jiang
2024-08-21 21:50 ` Verma, Vishal L
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=02a2425a8576ff86b5f1741521ccf7c25714b7d7.camel@intel.com \
--to=vishal.l.verma@intel.com \
--cc=jmoyer@redhat.com \
--cc=nvdimm@lists.linux.dev \
/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: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox