getifaddrs: std::nothrow and allocation checks.

Thanks to Axel for reviewing.
This commit is contained in:
Adrien Destugues
2015-01-16 15:03:44 +01:00
parent 46c267cf18
commit 8e13d9d6a6
+20 -4
View File
@@ -33,13 +33,28 @@ int getifaddrs(struct ifaddrs **ifap)
struct ifaddrs* previous = NULL; struct ifaddrs* previous = NULL;
struct ifaddrs* current = NULL; struct ifaddrs* current = NULL;
BNetworkInterface* interface = new BNetworkInterface(); BNetworkInterface* interface = new(std::nothrow) BNetworkInterface();
if (interface == NULL) {
errno = B_NO_MEMORY;
return -1;
}
while (roster.GetNextInterface(&cookie, *interface) == B_OK) { while (roster.GetNextInterface(&cookie, *interface) == B_OK) {
BNetworkInterfaceAddress address; BNetworkInterfaceAddress address;
int32 i = 0; int32 i = 0;
while (interface->GetAddressAt(i++, address) == B_OK) { while (interface->GetAddressAt(i++, address) == B_OK) {
current = new ifaddrs(); if (interface == NULL) {
freeifaddrs(previous);
errno = B_NO_MEMORY;
return -1;
}
current = new(std::nothrow) ifaddrs();
if (current == NULL) {
freeifaddrs(previous);
errno = B_NO_MEMORY;
return -1;
}
// Chain this interface with the next one // Chain this interface with the next one
current->ifa_next = previous; current->ifa_next = previous;
@@ -55,7 +70,7 @@ int getifaddrs(struct ifaddrs **ifap)
current->ifa_dstaddr = new sockaddr(address.Destination().SockAddr()); current->ifa_dstaddr = new sockaddr(address.Destination().SockAddr());
} }
interface = new BNetworkInterface(); interface = new(std::nothrow) BNetworkInterface();
} }
delete interface; delete interface;
@@ -65,7 +80,8 @@ int getifaddrs(struct ifaddrs **ifap)
} }
void freeifaddrs(struct ifaddrs *ifa) void
freeifaddrs(struct ifaddrs *ifa)
{ {
struct ifaddrs* next; struct ifaddrs* next;
BNetworkInterface* interface = NULL; BNetworkInterface* interface = NULL;