diff mbox series

iproute2: prevent memory leak

Message ID 20231114092410.43635-1-heminhong@kylinos.cn (mailing list archive)
State Superseded
Headers show
Series iproute2: prevent memory leak | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Minhong He Nov. 14, 2023, 9:24 a.m. UTC
The 'rtnl_talk' allocated memory for 'answer',
in the exception handling branch, memory should be free,
otherwise it will cause memory leak.

Signed-off-by: heminhong <heminhong@kylinos.cn>
---
 ip/link_gre.c    | 4 ++++
 ip/link_gre6.c   | 4 ++++
 ip/link_ip6tnl.c | 4 ++++
 ip/link_iptnl.c  | 4 ++++
 ip/link_vti.c    | 4 ++++
 ip/link_vti6.c   | 4 ++++
 6 files changed, 24 insertions(+)

Comments

Stephen Hemminger Nov. 15, 2023, 12:36 a.m. UTC | #1
On Tue, 14 Nov 2023 17:24:10 +0800
heminhong <heminhong@kylinos.cn> wrote:

> +			if (NULL != answer)
> +			{
> +				free(answer);
> +			}

Four style problems here:
	1. Don't use Windows convention of NULL first
	2. Don't use Windows style bracket indentation
	3. Brackets are not used in kernel style for single statment if()
	4. Since free of NULL is a perfectly valid nop. Just call free() and skip the if.

If you read the existing code, you would see that it uses kernel style.
Any change should follow the conventions of existing code.
diff mbox series

Patch

diff --git a/ip/link_gre.c b/ip/link_gre.c
index 74a5b5e9..b1c49ace 100644
--- a/ip/link_gre.c
+++ b/ip/link_gre.c
@@ -111,6 +111,10 @@  static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
 
 		if (rtnl_talk(&rth, &req.n, &answer) < 0) {
 get_failed:
+			if (NULL != answer)
+			{
+				free(answer);
+			}
 			fprintf(stderr,
 				"Failed to get existing tunnel info.\n");
 			return -1;
diff --git a/ip/link_gre6.c b/ip/link_gre6.c
index b03bd65a..64302d63 100644
--- a/ip/link_gre6.c
+++ b/ip/link_gre6.c
@@ -113,6 +113,10 @@  static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
 
 		if (rtnl_talk(&rth, &req.n, &answer) < 0) {
 get_failed:
+			if (NULL != answer)
+			{
+				free(answer);
+			}
 			fprintf(stderr,
 				"Failed to get existing tunnel info.\n");
 			return -1;
diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c
index b27d696f..16ed6e0d 100644
--- a/ip/link_ip6tnl.c
+++ b/ip/link_ip6tnl.c
@@ -99,6 +99,10 @@  static int ip6tunnel_parse_opt(struct link_util *lu, int argc, char **argv,
 
 		if (rtnl_talk(&rth, &req.n, &answer) < 0) {
 get_failed:
+			if (NULL != answer)
+			{
+				free(answer);
+			}
 			fprintf(stderr,
 				"Failed to get existing tunnel info.\n");
 			return -1;
diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c
index 1315aebe..27326382 100644
--- a/ip/link_iptnl.c
+++ b/ip/link_iptnl.c
@@ -103,6 +103,10 @@  static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
 
 		if (rtnl_talk(&rth, &req.n, &answer) < 0) {
 get_failed:
+			if (NULL != answer)
+			{
+				free(answer);
+			}
 			fprintf(stderr,
 				"Failed to get existing tunnel info.\n");
 			return -1;
diff --git a/ip/link_vti.c b/ip/link_vti.c
index 50943254..92d5c5ad 100644
--- a/ip/link_vti.c
+++ b/ip/link_vti.c
@@ -67,6 +67,10 @@  static int vti_parse_opt(struct link_util *lu, int argc, char **argv,
 
 		if (rtnl_talk(&rth, &req.n, &answer) < 0) {
 get_failed:
+			if (NULL != answer)
+			{
+				free(answer);
+			}
 			fprintf(stderr,
 				"Failed to get existing tunnel info.\n");
 			return -1;
diff --git a/ip/link_vti6.c b/ip/link_vti6.c
index 5764221e..b50158fe 100644
--- a/ip/link_vti6.c
+++ b/ip/link_vti6.c
@@ -69,6 +69,10 @@  static int vti6_parse_opt(struct link_util *lu, int argc, char **argv,
 
 		if (rtnl_talk(&rth, &req.n, &answer) < 0) {
 get_failed:
+			if (NULL != answer)
+			{
+				free(answer);
+			}
 			fprintf(stderr,
 				"Failed to get existing tunnel info.\n");
 			return -1;