Skip to content

Commit

Permalink
ibtracert: Fix memory leak
Browse files Browse the repository at this point in the history
[ Upstream commit 9ad625b ]

Avoid memory leak in find_mcpath() by freeing the allocated memory.

Fixes: e4d681d ("Diags: Renamed openib-diags to infiniband-diags")
Signed-off-by: Kamal Heib <kheib@redhat.com>
Signed-off-by: Nicolas Morey <nmorey@suse.com>
  • Loading branch information
Kamalheib authored and nmorey committed Oct 4, 2023
1 parent 5eda618 commit 47ca34e
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions infiniband-diags/ibtracert.c
Expand Up @@ -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));
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 47ca34e

Please sign in to comment.