librte_metrics: fix memory leak
diff mbox series

Message ID 20200801060838.20591-1-gaurav1086@gmail.com
State New
Delegated to: Thomas Monjalon
Headers show
Series
  • librte_metrics: fix memory leak
Related show

Checks

Context Check Description
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Gaurav Singh Aug. 1, 2020, 6:08 a.m. UTC
Fix memory leak issue

Signed-off-by: Gaurav Singh <gaurav1086@gmail.com>
---
 lib/librte_metrics/rte_metrics_telemetry.c | 24 +++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

Comments

Honnappa Nagarahalli Aug. 1, 2020, 11:34 p.m. UTC | #1
Hi Gaurav,
	Thanks for the fix. Few comments inline. Also, please follow contributing guidelines [1].  There is a cheat sheet [2] which is very helpful.

[1] https://doc.dpdk.org/guides/contributing/index.html
[2] https://doc.dpdk.org/guides/contributing/cheatsheet.html

> -----Original Message-----
> From: dev <dev-bounces@dpdk.org> On Behalf Of Gaurav Singh
> Sent: Saturday, August 1, 2020 1:09 AM
> To: dev@dpdk.org
> Cc: Gaurav Singh <gaurav1086@gmail.com>
> Subject: [dpdk-dev] [PATCH] librte_metrics: fix memory leak
                                                       ^^^^^^^^^^^^ should be 'lib/metrics'
> 
> 
> Fix memory leak issue
> 
Needs "Fixes" tag and Cc to stable@dpdk.org (check the contribution guidelines)

> Signed-off-by: Gaurav Singh <gaurav1086@gmail.com>
> ---
>  lib/librte_metrics/rte_metrics_telemetry.c | 24 +++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/lib/librte_metrics/rte_metrics_telemetry.c
> b/lib/librte_metrics/rte_metrics_telemetry.c
> index 289ebae0b..9fbe59c62 100644
> --- a/lib/librte_metrics/rte_metrics_telemetry.c
> +++ b/lib/librte_metrics/rte_metrics_telemetry.c
> @@ -41,12 +41,19 @@ rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t
> port_id)
>  	}
> 
>  	xstats_names = malloc(sizeof(*xstats_names) * num_xstats);
> +	if (xstats_names == NULL) {
> +		METRICS_LOG_ERR("Failed to malloc memory for
> xstats_names");
> +		ret = -ENOMEM;
> +		return ret;
You can return -ENOMEM directly without assigning it to 'ret'

> +	}
> +
>  	eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
>  			* num_xstats);
> -	if (eth_xstats_names == NULL || xstats_names == NULL) {
> +	if (eth_xstats_names == NULL) {
>  		METRICS_LOG_ERR("Failed to malloc memory for
> xstats_names");
   ^^^^^^^^^^^^ I think it is worth changing to eth_xstats_names

>  		ret = -ENOMEM;
> -		goto free_xstats;
> +		free(xstats_names);
> +		return ret;
Same here, return -ENOMEM directly

>  	}
> 
>  	if (rte_eth_xstats_get_names(port_id,
> @@ -54,7 +61,7 @@ rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t
> port_id)
>  		METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len %d
> failed",
>  				port_id, num_xstats);
>  		ret = -EPERM;
> -		goto free_xstats;
> +		return ret;
Any reason for this change? This change leaks memory.

>  	}
> 
>  	for (i = 0; i < num_xstats; i++)
> @@ -63,9 +70,6 @@ rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t
> port_id)
>  	if (ret < 0)
>  		METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics
> may already be registered");
> 
> -free_xstats:
> -	free(eth_xstats_names);
> -	free(xstats_names);
Any reason for this change? This is leaking memory in the successful case.

>  	return ret;
>  }
> 
> @@ -167,9 +171,15 @@ rte_metrics_tel_format_port(uint32_t pid, json_t
> *ports,
>  	}
> 
>  	metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
> +	if (metrics == NULL) {
> +		METRICS_LOG_ERR("Cannot allocate memory");
> +		return -ENOMEM;
> +	}
> +
>  	names = malloc(sizeof(struct rte_metric_name) * num_metrics);
> -	if (metrics == NULL || names == NULL) {
> +	if (names == NULL) {
>  		METRICS_LOG_ERR("Cannot allocate memory");
> +		free(metrics);
>  		return -ENOMEM;
>  	}
> 
> --
> 2.17.1

Patch
diff mbox series

diff --git a/lib/librte_metrics/rte_metrics_telemetry.c b/lib/librte_metrics/rte_metrics_telemetry.c
index 289ebae0b..9fbe59c62 100644
--- a/lib/librte_metrics/rte_metrics_telemetry.c
+++ b/lib/librte_metrics/rte_metrics_telemetry.c
@@ -41,12 +41,19 @@  rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
 	}
 
 	xstats_names = malloc(sizeof(*xstats_names) * num_xstats);
+	if (xstats_names == NULL) {
+		METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
+		ret = -ENOMEM;
+		return ret;
+	}
+
 	eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
 			* num_xstats);
-	if (eth_xstats_names == NULL || xstats_names == NULL) {
+	if (eth_xstats_names == NULL) {
 		METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
 		ret = -ENOMEM;
-		goto free_xstats;
+		free(xstats_names);
+		return ret;
 	}
 
 	if (rte_eth_xstats_get_names(port_id,
@@ -54,7 +61,7 @@  rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
 		METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len %d failed",
 				port_id, num_xstats);
 		ret = -EPERM;
-		goto free_xstats;
+		return ret;
 	}
 
 	for (i = 0; i < num_xstats; i++)
@@ -63,9 +70,6 @@  rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
 	if (ret < 0)
 		METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
 
-free_xstats:
-	free(eth_xstats_names);
-	free(xstats_names);
 	return ret;
 }
 
@@ -167,9 +171,15 @@  rte_metrics_tel_format_port(uint32_t pid, json_t *ports,
 	}
 
 	metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
+	if (metrics == NULL) {
+		METRICS_LOG_ERR("Cannot allocate memory");
+		return -ENOMEM;
+	}
+
 	names = malloc(sizeof(struct rte_metric_name) * num_metrics);
-	if (metrics == NULL || names == NULL) {
+	if (names == NULL) {
 		METRICS_LOG_ERR("Cannot allocate memory");
+		free(metrics);
 		return -ENOMEM;
 	}