This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: name-finder fd leak fix
- From: Tom Tromey <tromey at redhat dot com>
- To: Java Patch List <java-patches at gcc dot gnu dot org>
- Cc: Andrew Haley <aph at redhat dot com>
- Date: 12 Mar 2002 18:14:06 -0700
- Subject: Patch: name-finder fd leak fix
- Reply-to: tromey at redhat dot com
I think it is possible for name-finder to leak file descriptors.
For instance, if the first pipe() succeeds but the second one fails,
this will happen. Or, if the fdopen fails (can this really happen?),
then we will leak an fd.
This patch fixes the problem. Andrew, what do you think?
Another way to fix it would be to add more close() calls inline in the
constructor.
Tom
Index: ChangeLog
from Tom Tromey <tromey@redhat.com>
* include/name-finder.h (_Jv_name_finder::myclose): New method.
* name-finder.cc (_Jv_name_finder): Initialize file descriptors.
Index: name-finder.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/name-finder.cc,v
retrieving revision 1.10
diff -u -r1.10 name-finder.cc
--- name-finder.cc 2002/02/18 02:52:43 1.10
+++ name-finder.cc 2002/03/13 00:40:26
@@ -1,6 +1,6 @@
// name-finder.cc - Convert addresses to names
-/* Copyright (C) 2000 Free Software Foundation, Inc
+/* Copyright (C) 2000, 2002 Free Software Foundation, Inc
This file is part of libgcj.
@@ -61,6 +61,13 @@
#if defined (HAVE_PIPE) && defined (HAVE_FORK) && defined (HAVE_EXECVP)
error = 0;
+ // Initialize file descriptors so that shutdown works properly.
+ f_pipe[0] = -1;
+ f_pipe[1] = -1;
+ b_pipe[0] = -1;
+ b_pipe[1] = -1;
+ b_pipe_fd = NULL;
+
char *argv[6];
{
int arg = 0;
@@ -93,8 +100,12 @@
_exit (127);
}
+ // Close child end of pipes. Set local descriptors to -1 so we
+ // don't try to close the fd again.
close (f_pipe [0]);
+ f_pipe[0] = -1;
close (b_pipe [1]);
+ b_pipe[1] = -1;
if (pid < 0)
{
@@ -104,6 +115,12 @@
b_pipe_fd = fdopen (b_pipe[0], "r");
error |= !b_pipe_fd;
+
+ if (! error)
+ {
+ // Don't try to close the fd twice.
+ b_pipe[0] = -1;
+ }
#endif
}
Index: include/name-finder.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/name-finder.h,v
retrieving revision 1.5
diff -u -r1.5 name-finder.h
--- include/name-finder.h 2002/02/07 19:25:28 1.5
+++ include/name-finder.h 2002/03/13 00:40:31
@@ -40,12 +40,19 @@
~_Jv_name_finder ()
{
#if defined (HAVE_PIPE) && defined (HAVE_FORK)
- close (f_pipe[1]);
- fclose (b_pipe_fd);
-
- int wstat;
- // We don't care about errors here.
- waitpid (pid, &wstat, 0);
+ myclose (f_pipe[0]);
+ myclose (f_pipe[1]);
+ myclose (b_pipe[0]);
+ myclose (b_pipe[1]);
+ if (b_pipe_fd != NULL)
+ fclose (b_pipe_fd);
+
+ if (pid >= 0)
+ {
+ int wstat;
+ // We don't care about errors here.
+ waitpid (pid, &wstat, 0);
+ }
#endif
}
@@ -72,5 +79,13 @@
int f_pipe[2], b_pipe[2];
FILE *b_pipe_fd;
int error;
+
+ // Close a descriptor only if it has not been closed.
+ void myclose (int fd)
+ {
+ if (fd != -1)
+ close (fd);
+ }
+
#endif
};