~singpolyma/biboumi

0620c8e589c713fa0f0d0f66a4c4d203e8f3f87f — Florent Le Coz 8 years ago 5027b21
Avoid leaking socket filedescriptors

When trying the various results of getaddrinfo, we forgot to close the
socket when one fails, before trying the next one.

Also use the destructor to make sure we do not have some other unrelated
leak.
2 files changed, 9 insertions(+), 1 deletions(-)

M louloulibs/network/tcp_socket_handler.cpp
M louloulibs/network/tcp_socket_handler.hpp
M louloulibs/network/tcp_socket_handler.cpp => louloulibs/network/tcp_socket_handler.cpp +8 -0
@@ 44,8 44,16 @@ TCPSocketHandler::TCPSocketHandler(std::shared_ptr<Poller> poller):
#endif
{}

TCPSocketHandler::~TCPSocketHandler()
{
  this->close();
}


void TCPSocketHandler::init_socket(const struct addrinfo* rp)
{
  if (this->socket != -1)
    ::close(this->socket);
  if ((this->socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
    throw std::runtime_error("Could not create socket: "s + strerror(errno));
  int optval = 1;

M louloulibs/network/tcp_socket_handler.hpp => louloulibs/network/tcp_socket_handler.hpp +1 -1
@@ 27,7 27,7 @@
class TCPSocketHandler: public SocketHandler
{
protected:
  ~TCPSocketHandler() = default;
  ~TCPSocketHandler();

public:
  explicit TCPSocketHandler(std::shared_ptr<Poller> poller);