From 4914491fe4b5b75497144acbeaca3208e63d811b Mon Sep 17 00:00:00 2001 From: ruti <> Date: Tue, 27 Feb 2024 00:52:59 +0300 Subject: [PATCH] Fix double close, signal error check --- main.c | 2 +- proxy.c | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 8c849f2..eed373d 100644 --- a/main.c +++ b/main.c @@ -153,7 +153,7 @@ char *ftob(char *name, ssize_t *sl) } -int get_addr(char *str, struct sockaddr_ina *addr) +int get_addr(const char *str, struct sockaddr_ina *addr) { struct addrinfo hints = {0}, *res = 0; diff --git a/proxy.c b/proxy.c index 96f1535..af8951b 100644 --- a/proxy.c +++ b/proxy.c @@ -553,14 +553,21 @@ int big_loop(int srvfd) struct poolhd *pool = init_pool(params.max_open * 2 + 1); if (!pool) { perror("init pool"); + close(srvfd); + return -1; + } + if (!add_event(pool, EV_ACCEPT, srvfd, 0)) { + perror("add event"); + destroy_pool(pool); + close(srvfd); return -1; } char *buffer = malloc(params.bfsize); if (!buffer) { perror("malloc"); + destroy_pool(pool); return -1; } - add_event(pool, EV_ACCEPT, srvfd, 0); struct eval *val; int i = -1, etype; @@ -620,7 +627,7 @@ int big_loop(int srvfd) int listener(struct sockaddr_ina srv) { #ifdef SIGPIPE - if (signal(SIGPIPE, SIG_IGN)) + if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) uniperror("signal SIGPIPE!"); #endif signal(SIGINT, on_cancel); @@ -647,7 +654,5 @@ int listener(struct sockaddr_ina srv) close(srvfd); return -1; } - int status = big_loop(srvfd); - close(srvfd); - return status; + return big_loop(srvfd); }