From e988933ce92616a7e864c332c91472da76aef3ec Mon Sep 17 00:00:00 2001 From: Jeff Frontz Date: Fri, 22 Dec 2023 17:51:50 -0500 Subject: [PATCH] Centralize close() and set file descriptor values to -1 to prevent subsequent re-close --- src/core.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core.c b/src/core.c index 80f443e..7c1240d 100644 --- a/src/core.c +++ b/src/core.c @@ -462,8 +462,10 @@ static int start_chain(int *fd, proxy_data * pd, char *begin_mark) { error1: proxychains_write_log(TP " timeout\n"); error: - if(*fd != -1) + if(*fd != -1) { close(*fd); + *fd = -1; + } return SOCKET_ERROR; } @@ -537,7 +539,6 @@ static int chain_step(int ns, proxy_data * pfrom, proxy_data * pto) { if(!inet_ntop(v6?AF_INET6:AF_INET,pto->ip.addr.v6,ip_buf,sizeof ip_buf)) { pto->ps = DOWN_STATE; proxychains_write_log("<--ip conversion error!\n"); - close(ns); return SOCKET_ERROR; } hostname = ip_buf; @@ -552,12 +553,10 @@ static int chain_step(int ns, proxy_data * pfrom, proxy_data * pto) { case BLOCKED: pto->ps = BLOCKED_STATE; proxychains_write_log("<--denied\n"); - close(ns); break; case SOCKET_ERROR: pto->ps = DOWN_STATE; proxychains_write_log("<--socket error or timeout!\n"); - close(ns); break; } return retcode; @@ -581,6 +580,10 @@ int connect_proxy_chain(int sock, ip_type target_ip, PFUNC(); again: + if (ns != -1) { + close(ns); + ns = -1; + } rc = -1; DUMP_PROXY_CHAIN(pd, proxy_count); @@ -716,6 +719,7 @@ int connect_proxy_chain(int sock, ip_type target_ip, dup2(ns, sock); close(ns); return 0; + error: if(ns != -1) close(ns);