Permalink
Browse files

fix: avoid memory leakage in case realloc() fails

[nelhage@nelhage.com: Change error handling slightly]
  • Loading branch information...
1 parent d51bfc2 commit f904ad7a4cd0df5598ac8b99c4f3b036cf7a976f @pozdnychev pozdnychev committed with Feb 21, 2011
Showing with 10 additions and 3 deletions.
  1. +10 −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,15 @@ 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 = realloc(fds, allocated * sizeof *tmp);
+ if (tmp == NULL) {
+ child->error = errno;
+ error("Unable to allocate memory for fd array.");
+ free(fds);
+ fds = NULL;
+ 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 f904ad7

Please sign in to comment.