[PATCH 09/10] fabrics: fix some memory leaks
mwilck at suse.com
mwilck at suse.com
Sat Mar 6 00:36:23 GMT 2021
From: Martin Wilck <mwilck at suse.com>
None of these are critical for "nvme discover" or "nvme connect-all".
Still, silencing valgrind's error messages by fixing them gives some
peace of mind, and a longer-running program like the forthcomint
nvme monitor, leak checks are more important.
Use the previously introduced cleanup macros for this purpose.
Signed-off-by: Martin Wilck <mwilck at suse.com>
---
fabrics.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/fabrics.c b/fabrics.c
index c965beb..f2ff511 100644
--- a/fabrics.c
+++ b/fabrics.c
@@ -49,6 +49,7 @@
#include "common.h"
#include "util/log.h"
+#include "util/cleanup.h"
#ifdef HAVE_SYSTEMD
#include <systemd/sd-id128.h>
@@ -309,7 +310,7 @@ static bool ctrl_matches_connectargs(const char *name, struct connect_args *args
{
struct connect_args cargs;
bool found = false;
- char *path, *addr;
+ char *path = NULL, *addr;
int ret;
bool persistent = true;
@@ -369,6 +370,8 @@ static bool ctrl_matches_connectargs(const char *name, struct connect_args *args
free(cargs.traddr);
free(cargs.trsvcid);
free(cargs.host_traddr);
+ free(addr);
+ free(path);
return found;
}
@@ -425,13 +428,18 @@ static struct connect_args *extract_connect_args(char *argstr)
return cargs;
}
-static void free_connect_args(struct connect_args *cargs)
+static void destruct_connect_args(struct connect_args *cargs)
{
free(cargs->subsysnqn);
free(cargs->transport);
free(cargs->traddr);
free(cargs->trsvcid);
free(cargs->host_traddr);
+}
+
+static void free_connect_args(struct connect_args *cargs)
+{
+ destruct_connect_args(cargs);
free(cargs);
}
@@ -1285,7 +1293,7 @@ retry:
static bool cargs_match_found(struct nvmf_disc_rsp_page_entry *entry)
{
- struct connect_args cargs = {};
+ struct connect_args cargs __cleanup__(destruct_connect_args) = { NULL, };
struct connect_args *c = tracked_ctrls;
cargs.traddr = strdup(entry->traddr);
@@ -1371,9 +1379,11 @@ static void nvmf_get_host_identifiers(int ctrl_instance)
cfg.hostid = nvme_get_ctrl_attr(path, "hostid");
}
+static DEFINE_CLEANUP_FUNC(cleanup_log, struct nvmf_disc_rsp_page_hdr *, free);
+
static int do_discover(char *argstr, bool connect, enum nvme_print_flags flags)
{
- struct nvmf_disc_rsp_page_hdr *log = NULL;
+ struct nvmf_disc_rsp_page_hdr *log __cleanup__(cleanup_log) = NULL;
char *dev_name;
int instance, numrec = 0, ret, err;
int status = 0;
@@ -1457,7 +1467,7 @@ static int discover_from_conf_file(const char *desc, char *argstr,
const struct argconfig_commandline_options *opts, bool connect)
{
FILE *f;
- char line[256], *ptr, *args, **argv;
+ char line[256], *ptr, *all_args, *args, **argv;
int argc, err, ret = 0;
f = fopen(PATH_NVMF_DISC, "r");
@@ -1479,6 +1489,7 @@ static int discover_from_conf_file(const char *desc, char *argstr,
ret = -ENOMEM;
goto out;
}
+ all_args = args;
argv = calloc(MAX_DISC_ARGS, BUF_SIZE);
if (!argv) {
@@ -1522,7 +1533,7 @@ static int discover_from_conf_file(const char *desc, char *argstr,
ret = err;
free_and_continue:
- free(args);
+ free(all_args);
free(argv);
}
--
2.29.2
More information about the Linux-nvme
mailing list