Skip to content

Commit

Permalink
socket/win32: Use calloc where applicable in getifaddrs implementation
Browse files Browse the repository at this point in the history
This should prevent crashes like the one mentioned in #12 which are caused
by releasing an invalid pointer (due to uninitialized memory).
  • Loading branch information
nikias committed Oct 21, 2024
1 parent 69f98e3 commit 1085e46
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1085e46

Please sign in to comment.