The getaddrinfo() call in bind_socket() dynamically allocates memory
for the result linked list that must be freed with freeaddrinfo().
However, none of the code paths after a successful getaddrinfo() call
free this memory, causing a leak in every invocation of bind_socket().
Fixes: c35ecb95c448 ("selftests/net: Add test for timing a bind request to a
port with a populated bhash entry")
Signed-off-by: longlong yan <[email protected]>
---
tools/testing/selftests/net/bind_bhash.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/net/bind_bhash.c
b/tools/testing/selftests/net/bind_bhash.c
index da04b0b19b73..2bd100777448 100644
--- a/tools/testing/selftests/net/bind_bhash.c
+++ b/tools/testing/selftests/net/bind_bhash.c
@@ -52,18 +52,19 @@ static int bind_socket(int opt, const char *addr)
err = setsockopt(sock_fd, SOL_SOCKET, opt, &reuse,
sizeof(reuse));
if (err) {
perror("setsockopt failed");
- goto cleanup;
+ goto err_free_info;
}
}
err = bind(sock_fd, res->ai_addr, res->ai_addrlen);
if (err) {
perror("failed to bind to port");
- goto cleanup;
+ goto err_free_info;
}
-
+ freeaddrinfo(res);
return sock_fd;
-
+err_free_info:
+ freeaddrinfo(res);
cleanup:
close(sock_fd);
return err;
--
2.43.0