Skip to content

Commit cf34fb8

Browse files
committed
Addressing PR feedback: update wslcsdk.cpp
1 parent 04a4541 commit cf34fb8

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

src/windows/WslcSDK/wslcsdk.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -560,29 +560,39 @@ try
560560
// TODO: Consider using standard protocol numbers instead of our own enum.
561561
convertedPort.Protocol = internalPort.protocol == WSLC_PORT_PROTOCOL_TCP ? IPPROTO_TCP : IPPROTO_UDP;
562562

563+
// Validate IP address if provided and if valid, copy to runtime structure.
563564
if (internalPort.windowsAddress != nullptr)
564565
{
565566
char addrBuf[INET6_ADDRSTRLEN]{};
566-
if (internalPort.windowsAddress->ss_family == AF_INET)
567+
switch (internalPort.windowsAddress->ss_family)
567568
{
568-
const auto* addr4 = reinterpret_cast<const sockaddr_in*>(internalPort.windowsAddress);
569-
THROW_HR_IF_NULL(E_UNEXPECTED, inet_ntop(AF_INET, &addr4->sin_addr, addrBuf, sizeof(addrBuf)));
570-
convertedPort.Family = AF_INET;
571-
}
572-
else
573-
{
574-
const auto* addr6 = reinterpret_cast<const sockaddr_in6*>(internalPort.windowsAddress);
575-
THROW_HR_IF_NULL(E_UNEXPECTED, inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, sizeof(addrBuf)));
576-
convertedPort.Family = AF_INET6;
569+
case AF_INET:
570+
{
571+
const auto* addr4 = reinterpret_cast<const sockaddr_in*>(internalPort.windowsAddress);
572+
573+
THROW_HR_IF_NULL(E_UNEXPECTED, inet_ntop(AF_INET, &addr4->sin_addr, addrBuf, sizeof(addrBuf)));
574+
575+
convertedPort.Family = AF_INET;
576+
break;
577+
}
578+
579+
case AF_INET6:
580+
{
581+
const auto* addr6 = reinterpret_cast<const sockaddr_in6*>(internalPort.windowsAddress);
582+
583+
THROW_HR_IF_NULL(E_UNEXPECTED, inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, sizeof(addrBuf)));
584+
585+
convertedPort.Family = AF_INET6;
586+
break;
587+
}
588+
589+
default:
590+
// Reject unsupported or malformed address families
591+
THROW_HR(E_INVALIDARG);
577592
}
578593
bindingAddressStrings[i] = addrBuf;
579594
convertedPort.BindingAddress = bindingAddressStrings[i].c_str();
580595
}
581-
else
582-
{
583-
convertedPort.Family = AF_INET;
584-
convertedPort.BindingAddress = "127.0.0.1";
585-
}
586596
}
587597
containerOptions.Ports = convertedPorts.get();
588598
containerOptions.PortsCount = static_cast<ULONG>(internalContainerSettings->portsCount);

0 commit comments

Comments
 (0)