librte_metrics: fix memory leak
diff mbox series

Message ID 20200731034520.30791-1-gaurav1086@gmail.com
State Superseded, archived
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/travis-robot success Travis build: passed
ci/Intel-compilation success Compilation OK
ci/iol-intel-Performance success Performance Testing PASS
ci/checkpatch success coding style OK

Commit Message

Gaurav Singh July 31, 2020, 3:45 a.m. UTC
Fix memory leak for sequential allocations.

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

Comments

Honnappa Nagarahalli July 31, 2020, 4:54 a.m. UTC | #1
<snip>

Hi Gaurav,
	One comment inline.

> 
> Fix memory leak for sequential allocations.
> 
> Signed-off-by: Gaurav Singh <gaurav1086@gmail.com>
> ---
>  lib/librte_metrics/rte_metrics_telemetry.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/librte_metrics/rte_metrics_telemetry.c
> b/lib/librte_metrics/rte_metrics_telemetry.c
> index 289ebae0b..55c2b8478 100644
> --- a/lib/librte_metrics/rte_metrics_telemetry.c
> +++ b/lib/librte_metrics/rte_metrics_telemetry.c
> @@ -167,9 +167,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;
>  	}
There is a similar error in function 'rte_metrics_tel_reg_port_ethdev_to_metrics', can you fix that as well?

> 
> --
> 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..55c2b8478 100644
--- a/lib/librte_metrics/rte_metrics_telemetry.c
+++ b/lib/librte_metrics/rte_metrics_telemetry.c
@@ -167,9 +167,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;
 	}