Skip to contentSkip to conversation

libipvs: Fix memory leak

PATCH Applied Ryan O'Hara 3 participants 3 messages Aug 18, 2013 – Aug 20, 2013 IPVS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libipvs: Fix memory leak

From: Henrique Mecking <[email protected]>

If malloc fails, free allocated memory before returning NULL.

---
 libipvs/libipvs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
libipvs/libipvs.c +6 -2
index 2b066d2..1c9e066 100644
@@ -668,8 +668,10 @@ struct ip_vs_get_services *ipvs_get_services(void)
 		return NULL;
 	len = sizeof(*getk) +
 		sizeof(struct ip_vs_service_entry_kern) * ipvs_info.num_services;
-	if (!(getk = malloc(len)))
+	if (!(getk = malloc(len))) {
+		free(get);
 		return NULL;
+	}
 
 	ipvs_func = ipvs_get_services;
 	getk->num_services = ipvs_info.num_services;
@@ -853,8 +855,10 @@ ipvs_nl_dest_failure:
 	}
 
 	len = sizeof(*dk) + sizeof(struct ip_vs_dest_entry_kern) * svc->num_dests;
-	if (!(dk = malloc(len)))
+	if (!(dk = malloc(len))) {
+		free(d);
 		return NULL;
+	}
 
 	dk->fwmark = svc->fwmark;
 	dk->protocol = svc->protocol;
Signature · 1 line
1.8.1.4
maintainer Acked-by on v1 1/1
#1 lore raw

Hello,

Quoted 5 lines · Ryan O'Hara

From: Henrique Mecking <[email protected]>

If malloc fails, free allocated memory before returning NULL.

Looks good to me.

Quoted 34 lines
---
 libipvs/libipvs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
libipvs/libipvs.c +6 -2
index 2b066d2..1c9e066 100644
@@ -668,8 +668,10 @@ struct ip_vs_get_services *ipvs_get_services(void)
 		return NULL;
 	len = sizeof(*getk) +
 		sizeof(struct ip_vs_service_entry_kern) * ipvs_info.num_services;
-	if (!(getk = malloc(len)))
+	if (!(getk = malloc(len))) {
+		free(get);
 		return NULL;
+	}
 
 	ipvs_func = ipvs_get_services;
 	getk->num_services = ipvs_info.num_services;
@@ -853,8 +855,10 @@ ipvs_nl_dest_failure:
 	}
 
 	len = sizeof(*dk) + sizeof(struct ip_vs_dest_entry_kern) * svc->num_dests;
-	if (!(dk = malloc(len)))
+	if (!(dk = malloc(len))) {
+		free(d);
 		return NULL;
+	}
 
 	dk->fwmark = svc->fwmark;
 	dk->protocol = svc->protocol;
Signature · 1 line
1.8.1.4

Regards

--
Julian Anastasov <[email protected]>