From 13769e06c29a99477f94d8ee2f020ff9b795f859 Mon Sep 17 00:00:00 2001 From: Yuriy Syrota Date: Sun, 20 Oct 2024 16:07:05 +0300 Subject: [PATCH] Check fcntl() return value --- lib/https.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/https.c b/lib/https.c index c31eae5..d200d01 100644 --- a/lib/https.c +++ b/lib/https.c @@ -314,23 +314,29 @@ _establish_connection(struct https_request * const req, if (connected_socket == -1) { continue; } - sock_flags = fcntl(connected_socket, F_GETFL, 0); - fcntl(connected_socket, F_SETFL, sock_flags|O_NONBLOCK); + if ((sock_flags = fcntl(connected_socket, F_GETFL, 0)) == -1) { + goto fail; + } - if (connect(connected_socket, cur_res->ai_addr, cur_res->ai_addrlen) != 0 && - errno != EINPROGRESS) { - close(connected_socket); - connected_socket = -1; - continue; + if (fcntl(connected_socket, F_SETFL, sock_flags|O_NONBLOCK) == -1) { + goto fail; + } + + if (connect(connected_socket, cur_res->ai_addr, cur_res->ai_addrlen) != 0 + && errno != EINPROGRESS) { + goto fail; } + socket_error = _fd_wait(connected_socket, 10000); if (socket_error != 1) { - close(connected_socket); - connected_socket = -1; - continue; + goto fail; } + /* Connected! */ break; + fail: + close(connected_socket); + connected_socket = -1; } cur_res = NULL; freeaddrinfo(res);