From: jmoyer@redhat.com
To: nvdimm@lists.linux.dev
Cc: vishal.l.verma@intel.com, Jeff Moyer <jmoyer@redhat.com>
Subject: [PATCH ndctl 1/2] ndctl/keys.c: don't leak fd in error cases
Date: Fri, 3 May 2024 16:54:55 -0400 [thread overview]
Message-ID: <20240503205456.80004-2-jmoyer@redhat.com> (raw)
In-Reply-To: <20240503205456.80004-1-jmoyer@redhat.com>
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>
---
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;
}
--
2.43.0
next prev parent reply other threads:[~2024-05-03 20:55 UTC|newest]
Thread overview: 5+ 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 ` jmoyer [this message]
2024-05-06 19:11 ` [PATCH ndctl 1/2] ndctl/keys.c: don't leak fd in error cases Verma, Vishal L
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
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=20240503205456.80004-2-jmoyer@redhat.com \
--to=jmoyer@redhat.com \
--cc=nvdimm@lists.linux.dev \
--cc=vishal.l.verma@intel.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: 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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).