Patch: name-finder fd leak fix
Tom Tromey
tromey@redhat.com
Wed Mar 13 02:47:00 GMT 2002
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
};
More information about the Java-patches
mailing list