Skip to content

Commit

Permalink
Check fcntl() return value
Browse files Browse the repository at this point in the history
  • Loading branch information
ysyrota authored and AaronAtDuo committed Oct 22, 2024
1 parent d7db1c2 commit 13769e0
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/https.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 13769e0

Please sign in to comment.