nvdimm.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Vishal Verma <vishal.l.verma@intel.com>
To: linux-nvdimm@lists.01.org
Subject: [ndctl PATCH 3/4] ndctl, monitor: Fix memory leak in monitor_event
Date: Thu, 19 Jul 2018 16:59:57 -0600	[thread overview]
Message-ID: <20180719225958.6814-4-vishal.l.verma@intel.com> (raw)
In-Reply-To: <20180719225958.6814-1-vishal.l.verma@intel.com>

Static analysis reports we were leaking the 'events' buffer in the above
function. We were also returning a simple '1' for every error case. Take
this chance to return a more meaningful 'errno' in each case, while also
fixing up the resource leak.

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 | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 4d25c2b..7fc2aaa 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -360,32 +360,35 @@ static int monitor_event(struct ndctl_ctx *ctx,
 		struct monitor_filter_arg *mfa)
 {
 	struct epoll_event ev, *events;
-	int nfds, epollfd, i, rc;
+	int nfds, epollfd, i, rc = 0;
 	struct monitor_dimm *mdimm;
 	char buf;
 
 	events = calloc(mfa->num_dimm, sizeof(struct epoll_event));
 	if (!events) {
 		err(ctx, "malloc for events error\n");
-		return 1;
+		return -ENOMEM;
 	}
 	epollfd = epoll_create1(0);
 	if (epollfd == -1) {
 		err(ctx, "epoll_create1 error\n");
-		return 1;
+		rc = -errno;
+		goto out;
 	}
 	list_for_each(&mfa->dimms, mdimm, list) {
 		memset(&ev, 0, sizeof(ev));
 		rc = pread(mdimm->health_eventfd, &buf, sizeof(buf), 0);
 		if (rc < 0) {
 			err(ctx, "pread error\n");
-			return 1;
+			rc = -errno;
+			goto out;
 		}
 		ev.data.ptr = mdimm;
 		if (epoll_ctl(epollfd, EPOLL_CTL_ADD,
 				mdimm->health_eventfd, &ev) != 0) {
 			err(ctx, "epoll_ctl error\n");
-			return 1;
+			rc = -errno;
+			goto out;
 		}
 	}
 
@@ -394,7 +397,8 @@ static int monitor_event(struct ndctl_ctx *ctx,
 		nfds = epoll_wait(epollfd, events, mfa->num_dimm, -1);
 		if (nfds <= 0) {
 			err(ctx, "epoll_wait error\n");
-			return 1;
+			rc = -errno;
+			goto out;
 		}
 		for (i = 0; i < nfds; i++) {
 			mdimm = events[i].data.ptr;
@@ -404,13 +408,18 @@ static int monitor_event(struct ndctl_ctx *ctx,
 						ndctl_dimm_get_devname(mdimm->dimm));
 			}
 			rc = pread(mdimm->health_eventfd, &buf, sizeof(buf), 0);
-			if (rc < 0)
-				fail("pread error\n");
+			if (rc < 0) {
+				err(ctx, "pread error\n");
+				rc = -errno;
+				goto out;
+			}
 		}
 		if (did_fail)
 			return 1;
 	}
-	return 0;
+ out:
+	free(events);
+	return rc;
 }
 
 static int parse_monitor_event(struct monitor *_monitor, struct ndctl_ctx *ctx)
-- 
2.14.4

_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm

  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 ` [ndctl PATCH 2/4] ndctl, monitor: fix memory leak in read_config_file Vishal Verma
2018-07-19 22:59 ` Vishal Verma [this message]
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-4-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).