From 47b46d6907539d501ab4f45f3fba9f87896a83ec Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Fri, 9 Feb 2018 21:57:00 +0600 Subject: [PATCH] coap_io.c: Fix file descriptor leaks when "connections" fail If a TCP connection connection fails, or a an ICMP Unreachable is received for a UDP packet, then the file descriptor is not closed off. Eventually all fails when no more file descriptors are allowed. --- src/coap_io.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/coap_io.c b/src/coap_io.c index 9af6d98e..8ff3cb5a 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -315,7 +315,7 @@ coap_socket_connect_tcp1(coap_socket_t *sock, return 1; } coap_log(LOG_WARNING, "coap_socket_connect_tcp1: connect: %s\n", coap_socket_strerror()); - return 0; + goto error; } if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) { @@ -850,9 +850,10 @@ coap_network_read(coap_socket_t *sock, coap_packet_t *packet) { #else if (errno == ECONNREFUSED) { #endif - /* client-side ICMP destination unreachable, ignore it */ - coap_log(LOG_WARNING, "coap_network_read: unreachable\n"); - return -2; + /* client-side ICMP destination unreachable, ignore it */ + coap_log(LOG_WARNING, "coap_network_read: unreachable\n"); + coap_socket_close(sock); + return -2; } coap_log(LOG_WARNING, "coap_network_read: %s\n", coap_socket_strerror()); goto error;