Skip to content

Commit

Permalink
socket: fix addrinfo leak
Browse files Browse the repository at this point in the history
  • Loading branch information
janweinstock committed Mar 6, 2025
1 parent d90b92a commit 654a776
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/mwr/utils/socket_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,18 @@ void socket::connect(const string& host, u16 port) {
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;

addrinfo* ai;
int err = getaddrinfo(host.c_str(), pstr.c_str(), &hint, &ai);
addrinfo *ai, *info;
int err = getaddrinfo(host.c_str(), pstr.c_str(), &hint, &info);
MWR_REPORT_ON(err, "getaddrinfo failed: %s", gai_strerror(err));
if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
MWR_ERROR("getaddrinfo: protocol family %d", ai->ai_family);
if (info->ai_family != AF_INET && info->ai_family != AF_INET6)
MWR_ERROR("getaddrinfo: protocol family %d", info->ai_family);

for (; ai != nullptr; ai = ai->ai_next) {
for (ai = info; ai != nullptr; ai = ai->ai_next) {
m_conn = ::socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (m_conn < 0)
if (m_conn < 0) {
freeaddrinfo(info);
MWR_REPORT("failed to create socket: %s", strerror(errno));
}

if (::connect(m_conn, ai->ai_addr, ai->ai_addrlen) < 0) {
close(m_conn);
Expand All @@ -293,7 +295,7 @@ void socket::connect(const string& host, u16 port) {
break;
}

freeaddrinfo(ai);
freeaddrinfo(info);
MWR_REPORT_ON(m_peer.empty(), "connect failed: %s", strerror(errno));
SET_SOCKOPT(m_conn, IPPROTO_TCP, TCP_NODELAY, 1);
}
Expand Down
16 changes: 9 additions & 7 deletions src/mwr/utils/socket_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,18 @@ void socket::connect(const string& host, u16 port) {
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;

addrinfo* ai;
int err = getaddrinfo(host.c_str(), pstr.c_str(), &hint, &ai);
addrinfo *ai, *info;
int err = getaddrinfo(host.c_str(), pstr.c_str(), &hint, &info);
MWR_REPORT_ON(err, "getaddrinfo failed: %s", gai_strerror(err));
if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
MWR_ERROR("getaddrinfo: protocol family %d", ai->ai_family);
if (info->ai_family != AF_INET && info->ai_family != AF_INET6)
MWR_ERROR("getaddrinfo: protocol family %d", info->ai_family);

for (; ai != nullptr; ai = ai->ai_next) {
for (ai = info; ai != nullptr; ai = ai->ai_next) {
m_conn = ::socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (m_conn < 0)
if (m_conn < 0) {
freeaddrinfo(info);
MWR_REPORT("failed to create socket: %s", socket_strerror());
}

if (::connect(m_conn, ai->ai_addr, (int)ai->ai_addrlen) < 0) {
closesocket(m_conn);
Expand All @@ -316,7 +318,7 @@ void socket::connect(const string& host, u16 port) {
break;
}

freeaddrinfo(ai);
freeaddrinfo(info);
MWR_REPORT_ON(m_peer.empty(), "connect failed: %s", socket_strerror());
SET_SOCKOPT(m_conn, IPPROTO_TCP, TCP_NODELAY, 1);
}
Expand Down

0 comments on commit 654a776

Please sign in to comment.