Skip to content

Commit

Permalink
fix(windows): fix crash in getIpAddressSync (#631)
Browse files Browse the repository at this point in the history
Even if not properly documented, some properties of
`windows.networking.connectivity.connectionprofile` can throw.
It's the case of `NetworkAdapter` since it's then calling
`check_hresult` which will throw if the returned hresult is an error one.

Since `RNCNetInfo::getIpAddressSync` is marked as `noexcept`, any exception
thrown in this method will call `std::terminate`.

This commit is to return `"unknown"` in this case.
  • Loading branch information
aironefr committed Oct 15, 2022
1 parent b793b99 commit cc3ed0f
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions windows/RNCNetInfoCPP/RNCNetInfo.cpp
Expand Up @@ -107,24 +107,30 @@ namespace winrt::ReactNativeNetInfo::implementation {

std::string getIpAddressSync() noexcept
{
auto icp = Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile();
if (!icp || !icp.NetworkAdapter())
{
return "unknown";
} else
{
auto hostnames = Windows::Networking::Connectivity::NetworkInformation::GetHostNames();
for (auto const& hostname : hostnames)
try {
auto icp = Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile();
if (!icp || !icp.NetworkAdapter())
{
return "unknown";
}
else
{
if (
hostname.Type() == Windows::Networking::HostNameType::Ipv4 &&
hostname.IPInformation() &&
hostname.IPInformation().NetworkAdapter() &&
hostname.IPInformation().NetworkAdapter().NetworkAdapterId() == icp.NetworkAdapter().NetworkAdapterId())
auto hostnames = Windows::Networking::Connectivity::NetworkInformation::GetHostNames();
for (auto const& hostname : hostnames)
{
return winrt::to_string(hostname.CanonicalName());
if (
hostname.Type() == Windows::Networking::HostNameType::Ipv4 &&
hostname.IPInformation() &&
hostname.IPInformation().NetworkAdapter() &&
hostname.IPInformation().NetworkAdapter().NetworkAdapterId() == icp.NetworkAdapter().NetworkAdapterId())
{
return winrt::to_string(hostname.CanonicalName());
}
}
return "unknown";
}
}
catch (...) {
return "unknown";
}
}
Expand Down

0 comments on commit cc3ed0f

Please sign in to comment.