Skip to content

Commit b837a74

Browse files
authored
Merge pull request #1602 from YanLongDai/ibdiag_sa_realloc
ibdiag_sa: Fix memory leak in sa_query()
2 parents 0866d2a + 6dc445c commit b837a74

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

infiniband-diags/ibdiag_sa.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ int sa_query(struct sa_handle * h, uint8_t method,
104104
struct sa_query_result *result)
105105
{
106106
ib_rpc_t rpc;
107-
void *umad, *mad;
107+
void *umad, *mad, *new_umad;
108108
int ret, offset, len = 256;
109109

110110
memset(&rpc, 0, sizeof(rpc));
@@ -139,7 +139,13 @@ int sa_query(struct sa_handle * h, uint8_t method,
139139
ret = umad_recv(h->fd, umad, &len, ibd_timeout);
140140
if (ret < 0) {
141141
if (errno == ENOSPC) {
142-
umad = realloc(umad, umad_size() + len);
142+
new_umad = realloc(umad, umad_size() + len);
143+
if (!new_umad) {
144+
IBWARN("Failed to reallocate memory for umad: %s\n", strerror(errno));
145+
free(umad);
146+
return (-ret);
147+
}
148+
umad = new_umad;
143149
goto recv_mad;
144150
}
145151
IBWARN("umad_recv failed: attr 0x%x: %s\n", attr,

0 commit comments

Comments
 (0)