From 4ca72a0c670082f087529257f620614c7d94c1c9 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Thu, 4 Aug 2016 21:20:07 +0200 Subject: lib: sockets: Fix cancellation cleanup When cancelled in read() the fd should be closed and the allocated memory freed. --- src/lib/sockets.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/lib/sockets.c') diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 9e13b687..5d861cdf 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -33,6 +33,8 @@ #include #include #include +#include +#include int client_socket_open(char * file_name) { @@ -99,6 +101,11 @@ int server_socket_open(char * file_name) return sockfd; } +void close_ptr(void * o) +{ + close(*(int *) o); +} + irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { int sockfd; @@ -122,6 +129,9 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) return NULL; } + pthread_cleanup_push(close_ptr, &sockfd); + pthread_cleanup_push((void (*)(void *)) free, (void *) buf.data); + irm_msg__pack(msg, buf.data); if (write(sockfd, buf.data, buf.len) == -1) { @@ -144,8 +154,9 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) return NULL; } - free(buf.data); - close(sockfd); + pthread_cleanup_pop(true); + pthread_cleanup_pop(true); + return recv_msg; } -- cgit v1.2.3