Bail out early when no memory is available.
diff --git a/src/posix.c b/src/posix.c
index 8d86aa8..de9181a 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -62,7 +62,10 @@ int p_getaddrinfo(
ai = ainfo;
for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) {
- ai->ai_next = malloc(sizeof(struct addrinfo));
+ if (!(ai->ai_next = malloc(sizeof(struct addrinfo)))) {
+ p_freeaddrinfo(ainfo);
+ return -1;
+ }
memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo));
memcpy(&ai->ai_next->ai_addr_in.sin_addr,
ainfo->ai_hostent->h_addr_list[p],