From 1085e461a58b61936ab9ffd05e33c69fd072b739 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 21 Oct 2024 12:27:40 +0200 Subject: [PATCH] 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). --- src/socket.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/socket.c b/src/socket.c index 6e65c42..100562c 100644 --- a/src/socket.c +++ b/src/socket.c @@ -669,7 +669,7 @@ static int getifaddrs(struct ifaddrs** ifap) } if (!ifa) { - ifa = malloc(sizeof(struct ifaddrs)); + ifa = calloc(1, sizeof(struct ifaddrs)); if (!ifa) { errno = ENOMEM; free(pAddresses); @@ -678,7 +678,7 @@ static int getifaddrs(struct ifaddrs** ifap) *ifap = ifa; ifa->ifa_next = NULL; } else { - struct ifaddrs* ifanew = malloc(sizeof(struct ifaddrs)); + struct ifaddrs* ifanew = calloc(1, sizeof(struct ifaddrs)); if (!ifanew) { freeifaddrs(*ifap); free(pAddresses); @@ -708,8 +708,7 @@ static int getifaddrs(struct ifaddrs** ifap) memcpy(ifa->ifa_addr, unicast->Address.lpSockaddr, unicast->Address.iSockaddrLength); /* netmask */ - ifa->ifa_netmask = (struct sockaddr*)malloc(sizeof(struct sockaddr_storage)); - memset(ifa->ifa_netmask, 0, sizeof(struct sockaddr_storage)); + ifa->ifa_netmask = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_storage)); /* store mac address */ if (adapter->PhysicalAddressLength == 6) {