Linux CXL
 help / color / mirror / Atom feed
From: Vishal Verma <vishal.l.verma@intel.com>
To: <linux-cxl@vger.kernel.org>
Cc: <nvdimm@lists.linux.dev>, Dan Williams <dan.j.williams@intel.com>,
	Vishal Verma <vishal.l.verma@intel.com>
Subject: [ndctl PATCH v2 2/3] libcxl: fox a resource leak and a forward NULL check
Date: Tue, 23 Aug 2022 01:45:26 -0600	[thread overview]
Message-ID: <20220823074527.404435-3-vishal.l.verma@intel.com> (raw)
In-Reply-To: <20220823074527.404435-1-vishal.l.verma@intel.com>

Static analysis reports a couple of issues in add_cxl_region(). Firstly,
'path' wasn't freed in the success case, only in the error case.
Secondly, the error handling after 'calloc()'ing the region object
erroneously jumped to the error path which tried to free the region object.

Add anew error label to just free 'path' and return for this exit case.

Cc: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 cxl/lib/libcxl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
index 021d59f..e8c5d44 100644
--- a/cxl/lib/libcxl.c
+++ b/cxl/lib/libcxl.c
@@ -482,7 +482,7 @@ static void *add_cxl_region(void *parent, int id, const char *cxlregion_base)
 
 	region = calloc(1, sizeof(*region));
 	if (!region)
-		goto err;
+		goto err_path;
 
 	region->id = id;
 	region->ctx = ctx;
@@ -551,11 +551,13 @@ static void *add_cxl_region(void *parent, int id, const char *cxlregion_base)
 
 	list_add_sorted(&decoder->regions, region, list, region_start_cmp);
 
+	free(path);
 	return region;
 err:
 	free(region->dev_path);
 	free(region->dev_buf);
 	free(region);
+err_path:
 	free(path);
 	return NULL;
 }
-- 
2.37.2


  parent reply	other threads:[~2022-08-23  7:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23  7:45 [ndctl PATCH v2 0/3] cxl: static analysis fixes Vishal Verma
2022-08-23  7:45 ` [ndctl PATCH v2 1/3] cxl/region: fix a dereferecnce after NULL check Vishal Verma
2022-08-23 17:25   ` Dan Williams
2022-08-24  0:03   ` Dave Jiang
2022-08-24  9:30   ` Jonathan Cameron
2022-08-23  7:45 ` Vishal Verma [this message]
2022-08-23 17:27   ` [ndctl PATCH v2 2/3] libcxl: fox a resource leak and a forward " Dan Williams
2022-08-24  0:04   ` Dave Jiang
2022-08-23  7:45 ` [ndctl PATCH v2 3/3] cxl/filter: Fix an uninitialized pointer dereference Vishal Verma
2022-08-23 17:27   ` Dan Williams
2022-08-24  0:04   ` Dave Jiang
2022-08-24  9:37 ` [ndctl PATCH v2 0/3] cxl: static analysis fixes Jonathan Cameron

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=20220823074527.404435-3-vishal.l.verma@intel.com \
    --to=vishal.l.verma@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    /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