From c3cf25012053338c0d4fd8fce4e8d94f141603b3 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Thu, 4 Aug 2016 22:59:12 +0200 Subject: lib: ipcp.c: Fix memleak upon cancellation --- src/lib/ipcp.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 373baafd..76098691 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -34,9 +34,15 @@ #include #include #include +#include #include #include +static void close_ptr(void * o) +{ + close(*((int *) o)); +} + static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, ipcp_msg_t * msg) { @@ -56,24 +62,26 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, return NULL; } + free(sock_path); + buf.len = ipcp_msg__get_packed_size(msg); if (buf.len == 0) { close(sockfd); - free(sock_path); return NULL; } buf.data = malloc(IPCP_MSG_BUF_SIZE); if (buf.data == NULL) { close(sockfd); - free(sock_path); return NULL; } + pthread_cleanup_push(close_ptr, (void *) &sockfd); + pthread_cleanup_push((void (*)(void *)) free, (void *) buf.data); + ipcp_msg__pack(msg, buf.data); if (write(sockfd, buf.data, buf.len) == -1) { - free(sock_path); free(buf.data); close(sockfd); return NULL; @@ -81,7 +89,6 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, count = read(sockfd, buf.data, IPCP_MSG_BUF_SIZE); if (count <= 0) { - free(sock_path); free(buf.data); close(sockfd); return NULL; @@ -89,15 +96,14 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, recv_msg = ipcp_msg__unpack(NULL, count, buf.data); if (recv_msg == NULL) { - free(sock_path); free(buf.data); close(sockfd); return NULL; } - free(buf.data); - free(sock_path); - close(sockfd); + pthread_cleanup_pop(true); + pthread_cleanup_pop(true); + return recv_msg; } -- cgit v1.2.3