examples/ipsec-secgw: fix memory leaks

Message ID 20211109095140.3888643-1-radu.nicolau@intel.com (mailing list archive)
State Accepted, archived
Delegated to: akhil goyal
Headers
Series examples/ipsec-secgw: fix memory leaks |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS

Commit Message

Radu Nicolau Nov. 9, 2021, 9:51 a.m. UTC
  Free telemetry structures when memory allocation
or input parameter errors occur.
Fixes: 3e7b7dd88021 ("examples/ipsec-secgw: support telemetry")

Signed-off-by: Radu Nicolau <radu.nicolau@intel.com>
---
 examples/ipsec-secgw/ipsec-secgw.c | 65 ++++++++++++++++++++++--------
 1 file changed, 49 insertions(+), 16 deletions(-)
  

Comments

Akhil Goyal Nov. 11, 2021, 11:21 a.m. UTC | #1
> Free telemetry structures when memory allocation
> or input parameter errors occur.
> Fixes: 3e7b7dd88021 ("examples/ipsec-secgw: support telemetry")
> 
> Signed-off-by: Radu Nicolau <radu.nicolau@intel.com>
> ---
Acked-by: Akhil Goyal <gakhil@marvell.com>

Applied to dpdk-next-crypto

Thanks.
  

Patch

diff --git a/examples/ipsec-secgw/ipsec-secgw.c b/examples/ipsec-secgw/ipsec-secgw.c
index 0a1c5bcaaa..564d7981e5 100644
--- a/examples/ipsec-secgw/ipsec-secgw.c
+++ b/examples/ipsec-secgw/ipsec-secgw.c
@@ -3025,12 +3025,14 @@  handle_telemetry_cmd_ipsec_secgw_stats_outbound(const char *cmd __rte_unused,
 	struct rte_tel_data *spd4_data = rte_tel_data_alloc();
 	struct rte_tel_data *spd6_data = rte_tel_data_alloc();
 	struct rte_tel_data *sad_data = rte_tel_data_alloc();
-
 	unsigned int coreid = UINT32_MAX;
+	int rc = 0;
 
 	/* verify allocated telemetry data structures */
-	if (!spd4_data || !spd6_data || !sad_data)
-		return -ENOMEM;
+	if (!spd4_data || !spd6_data || !sad_data) {
+		rc = -ENOMEM;
+		goto exit;
+	}
 
 	/* initialize telemetry data structs as dicts */
 	rte_tel_data_start_dict(data);
@@ -3041,8 +3043,10 @@  handle_telemetry_cmd_ipsec_secgw_stats_outbound(const char *cmd __rte_unused,
 
 	if (params) {
 		coreid = (uint32_t)atoi(params);
-		if (rte_lcore_is_enabled(coreid) == 0)
-			return -EINVAL;
+		if (rte_lcore_is_enabled(coreid) == 0) {
+			rc = -EINVAL;
+			goto exit;
+		}
 	}
 
 	update_statistics(&total_stats, coreid);
@@ -3076,7 +3080,13 @@  handle_telemetry_cmd_ipsec_secgw_stats_outbound(const char *cmd __rte_unused,
 
 	rte_tel_data_add_dict_container(data, "sad", sad_data, 0);
 
-	return 0;
+exit:
+	if (rc) {
+		rte_tel_data_free(spd4_data);
+		rte_tel_data_free(spd6_data);
+		rte_tel_data_free(sad_data);
+	}
+	return rc;
 }
 
 static int
@@ -3088,12 +3098,14 @@  handle_telemetry_cmd_ipsec_secgw_stats_inbound(const char *cmd __rte_unused,
 	struct rte_tel_data *spd4_data = rte_tel_data_alloc();
 	struct rte_tel_data *spd6_data = rte_tel_data_alloc();
 	struct rte_tel_data *sad_data = rte_tel_data_alloc();
-
 	unsigned int coreid = UINT32_MAX;
+	int rc = 0;
 
 	/* verify allocated telemetry data structures */
-	if (!spd4_data || !spd6_data || !sad_data)
-		return -ENOMEM;
+	if (!spd4_data || !spd6_data || !sad_data) {
+		rc = -ENOMEM;
+		goto exit;
+	}
 
 	/* initialize telemetry data structs as dicts */
 	rte_tel_data_start_dict(data);
@@ -3105,8 +3117,10 @@  handle_telemetry_cmd_ipsec_secgw_stats_inbound(const char *cmd __rte_unused,
 
 	if (params) {
 		coreid = (uint32_t)atoi(params);
-		if (rte_lcore_is_enabled(coreid) == 0)
-			return -EINVAL;
+		if (rte_lcore_is_enabled(coreid) == 0) {
+			rc = -EINVAL;
+			goto exit;
+		}
 	}
 
 	update_statistics(&total_stats, coreid);
@@ -3140,7 +3154,13 @@  handle_telemetry_cmd_ipsec_secgw_stats_inbound(const char *cmd __rte_unused,
 
 	rte_tel_data_add_dict_container(data, "spd6", spd6_data, 0);
 
-	return 0;
+exit:
+	if (rc) {
+		rte_tel_data_free(spd4_data);
+		rte_tel_data_free(spd6_data);
+		rte_tel_data_free(sad_data);
+	}
+	return rc;
 }
 
 static int
@@ -3151,8 +3171,14 @@  handle_telemetry_cmd_ipsec_secgw_stats_routing(const char *cmd __rte_unused,
 
 	struct rte_tel_data *lpm4_data = rte_tel_data_alloc();
 	struct rte_tel_data *lpm6_data = rte_tel_data_alloc();
-
 	unsigned int coreid = UINT32_MAX;
+	int rc = 0;
+
+	/* verify allocated telemetry data structures */
+	if (!lpm4_data || !lpm6_data) {
+		rc = -ENOMEM;
+		goto exit;
+	}
 
 	/* initialize telemetry data structs as dicts */
 	rte_tel_data_start_dict(data);
@@ -3162,8 +3188,10 @@  handle_telemetry_cmd_ipsec_secgw_stats_routing(const char *cmd __rte_unused,
 
 	if (params) {
 		coreid = (uint32_t)atoi(params);
-		if (rte_lcore_is_enabled(coreid) == 0)
-			return -EINVAL;
+		if (rte_lcore_is_enabled(coreid) == 0) {
+			rc = -EINVAL;
+			goto exit;
+		}
 	}
 
 	update_statistics(&total_stats, coreid);
@@ -3180,7 +3208,12 @@  handle_telemetry_cmd_ipsec_secgw_stats_routing(const char *cmd __rte_unused,
 
 	rte_tel_data_add_dict_container(data, "IPv6 LPM", lpm6_data, 0);
 
-	return 0;
+exit:
+	if (rc) {
+		rte_tel_data_free(lpm4_data);
+		rte_tel_data_free(lpm6_data);
+	}
+	return rc;
 }
 
 static void