From 22c4b19416a86f9218d1832bc2519cfb97c1535f Mon Sep 17 00:00:00 2001 From: elx Date: Sun, 6 Feb 2022 15:43:09 +0100 Subject: Close clients when max clients exceeded After compiling with `-fsanitize=address`, you can see that hittpd would crash with the following commands: ulimit -n 4096; ./hittpd for i in {1..1024}; do (echo ""; sleep 5) > /dev/tcp/127.0.0.1/80 & done --- hittpd.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hittpd.c b/hittpd.c index 3438102..0a243b4 100644 --- a/hittpd.c +++ b/hittpd.c @@ -1164,8 +1164,13 @@ main(int argc, char *argv[]) accept_client(i, connfd); break; } - if (i == MAX_CLIENTS) + if (i == MAX_CLIENTS) { printf("too many clients\n"); + int connfd = accept(listenfd, 0, 0); + if (connfd >= 0) + close(connfd); + continue; + } if (i > maxi) maxi = i; /* max index in client[] array */ if (--nready <= 0) -- cgit 1.4.1