fix strncpy() warnings and ensure C strings are 0-terminated

This commit is contained in:
arvidn 2020-02-15 11:08:04 +01:00 committed by Arvid Norberg
parent 224cc0ec6d
commit 14f1266199
2 changed files with 11 additions and 9 deletions

View File

@ -412,7 +412,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
return false;
}
std::strncpy(rv.name, ifa->ifa_name, sizeof(rv.name));
std::strncpy(rv.name, ifa->ifa_name, sizeof(rv.name) - 1);
rv.name[sizeof(rv.name) - 1] = '\0';
// determine address
@ -620,7 +620,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
{
ip_interface iface;
iface.interface_address = sockaddr_to_address(&item.ifr_addr);
std::strncpy(iface.name, item.ifr_name, sizeof(iface.name));
std::strncpy(iface.name, item.ifr_name, sizeof(iface.name) - 1);
iface.name[sizeof(iface.name) - 1] = '\0';
ifreq req = {};
@ -685,7 +685,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
adapter != 0; adapter = adapter->Next)
{
ip_interface r;
std::strncpy(r.name, adapter->AdapterName, sizeof(r.name));
std::strncpy(r.name, adapter->AdapterName, sizeof(r.name) - 1);
r.name[sizeof(r.name) - 1] = '\0';
wcstombs(r.friendly_name, adapter->FriendlyName, sizeof(r.friendly_name));
r.friendly_name[sizeof(r.friendly_name) - 1] = '\0';
@ -1045,7 +1045,8 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
r.destination = address::from_string(adapter->IpAddressList.IpAddress.String, ec);
r.gateway = address::from_string(adapter->GatewayList.IpAddress.String, ec);
r.netmask = address::from_string(adapter->IpAddressList.IpMask.String, ec);
strncpy(r.name, adapter->AdapterName, sizeof(r.name));
strncpy(r.name, adapter->AdapterName, sizeof(r.name) - 1);
r.name[sizeof(r.name) - 1] = '\0';
if (ec)
{
@ -1109,8 +1110,8 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
{
name += wcslen(L"\\DEVICE\\TCPIP_");
}
wcstombs(r.name, name, sizeof(r.name));
r.name[sizeof(r.name) - 1] = 0;
wcstombs(r.name, name, sizeof(r.name) - 1);
r.name[sizeof(r.name) - 1] = '\0';
r.mtu = ifentry.dwMtu;
ret.push_back(r);
}
@ -1158,8 +1159,8 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
ifentry.dwIndex = routes->table[i].dwForwardIfIndex;
if (GetIfEntry(&ifentry) == NO_ERROR)
{
wcstombs(r.name, ifentry.wszName, sizeof(r.name));
r.name[sizeof(r.name) - 1] = 0;
wcstombs(r.name, ifentry.wszName, sizeof(r.name) - 1);
r.name[sizeof(r.name) - 1] = '\0';
r.mtu = ifentry.dwMtu;
ret.push_back(r);
}

View File

@ -5505,7 +5505,8 @@ namespace aux {
ip_interface ip;
ip.interface_address = s.local_endpoint.address();
ip.netmask = s.netmask;
std::strncpy(ip.name, s.device.c_str(), sizeof(ip.name));
std::strncpy(ip.name, s.device.c_str(), sizeof(ip.name) - 1);
ip.name[sizeof(ip.name) - 1] = '\0';
s.natpmp_mapper->start(ip);
}
}