[haiku-commits] haiku: hrev48686 - src/kits/network/libnetapi

  • From: pulkomandy@xxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Fri, 16 Jan 2015 15:03:10 +0100 (CET)

hrev48686 adds 1 changeset to branch 'master'
old head: 46c267cf18011590897c353df1cc2649aec3715a
new head: 8e13d9d6a6f4f2f5ba0bdb76f7290dbe27847f53
overview: 
http://cgit.haiku-os.org/haiku/log/?qt=range&q=8e13d9d6a6f4+%5E46c267cf1801

----------------------------------------------------------------------------

8e13d9d6a6f4: getifaddrs: std::nothrow and allocation checks.
  
  Thanks to Axel for reviewing.

                                 [ Adrien Destugues <pulkomandy@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev48686
Commit:      8e13d9d6a6f4f2f5ba0bdb76f7290dbe27847f53
URL:         http://cgit.haiku-os.org/haiku/commit/?id=8e13d9d6a6f4
Author:      Adrien Destugues <pulkomandy@xxxxxxxxx>
Date:        Fri Jan 16 13:59:13 2015 UTC

----------------------------------------------------------------------------

1 file changed, 20 insertions(+), 4 deletions(-)
src/kits/network/libnetapi/getifaddrs.cpp | 24 ++++++++++++++++++++----

----------------------------------------------------------------------------

diff --git a/src/kits/network/libnetapi/getifaddrs.cpp 
b/src/kits/network/libnetapi/getifaddrs.cpp
index e9431f1..575bdd5 100644
--- a/src/kits/network/libnetapi/getifaddrs.cpp
+++ b/src/kits/network/libnetapi/getifaddrs.cpp
@@ -33,13 +33,28 @@ int getifaddrs(struct ifaddrs **ifap)
 
        struct ifaddrs* previous = 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) {
                BNetworkInterfaceAddress address;
                int32 i = 0;
                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
                        current->ifa_next = previous;
@@ -55,7 +70,7 @@ int getifaddrs(struct ifaddrs **ifap)
                        current->ifa_dstaddr = new 
sockaddr(address.Destination().SockAddr());
                }
 
-               interface = new BNetworkInterface();
+               interface = new(std::nothrow) BNetworkInterface();
        }
 
        delete interface;
@@ -65,7 +80,8 @@ int getifaddrs(struct ifaddrs **ifap)
 }
 
 
-void freeifaddrs(struct ifaddrs *ifa)
+void
+freeifaddrs(struct ifaddrs *ifa)
 {
        struct ifaddrs* next;
        BNetworkInterface* interface = NULL;


Other related posts: