This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[commit] Fix file descriptor leaks in ser-pipe.c
- From: dje at google dot com (Doug Evans)
- To: gdb-patches at sourceware dot org
- Date: Mon, 19 Apr 2010 23:08:38 -0700 (PDT)
- Subject: [commit] Fix file descriptor leaks in ser-pipe.c
Hi.
While testing a gdbserver that can be started via
"target remote | ssh gdbserver" I found these problems.
Checked in.
2010-04-19 Doug Evans <dje@google.com>
* ser-base.c (generic_readchar): Watch for EOF in read of error_fd.
* ser-pipe.c (pipe_open): Fix file descriptor leaks.
(pipe_close): Ditto.
Index: ser-base.c
===================================================================
RCS file: /cvs/src/src/gdb/ser-base.c,v
retrieving revision 1.15
diff -u -p -r1.15 ser-base.c
--- ser-base.c 1 Jan 2010 07:31:41 -0000 1.15
+++ ser-base.c 20 Apr 2010 06:05:25 -0000
@@ -363,6 +363,13 @@ generic_readchar (struct serial *scb, in
s = read (scb->error_fd, &buf, to_read);
if (s == -1)
break;
+ if (s == 0)
+ {
+ /* EOF */
+ close (scb->error_fd);
+ scb->error_fd = -1;
+ break;
+ }
/* In theory, embedded newlines are not a problem.
But for MI, we want each output line to have just
Index: ser-pipe.c
===================================================================
RCS file: /cvs/src/src/gdb/ser-pipe.c,v
retrieving revision 1.23
diff -u -p -r1.23 ser-pipe.c
--- ser-pipe.c 1 Jan 2010 07:31:41 -0000 1.23
+++ ser-pipe.c 20 Apr 2010 05:49:35 -0000
@@ -66,7 +66,11 @@ pipe_open (struct serial *scb, const cha
if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
return -1;
if (socketpair (AF_UNIX, SOCK_STREAM, 0, err_pdes) < 0)
- return -1;
+ {
+ close (pdes[0]);
+ close (pdes[1]);
+ return -1;
+ }
/* Create the child process to run the command in. Note that the
apparent call to vfork() below *might* actually be a call to
@@ -123,6 +127,8 @@ pipe_open (struct serial *scb, const cha
/* Parent. */
close (pdes[1]);
+ if (err_pdes[1] != -1)
+ close (err_pdes[1]);
/* :end chunk */
state = XMALLOC (struct pipe_state);
state->pid = pid;
@@ -145,10 +151,15 @@ pipe_close (struct serial *scb)
int pid = state->pid;
close (scb->fd);
scb->fd = -1;
+ if (scb->error_fd != -1)
+ close (scb->error_fd);
+ scb->error_fd = -1;
xfree (state);
scb->state = NULL;
kill (pid, SIGTERM);
- /* Might be useful to check that the child does die. */
+ /* Might be useful to check that the child does die,
+ and while we're waiting for it to die print any remaining
+ stderr output. */
}
}