From a56045dd8047f9b147e63be67c41a4423023437f Mon Sep 17 00:00:00 2001 From: Yu-Chen Lin Date: Fri, 31 May 2019 22:15:31 +0800 Subject: [PATCH] Prevent socket leak on error Signed-off-by: Yu-Chen Lin Signed-off-by: Romain Vimont --- app/src/net.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/net.c b/app/src/net.c index b5b227c2..1b487f7f 100644 --- a/app/src/net.c +++ b/app/src/net.c @@ -33,6 +33,7 @@ net_connect(uint32_t addr, uint16_t port) { if (connect(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) { perror("connect"); + close(sock); return INVALID_SOCKET; } @@ -60,11 +61,13 @@ net_listen(uint32_t addr, uint16_t port, int backlog) { if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) { perror("bind"); + close(sock); return INVALID_SOCKET; } if (listen(sock, backlog) == SOCKET_ERROR) { perror("listen"); + close(sock); return INVALID_SOCKET; }