Skip to content

Commit

Permalink
fix: avoid memory leakage in case realloc() fails
Browse files Browse the repository at this point in the history
  • Loading branch information
pozdnychev committed Feb 21, 2011
1 parent d51bfc2 commit a9771df
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions attach.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int *get_child_tty_fds(struct ptrace_child *child, int *count) {
DIR *dir;
ssize_t len;
struct dirent *d;
int *tmp = NULL;

debug("Looking up fds for tty in child.");
snprintf(buf, sizeof buf, "/proc/%d/fd/0", child->pid);
Expand All @@ -83,9 +84,12 @@ int *get_child_tty_fds(struct ptrace_child *child, int *count) {
|| strcmp(buf, "/dev/tty") == 0) {
if (n == allocated) {
allocated = allocated ? 2 * allocated : 2;
fds = realloc(fds, sizeof(int) * allocated);
if (!fds)
goto out;
tmp = relloc(fds, allocated * sizeof *tmp);
if (! tmp) {
perror("realloc");
goto out;
}
fds = tmp;
}
debug("Found an alias for the tty: %s", d->d_name);
fds[n++] = atoi(d->d_name);
Expand Down

0 comments on commit a9771df

Please sign in to comment.