From 47ca34e915aa0299a672660af67a50e99a526959 Mon Sep 17 00:00:00 2001 From: Kamal Heib Date: Fri, 22 Sep 2023 20:24:41 -0400 Subject: [PATCH] ibtracert: Fix memory leak [ Upstream commit 9ad625b1d405a95bfd1c425c3f17a9dde9614415 ] Avoid memory leak in find_mcpath() by freeing the allocated memory. Fixes: e4d681de5167 ("Diags: Renamed openib-diags to infiniband-diags") Signed-off-by: Kamal Heib Signed-off-by: Nicolas Morey --- infiniband-diags/ibtracert.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/infiniband-diags/ibtracert.c b/infiniband-diags/ibtracert.c index 09e6a54fd..0ebbe4390 100644 --- a/infiniband-diags/ibtracert.c +++ b/infiniband-diags/ibtracert.c @@ -542,11 +542,15 @@ static Node *find_mcpath(ib_portid_t * from, int mlid) DEBUG("from %s", portid2str(from)); - if (!(node = calloc(1, sizeof(Node)))) + node = calloc(1, sizeof(Node)); + if (!node) IBEXIT("out of memory"); - if (!(port = calloc(1, sizeof(Port)))) + port = calloc(1, sizeof(Port)); + if (!port) { + free(node); IBEXIT("out of memory"); + } if (get_node(node, port, from) < 0) { IBWARN("can't reach node %s", portid2str(from)); @@ -564,8 +568,11 @@ static Node *find_mcpath(ib_portid_t * from, int mlid) return NULL; /* ibtracert from host to itself is unsupported */ } - if (switch_mclookup(node, from, mlid, map) < 0 || !map[0]) + if (switch_mclookup(node, from, mlid, map) < 0 || !map[0]) { + free(node); + free(port); return NULL; + } return node; } @@ -612,7 +619,8 @@ static Node *find_mcpath(ib_portid_t * from, int mlid) if (from->drpath.cnt > 0) path->drpath.cnt--; } else { - if (!(port = calloc(1, sizeof(Port)))) + port = calloc(1, sizeof(Port)); + if (!port) IBEXIT("out of memory"); if (get_port(port, i, path) < 0) { @@ -637,11 +645,18 @@ static Node *find_mcpath(ib_portid_t * from, int mlid) } } - if (!(remotenode = calloc(1, sizeof(Node)))) + remotenode = calloc(1, sizeof(Node)); + if (!remotenode) { + free(port); IBEXIT("out of memory"); + } - if (!(remoteport = calloc(1, sizeof(Port)))) + remoteport = calloc(1, sizeof(Port)); + if (!remoteport) { + free(port); + free(remotenode); IBEXIT("out of memory"); + } if (get_node(remotenode, remoteport, path) < 0) { IBWARN @@ -670,6 +685,9 @@ static Node *find_mcpath(ib_portid_t * from, int mlid) remotenode, remoteport); path->drpath.cnt--; /* restore path */ + free(port); + free(remotenode); + free(remoteport); } } }