refactor: Construct std::string outside critical section
There's no need to hold the mutex while constructing a `std::string` from the `char` string. Also use `inet_ntop` instead of the potentially thread-unsafe `inet_ntoa` that might use a static buffer for the result. This makes the `INet` case match the `INet6` case below.
This commit is contained in:
committed by
Nick Bolton
parent
b788d63044
commit
f98f8b10d1
@ -590,10 +590,13 @@ std::string ArchNetworkBSD::addrToString(ArchNetAddress addr)
|
||||
|
||||
switch (getAddrFamily(addr)) {
|
||||
case INet: {
|
||||
char strAddr[INET_ADDRSTRLEN];
|
||||
const auto *ipAddr = TYPED_ADDR(struct sockaddr_in, addr);
|
||||
std::scoped_lock lock{m_mutex};
|
||||
std::string s = inet_ntoa(ipAddr->sin_addr);
|
||||
return s;
|
||||
{
|
||||
std::scoped_lock lock{m_mutex};
|
||||
inet_ntop(AF_INET, &ipAddr->sin_addr, strAddr, INET_ADDRSTRLEN);
|
||||
}
|
||||
return strAddr;
|
||||
}
|
||||
|
||||
case INet6: {
|
||||
|
||||
Reference in New Issue
Block a user