nghttpd: Fix leak when server failed to listen to given port

This commit is contained in:
Tatsuhiro Tsujikawa 2015-10-21 22:27:15 +09:00
parent 46d7c30c17
commit e3c95265a4
1 changed files with 7 additions and 1 deletions

View File

@ -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<AcceptHandler>(sv, sessions, config);
std::shared_ptr<AcceptHandler> 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<AcceptHandler>(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;
}