summary refs log tree commit diff
diff options
context:
space:
mode:
authorelx <me@ellcs.de>2022-02-06 15:43:09 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-02-07 14:52:57 +0100
commit22c4b19416a86f9218d1832bc2519cfb97c1535f (patch)
tree9258a154ff15164c89018149cce5e7ffaf27d008
parentbdc22f5a1e5d2eae014319daa07e8efcc61da27e (diff)
downloadhittpd-22c4b19416a86f9218d1832bc2519cfb97c1535f.tar.gz
hittpd-22c4b19416a86f9218d1832bc2519cfb97c1535f.tar.xz
hittpd-22c4b19416a86f9218d1832bc2519cfb97c1535f.zip
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
-rw-r--r--hittpd.c7
1 files changed, 6 insertions, 1 deletions
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)