Skip to content

Commit

Permalink
Fix memory leak in libproc lookup code
Browse files Browse the repository at this point in the history
  • Loading branch information
droe committed Nov 10, 2014
1 parent f886736 commit d9d8674
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions nat.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@

#ifdef HAVE_DARWIN_LIBPROC
#include <libproc.h>
#endif
#endif /* HAVE_DARWIN_LIBPROC */


/*
* Access NAT state tables in a NAT engine independant way.
Expand Down Expand Up @@ -134,7 +135,7 @@ nat_pf_lookup_proc(pid_t *result, struct sockaddr *dst_addr, UNUSED socklen_t *d
int pid_count = proc_listallpids(NULL, 0);
pids = malloc(sizeof(pid_t) * pid_count);
if (!pids) {
goto done;
goto out1;
}

pid_count = proc_listallpids(pids, sizeof(pid_t) * pid_count);
Expand All @@ -149,9 +150,12 @@ nat_pf_lookup_proc(pid_t *result, struct sockaddr *dst_addr, UNUSED socklen_t *d
continue;
}

if (fds) {
free(fds);
}
fds = malloc(PROC_PIDLISTFD_SIZE * fd_count);
if (!fds) {
goto done;
goto out2;
}
fd_count = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fds, sizeof(fds[0]) * fd_count);

Expand Down Expand Up @@ -198,13 +202,14 @@ nat_pf_lookup_proc(pid_t *result, struct sockaddr *dst_addr, UNUSED socklen_t *d
/* valid match */
*result = pid;
ret = 0;
goto done;
break;
}
}

done:
free(pids);
free(fds);
out2:
free(pids);
out1:
return ret;
}

Expand Down

0 comments on commit d9d8674

Please sign in to comment.