Skip to content

Commit 6dc445c

Browse files
author
daiyanlong
committed
ibdiag_sa: Fix memory leak in sa_query()
When realloc() fails on line 142 of infiniband-diags/ibdiag_sta. c, it will cause the loss of the umad's original memory reference, resulting in a leak of the original memory. The modified logic is to store the result in a temporary variable new_umad before calling realloc, which can avoid directly overwriting the original pointer umad and prevent losing the reference to the original memory when realloc fails. Signed-off-by: daiyanlong <[email protected]>
1 parent ca4e1c1 commit 6dc445c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

infiniband-diags/ibdiag_sa.c

Lines changed: 8 additions & 2 deletions
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)