From: Vishal Verma <vishal.l.verma@intel.com>
To: linux-nvdimm@lists.01.org
Subject: [ndctl PATCH 2/4] ndctl, monitor: fix memory leak in read_config_file
Date: Thu, 19 Jul 2018 16:59:56 -0600 [thread overview]
Message-ID: <20180719225958.6814-3-vishal.l.verma@intel.com> (raw)
In-Reply-To: <20180719225958.6814-1-vishal.l.verma@intel.com>
Static analysis reports that we leak 'buf' in the above function. Free
that, and refactor some of the error reporting to clean up the flow.
Since the parser loop was modifying 'buf', it wasn't possible to
directly free it. Introduce a 'seek' variable to seek within the buf so
that buf itself remains untouched, and can later be freed.
Fixes: 80dd56d82dd3 ("ndctl, monitor: add main ndctl monitor configuration file")
Cc: QI Fuli <qi.fuli@jp.fujitsu.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
ndctl/monitor.c | 44 +++++++++++++++++++++++---------------------
1 file changed, 23 insertions(+), 21 deletions(-)
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index abab45f..4d25c2b 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -478,9 +478,9 @@ static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor,
struct util_filter_params *_param)
{
FILE *f;
- int line = 0;
size_t len = 0;
- char *buf, *value, *config_file;
+ int line = 0, rc = 0;
+ char *buf = NULL, *seek, *value, *config_file;
if (_monitor->config_file)
config_file = strdup(_monitor->config_file);
@@ -488,32 +488,36 @@ static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor,
config_file = strdup(DEF_CONF_FILE);
if (!config_file) {
fail("strdup default config file failed\n");
+ rc = -ENOMEM;
goto out;
}
buf = malloc(BUF_SIZE);
if (!buf) {
fail("malloc read config-file buf error\n");
+ rc = -ENOMEM;
goto out;
}
+ seek = buf;
f = fopen(config_file, "r");
if (!f) {
fail("config-file: %s cannot be opened\n", config_file);
+ rc = -errno;
goto out;
}
- while (fgets(buf, BUF_SIZE, f)) {
+ while (fgets(seek, BUF_SIZE, f)) {
value = NULL;
line++;
- while (isspace(*buf))
- buf++;
+ while (isspace(*seek))
+ seek++;
- if (*buf == '#' || *buf == '\0')
+ if (*seek == '#' || *seek == '\0')
continue;
- value = strchr(buf, '=');
+ value = strchr(seek, '=');
if (!value) {
fail("config-file syntax error, skip line[%i]\n", line);
continue;
@@ -525,12 +529,12 @@ static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor,
while (isspace(value[0]))
value++;
- len = strlen(buf);
+ len = strlen(seek);
if (len == 0)
continue;
- while (isspace(buf[len-1]))
+ while (isspace(seek[len-1]))
len--;
- buf[len] = '\0';
+ seek[len] = '\0';
len = strlen(value);
if (len == 0)
@@ -542,22 +546,20 @@ static int read_config_file(struct ndctl_ctx *ctx, struct monitor *_monitor,
if (len == 0)
continue;
- parse_config(&_param->bus, "bus", value, buf);
- parse_config(&_param->dimm, "dimm", value, buf);
- parse_config(&_param->region, "region", value, buf);
- parse_config(&_param->namespace, "namespace", value, buf);
- parse_config(&_monitor->dimm_event, "dimm-event", value, buf);
+ parse_config(&_param->bus, "bus", value, seek);
+ parse_config(&_param->dimm, "dimm", value, seek);
+ parse_config(&_param->region, "region", value, seek);
+ parse_config(&_param->namespace, "namespace", value, seek);
+ parse_config(&_monitor->dimm_event, "dimm-event", value, seek);
if (!_monitor->log)
- parse_config(&_monitor->log, "log", value, buf);
+ parse_config(&_monitor->log, "log", value, seek);
}
fclose(f);
- free(config_file);
- return 0;
out:
- if (config_file)
- free(config_file);
- return 1;
+ free(buf);
+ free(config_file);
+ return rc;
}
int cmd_monitor(int argc, const char **argv, void *ctx)
--
2.14.4
_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm
next prev parent reply other threads:[~2018-07-19 23:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-19 22:59 [ndctl PATCH 0/4] misc fixups for ndctl-monitor Vishal Verma
2018-07-19 22:59 ` [ndctl PATCH 1/4] ndctl, monitor: Add a config-file section to the man page Vishal Verma
2018-07-19 22:59 ` Vishal Verma [this message]
2018-07-19 22:59 ` [ndctl PATCH 3/4] ndctl, monitor: Fix memory leak in monitor_event Vishal Verma
2018-07-19 22:59 ` [ndctl PATCH 4/4] ndctl, monitor: improve error reporting throughout monitor.c Vishal Verma
2018-07-19 23:59 ` [ndctl PATCH 0/4] misc fixups for ndctl-monitor Qi, Fuli
2018-07-20 3:26 ` Vishal Verma
2018-07-20 4:10 ` Qi, Fuli
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=20180719225958.6814-3-vishal.l.verma@intel.com \
--to=vishal.l.verma@intel.com \
--cc=linux-nvdimm@lists.01.org \
/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 NNTP newsgroup(s).