summaryrefslogtreecommitdiff
path: root/src/lib/ipcp.c
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@intec.ugent.be>2016-08-04 22:59:12 +0200
committerdimitri staessens <dimitri.staessens@intec.ugent.be>2016-08-04 23:12:23 +0200
commitc3cf25012053338c0d4fd8fce4e8d94f141603b3 (patch)
tree7a11e2f2332fcd891e94859ad739324c43304664 /src/lib/ipcp.c
parent00761652fb55e16227508457da4bda82b19ef193 (diff)
downloadouroboros-c3cf25012053338c0d4fd8fce4e8d94f141603b3.tar.gz
ouroboros-c3cf25012053338c0d4fd8fce4e8d94f141603b3.zip
lib: ipcp.c: Fix memleak upon cancellation
Diffstat (limited to 'src/lib/ipcp.c')
-rw-r--r--src/lib/ipcp.c22
1 files changed, 14 insertions, 8 deletions
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 <string.h>
#include <signal.h>
#include <stdbool.h>
+#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
+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;
}