From patchwork Wed Jul 25 00:09:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 10543343 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 650B4112E for ; Wed, 25 Jul 2018 00:09:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E66728DBF for ; Wed, 25 Jul 2018 00:09:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 42AAB293FF; Wed, 25 Jul 2018 00:09:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id ED64129377 for ; Wed, 25 Jul 2018 00:09:30 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 75A38210C1B85; Tue, 24 Jul 2018 17:09:30 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.120; helo=mga04.intel.com; envelope-from=vishal.l.verma@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 2D01E210C1B83 for ; Tue, 24 Jul 2018 17:09:28 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Jul 2018 17:09:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,399,1526367600"; d="scan'208";a="69638546" Received: from vverma7-desk1.lm.intel.com ([10.232.112.133]) by orsmga003.jf.intel.com with ESMTP; 24 Jul 2018 17:09:28 -0700 From: Vishal Verma To: Subject: [ndctl PATCH] ndctl, monitor: fix a resource leak in parse_monitor_event Date: Tue, 24 Jul 2018 18:09:13 -0600 Message-Id: <20180725000913.25528-1-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.14.4 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Vishal Verma --- 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)