Skip to content

Commit 1085e46

Browse files
committed
socket/win32: Use calloc where applicable in getifaddrs implementation
This should prevent crashes like the one mentioned in #12 which are caused by releasing an invalid pointer (due to uninitialized memory).
1 parent 69f98e3 commit 1085e46

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/socket.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ static int getifaddrs(struct ifaddrs** ifap)
669669
}
670670

671671
if (!ifa) {
672-
ifa = malloc(sizeof(struct ifaddrs));
672+
ifa = calloc(1, sizeof(struct ifaddrs));
673673
if (!ifa) {
674674
errno = ENOMEM;
675675
free(pAddresses);
@@ -678,7 +678,7 @@ static int getifaddrs(struct ifaddrs** ifap)
678678
*ifap = ifa;
679679
ifa->ifa_next = NULL;
680680
} else {
681-
struct ifaddrs* ifanew = malloc(sizeof(struct ifaddrs));
681+
struct ifaddrs* ifanew = calloc(1, sizeof(struct ifaddrs));
682682
if (!ifanew) {
683683
freeifaddrs(*ifap);
684684
free(pAddresses);
@@ -708,8 +708,7 @@ static int getifaddrs(struct ifaddrs** ifap)
708708
memcpy(ifa->ifa_addr, unicast->Address.lpSockaddr, unicast->Address.iSockaddrLength);
709709

710710
/* netmask */
711-
ifa->ifa_netmask = (struct sockaddr*)malloc(sizeof(struct sockaddr_storage));
712-
memset(ifa->ifa_netmask, 0, sizeof(struct sockaddr_storage));
711+
ifa->ifa_netmask = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_storage));
713712

714713
/* store mac address */
715714
if (adapter->PhysicalAddressLength == 6) {

0 commit comments

Comments
 (0)