Commit 71d62d39 by Russell Belfer

Fix memory leak in p_getaddrinfo on Amiga

If gethostbyname() fails on platforms with NO_ADDRINFO, the code
leaks the struct addrinfo that was allocated.  This fixes that
(and a number of code formatting issues in that area of code in
src/posix.c).
parent a7ed7460
...@@ -13,25 +13,28 @@ ...@@ -13,25 +13,28 @@
#ifndef GIT_WIN32 #ifndef GIT_WIN32
#ifdef NO_ADDRINFO #ifdef NO_ADDRINFO
int p_getaddrinfo( int p_getaddrinfo(
const char *host, const char *host,
const char *port, const char *port,
struct addrinfo *hints, struct addrinfo *hints,
struct addrinfo **info) struct addrinfo **info)
{ {
GIT_UNUSED(hints);
struct addrinfo *ainfo, *ai; struct addrinfo *ainfo, *ai;
int p = 0; int p = 0;
GIT_UNUSED(hints);
if ((ainfo = malloc(sizeof(struct addrinfo))) == NULL) if ((ainfo = malloc(sizeof(struct addrinfo))) == NULL)
return -1; return -1;
if ((ainfo->ai_hostent = gethostbyname(host)) == NULL) if ((ainfo->ai_hostent = gethostbyname(host)) == NULL) {
free(ainfo);
return -2; return -2;
}
ainfo->ai_servent = getservbyname(port, 0); ainfo->ai_servent = getservbyname(port, 0);
if (ainfo->ai_servent) if (ainfo->ai_servent)
ainfo->ai_port = ainfo->ai_servent->s_port; ainfo->ai_port = ainfo->ai_servent->s_port;
else else
...@@ -50,14 +53,14 @@ int p_getaddrinfo( ...@@ -50,14 +53,14 @@ int p_getaddrinfo(
ainfo->ai_addrlen = sizeof(struct sockaddr_in); ainfo->ai_addrlen = sizeof(struct sockaddr_in);
*info = ainfo; *info = ainfo;
if (ainfo->ai_hostent->h_addr_list[1] == NULL) { if (ainfo->ai_hostent->h_addr_list[1] == NULL) {
ainfo->ai_next = NULL; ainfo->ai_next = NULL;
return 0; return 0;
} }
ai = ainfo; ai = ainfo;
for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) { for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) {
ai->ai_next = malloc(sizeof(struct addrinfo)); ai->ai_next = malloc(sizeof(struct addrinfo));
memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo)); memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo));
...@@ -67,7 +70,7 @@ int p_getaddrinfo( ...@@ -67,7 +70,7 @@ int p_getaddrinfo(
ai->ai_next->ai_addr = (struct addrinfo *)&ai->ai_next->ai_addr_in; ai->ai_next->ai_addr = (struct addrinfo *)&ai->ai_next->ai_addr_in;
ai = ai->ai_next; ai = ai->ai_next;
} }
ai->ai_next = NULL; ai->ai_next = NULL;
return 0; return 0;
} }
...@@ -75,9 +78,9 @@ int p_getaddrinfo( ...@@ -75,9 +78,9 @@ int p_getaddrinfo(
void p_freeaddrinfo(struct addrinfo *info) void p_freeaddrinfo(struct addrinfo *info)
{ {
struct addrinfo *p, *next; struct addrinfo *p, *next;
p = info; p = info;
while(p != NULL) { while(p != NULL) {
next = p->ai_next; next = p->ai_next;
free(p); free(p);
...@@ -88,27 +91,19 @@ void p_freeaddrinfo(struct addrinfo *info) ...@@ -88,27 +91,19 @@ void p_freeaddrinfo(struct addrinfo *info)
const char *p_gai_strerror(int ret) const char *p_gai_strerror(int ret)
{ {
switch(ret) { switch(ret) {
case -1: case -1: return "Out of memory"; break;
return "Out of memory"; case -2: return "Address lookup failed"; break;
break; default: return "Unknown error"; break;
case -2:
return "Address lookup failed";
break;
default:
return "Unknown error";
break;
} }
} }
#endif /* NO_ADDRINFO */ #endif /* NO_ADDRINFO */
int p_open(const char *path, int flags, ...) int p_open(const char *path, int flags, ...)
{ {
mode_t mode = 0; mode_t mode = 0;
if (flags & O_CREAT) if (flags & O_CREAT) {
{
va_list arg_list; va_list arg_list;
va_start(arg_list, flags); va_start(arg_list, flags);
...@@ -159,6 +154,7 @@ int p_rename(const char *from, const char *to) ...@@ -159,6 +154,7 @@ int p_rename(const char *from, const char *to)
int p_read(git_file fd, void *buf, size_t cnt) int p_read(git_file fd, void *buf, size_t cnt)
{ {
char *b = buf; char *b = buf;
while (cnt) { while (cnt) {
ssize_t r; ssize_t r;
#ifdef GIT_WIN32 #ifdef GIT_WIN32
...@@ -183,6 +179,7 @@ int p_read(git_file fd, void *buf, size_t cnt) ...@@ -183,6 +179,7 @@ int p_read(git_file fd, void *buf, size_t cnt)
int p_write(git_file fd, const void *buf, size_t cnt) int p_write(git_file fd, const void *buf, size_t cnt)
{ {
const char *b = buf; const char *b = buf;
while (cnt) { while (cnt) {
ssize_t r; ssize_t r;
#ifdef GIT_WIN32 #ifdef GIT_WIN32
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment