metrics: fix memory leak on allocation failure

If an error occurred when allocating memory for metrics or names,
the function returned without freeing allocated memory. This is now
fixed to avoid the resource leak in the case that either metrics or
names had been successfully allocated memory.

Coverity issue: 362053
Fixes: c5b7197f66 ("telemetry: move some functions to metrics library")
Cc: stable@dpdk.org

Reported-by: Gaurav Singh <gaurav1086@gmail.com>
Signed-off-by: Ciara Power <ciara.power@intel.com>
Acked-by: John McNamara <john.mcnamara@intel.com>
This commit is contained in:
Ciara Power 2020-09-17 16:03:41 +01:00 committed by David Marchand
parent fc61d9a89b
commit a5c79ad0f0

View File

@ -170,7 +170,8 @@ rte_metrics_tel_format_port(uint32_t pid, json_t *ports,
names = malloc(sizeof(struct rte_metric_name) * num_metrics);
if (metrics == NULL || names == NULL) {
METRICS_LOG_ERR("Cannot allocate memory");
return -ENOMEM;
ret = -ENOMEM;
goto fail;
}
if (rte_metrics_get_names(names, num_metrics) != num_metrics ||