diff mbox series

[ndctl,2/4] cxl/region: fix a resource leak in to_csv()

Message ID 20230110-vv-coverity-fixes-v1-2-c7ee6c76b200@intel.com (mailing list archive)
State Accepted
Commit 5df0713d707b1544a442e535c14962351b009ca4
Headers show
Series cxl: misc coverity and typo fixes | expand

Commit Message

Verma, Vishal L Jan. 10, 2023, 11:09 p.m. UTC
Static analysis reports there can be a memory leak in to_csv as an exit
path returns from the function before freeing 'csv'. Since this is the
only errpr path exit point after the allocation, just free() before
returning.

Fixes: 3d6cd829ec08 ("cxl/region: Use cxl_filter_walk() to gather create-region targets")
Cc: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 cxl/region.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Alison Schofield Jan. 11, 2023, 10:52 p.m. UTC | #1
On Tue, Jan 10, 2023 at 04:09:15PM -0700, Vishal Verma wrote:
> Static analysis reports there can be a memory leak in to_csv as an exit
> path returns from the function before freeing 'csv'. Since this is the
> only errpr path exit point after the allocation, just free() before
> returning.
> 
> Fixes: 3d6cd829ec08 ("cxl/region: Use cxl_filter_walk() to gather create-region targets")
> Cc: Dan Williams <dan.j.williams@intel.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>

Reviewed-by: Alison Schofield <alison.schofield@intel.com>


> ---
>  cxl/region.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/cxl/region.c b/cxl/region.c
> index bb3a10a..9a81113 100644
> --- a/cxl/region.c
> +++ b/cxl/region.c
> @@ -146,8 +146,10 @@ static const char *to_csv(int *count, const char **strings)
>  		return NULL;
>  	for (i = 0; i < *count; i++) {
>  		list = strdup(strings[i]);
> -		if (!list)
> +		if (!list) {
> +			free(csv);
>  			return NULL;
> +		}
>  
>  		for (arg = strtok_r(list, which_sep(list), &save); arg;
>  		     arg = strtok_r(NULL, which_sep(list), &save)) {
> 
> -- 
> 2.39.0
diff mbox series

Patch

diff --git a/cxl/region.c b/cxl/region.c
index bb3a10a..9a81113 100644
--- a/cxl/region.c
+++ b/cxl/region.c
@@ -146,8 +146,10 @@  static const char *to_csv(int *count, const char **strings)
 		return NULL;
 	for (i = 0; i < *count; i++) {
 		list = strdup(strings[i]);
-		if (!list)
+		if (!list) {
+			free(csv);
 			return NULL;
+		}
 
 		for (arg = strtok_r(list, which_sep(list), &save); arg;
 		     arg = strtok_r(NULL, which_sep(list), &save)) {