[ofa-general] [PATCH] libibnetdisc: fix potential memory leak of port object

Ira Weiny weiny2 at llnl.gov
Fri Aug 7 09:07:03 PDT 2009


From: Ira Weiny <weiny2 at llnl.gov>
Date: Fri, 7 Aug 2009 09:05:44 -0700
Subject: [PATCH] libibnetdisc: fix potential memory leak of port object

	NOTE: This moves the port allocation below the port array allocation
	failure rather than free the port allocation after port array
	allocation fails.

Signed-off-by: Ira Weiny <weiny2 at llnl.gov>
---
 infiniband-diags/libibnetdisc/src/ibnetdisc.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/infiniband-diags/libibnetdisc/src/ibnetdisc.c b/infiniband-diags/libibnetdisc/src/ibnetdisc.c
index b9e89d9..27ae9f3 100644
--- a/infiniband-diags/libibnetdisc/src/ibnetdisc.c
+++ b/infiniband-diags/libibnetdisc/src/ibnetdisc.c
@@ -446,14 +446,6 @@ add_port_to_node(struct ibnd_fabric *fabric, struct ibnd_node *node, struct ibnd
 {
 	struct ibnd_port *port;
 
-	port = malloc(sizeof(*port));
-	if (!port)
-		return NULL;
-
-	memcpy(port, temp, sizeof(*port));
-	port->port.node = (ibnd_node_t *)node;
-	port->port.ext_portnum = 0;
-
 	if (node->node.ports == NULL) {
 		node->node.ports = calloc(sizeof(*node->node.ports), node->node.numports + 1);
 		if (!node->node.ports) {
@@ -462,6 +454,14 @@ add_port_to_node(struct ibnd_fabric *fabric, struct ibnd_node *node, struct ibnd
 		}
 	}
 
+	port = malloc(sizeof(*port));
+	if (!port)
+		return NULL;
+
+	memcpy(port, temp, sizeof(*port));
+	port->port.node = (ibnd_node_t *)node;
+	port->port.ext_portnum = 0;
+
 	node->node.ports[temp->port.portnum] = (ibnd_port_t *)port;
 
 	add_to_portguid_hash(port, fabric->portstbl);
-- 
1.5.4.5




More information about the general mailing list