From ed48f43a016ce68bc58dcfcf2b7944aae27c5991 Mon Sep 17 00:00:00 2001 From: Sergey Markelov Date: Tue, 31 Mar 2026 14:42:31 +0700 Subject: [PATCH 1/2] fix: socket close in `fetch_get()`, possible leak --- fetch_posix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fetch_posix.c b/fetch_posix.c index 95584ba..c766e7e 100644 --- a/fetch_posix.c +++ b/fetch_posix.c @@ -37,7 +37,7 @@ char *fetch_get(const char *url, int32_t *error) { struct addrinfo hints = {0}; struct addrinfo *address_info = NULL; - SOCKET sfd = 0; + SOCKET sfd = -1; char *body = NULL; char *host = NULL; int32_t err = 0; @@ -182,7 +182,7 @@ char *fetch_get(const char *url, int32_t *error) { download_cleanup: if (address_info) freeaddrinfo(address_info); - if (sfd) + if (sfd != -1) closesocket(sfd); free(host); From 2940b23992744416f078613148064987576000f4 Mon Sep 17 00:00:00 2001 From: Sergey Markelov Date: Tue, 31 Mar 2026 14:10:36 +0700 Subject: [PATCH 2/2] improve: prepare fetch_posix.c for platforms that miss getaddrinfo() This reduces amount of #if blocks. --- fetch_posix.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fetch_posix.c b/fetch_posix.c index c766e7e..10a002d 100644 --- a/fetch_posix.c +++ b/fetch_posix.c @@ -35,8 +35,13 @@ // Fetch proxy auto configuration using HTTP only char *fetch_get(const char *url, int32_t *error) { + const int32_t socktype = SOCK_STREAM; + const int32_t protocol = IPPROTO_TCP; + struct addrinfo hints = {0}; struct addrinfo *address_info = NULL; + struct sockaddr *addr = NULL; + socklen_t addrlen = 0; SOCKET sfd = -1; char *body = NULL; char *host = NULL; @@ -72,8 +77,8 @@ char *fetch_get(const char *url, int32_t *error) { // Attempt to resolve the host name hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; + hints.ai_socktype = socktype; + hints.ai_protocol = protocol; err = getaddrinfo(host, port, &hints, &address_info); if (err != 0) { @@ -82,8 +87,11 @@ char *fetch_get(const char *url, int32_t *error) { goto download_cleanup; } + addr = address_info->ai_addr; + addrlen = address_info->ai_addrlen; + // Create communication socket - sfd = socket(address_info->ai_family, address_info->ai_socktype, address_info->ai_protocol); + sfd = socket(addr->sa_family, socktype, protocol); if ((int)sfd == -1) { err = socketerr; log_error("Unable to create socket (%" PRId32 ")", err); @@ -91,7 +99,7 @@ char *fetch_get(const char *url, int32_t *error) { } // Connect to remote address - err = connect(sfd, address_info->ai_addr, (int)address_info->ai_addrlen); + err = connect(sfd, addr, addrlen); if (err != 0) { err = socketerr; log_debug("Unable to connect to host %s (%" PRId32 ")", host, err);