From e3c95265a4f8fb0ca5588025799e9d6182998503 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 21 Oct 2015 22:27:15 +0900 Subject: [PATCH] nghttpd: Fix leak when server failed to listen to given port --- src/HttpServer.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/HttpServer.cc b/src/HttpServer.cc index f3d17d24..7b55fe61 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -1671,7 +1671,7 @@ int start_listen(HttpServer *sv, struct ev_loop *loop, Sessions *sessions, bool ok = false; const char *addr = nullptr; - auto acceptor = std::make_shared(sv, sessions, config); + std::shared_ptr acceptor; auto service = util::utos(config->port); addrinfo hints{}; @@ -1715,6 +1715,9 @@ int start_listen(HttpServer *sv, struct ev_loop *loop, Sessions *sessions, } #endif // IPV6_V6ONLY if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0 && listen(fd, 1000) == 0) { + if (!acceptor) { + acceptor = std::make_shared(sv, sessions, config); + } new ListenEventHandler(sessions, fd, acceptor); if (config->verbose) { @@ -1870,6 +1873,9 @@ int HttpServer::run() { Sessions sessions(this, loop, config_, ssl_ctx); if (start_listen(this, loop, &sessions, config_) != 0) { std::cerr << "Could not listen" << std::endl; + if (ssl_ctx) { + SSL_CTX_free(ssl_ctx); + } return -1; }