From: Vishal Verma <vishal.l.verma@intel.com>
To: linux-nvdimm@lists.01.org
Subject: [ndctl PATCH] ndctl, monitor: fix a resource leak in parse_monitor_event
Date: Tue, 24 Jul 2018 18:09:13 -0600 [thread overview]
Message-ID: <20180725000913.25528-1-vishal.l.verma@intel.com> (raw)
Static analysis reports we leak dimm_event in the above function.
Fix it by adding an 'out' label for all exit paths, and refactoring out
the 'all events' cases.
Fixes: fdf6b6844ccf ("ndctl, monitor: add a new command - monitor")
Cc: QI Fuli <qi.fuli@jp.fujitsu.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
ndctl/monitor.c | 37 ++++++++++++++++++++++---------------
1 file changed, 22 insertions(+), 15 deletions(-)
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index dbad7aa..b97d1ea 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -410,22 +410,35 @@ static int monitor_event(struct ndctl_ctx *ctx,
return rc;
}
+static void monitor_enable_all_events(struct monitor *_monitor)
+{
+ _monitor->event_flags = ND_EVENT_SPARES_REMAINING
+ | ND_EVENT_MEDIA_TEMPERATURE
+ | ND_EVENT_CTRL_TEMPERATURE
+ | ND_EVENT_HEALTH_STATE
+ | ND_EVENT_UNCLEAN_SHUTDOWN;
+}
+
static int parse_monitor_event(struct monitor *_monitor, struct ndctl_ctx *ctx)
{
char *dimm_event, *save;
const char *event;
+ int rc = 0;
+
+ if (!_monitor->dimm_event) {
+ monitor_enable_all_events(_monitor);
+ return 0;;
+ }
- if (!_monitor->dimm_event)
- goto dimm_event_all;
dimm_event = strdup(_monitor->dimm_event);
if (!dimm_event)
- return 1;
+ return -ENOMEM;
for (event = strtok_r(dimm_event, " ", &save); event;
event = strtok_r(NULL, " ", &save)) {
if (strcmp(event, "all") == 0) {
- free(dimm_event);
- goto dimm_event_all;
+ monitor_enable_all_events(_monitor);
+ goto out;
}
if (strcmp(event, "dimm-spares-remaining") == 0)
_monitor->event_flags |= ND_EVENT_SPARES_REMAINING;
@@ -439,20 +452,14 @@ static int parse_monitor_event(struct monitor *_monitor, struct ndctl_ctx *ctx)
_monitor->event_flags |= ND_EVENT_UNCLEAN_SHUTDOWN;
else {
err(ctx, "no dimm-event named %s\n", event);
- return 1;
+ rc = -EINVAL;
+ goto out;
}
}
+out:
free(dimm_event);
- return 0;
-
-dimm_event_all:
- _monitor->event_flags = ND_EVENT_SPARES_REMAINING
- | ND_EVENT_MEDIA_TEMPERATURE
- | ND_EVENT_CTRL_TEMPERATURE
- | ND_EVENT_HEALTH_STATE
- | ND_EVENT_UNCLEAN_SHUTDOWN;
- return 0;
+ return rc;
}
static void parse_config(const char **arg, char *key, char *val, char *ident)
--
2.14.4
_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm
next reply other threads:[~2018-07-25 0:09 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-25 0:09 Vishal Verma [this message]
2018-07-25 1:46 ` [ndctl PATCH] ndctl, monitor: fix a resource leak in parse_monitor_event 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=20180725000913.25528-1-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).