Permalink
Browse files

fix: avoid memory leakage in case realloc() fails

  • Loading branch information...
1 parent d51bfc2 commit a9771df8558c858feaee4317086325386a42f6d2 @pozdnychev pozdnychev committed Feb 21, 2011
Showing with 7 additions and 3 deletions.
  1. +7 −3 attach.c
View
@@ -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);
@@ -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);

0 comments on commit a9771df

Please sign in to comment.