[cpp patch]: Stop leaking file descriptors

Nathan Sidwell nathan@codesourcery.com
Thu Nov 2 04:51:00 GMT 2000


Hi,
cpp will leak file descriptors when reincluding an idempotent header file.
What happens is that open_file (cppfiles.c) will reopen the header,
but stack_unclude_file will simply ignore it, forgetting to close the
descriptor. With something like gtk, which reincludes a lot of
such headers, you run out of file descriptors!

the patch stops open_file re-opening a DO_NOT_REREAD header -- was
there a reason it should really be re-opened? And makes sure that
stack_include_file releases the file descriptor, if there was one.

the alternative is that when open_file gets a EMFILE, it should walk
the #include tree closing any DO_NOT_REREAD descriptors.

built & tested on sparc-sun-solaris2.7. ok?

nathan
-- 
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
2000-11-02  Nathan Sidwell  <nathan@codesourcery.com>

	* cppfiles.c (open_file): If DO_NOT_REREAD, then don't reopen.
	(stack_include_file): If DO_NOT_REREAD, close it if open.

Index: cppfiles.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cppfiles.c,v
retrieving revision 1.84
diff -c -3 -p -r1.84 cppfiles.c
*** cppfiles.c	2000/10/30 22:28:59	1.84
--- cppfiles.c	2000/11/02 12:42:28
*************** open_file (pfile, filename)
*** 148,153 ****
--- 148,155 ----
        /* -1 indicates a file we've opened previously, and since closed.  */
        if (file->fd != -1)
  	return file;
+       if (DO_NOT_REREAD (file))
+         return file;
      }
    else
      {
*************** stack_include_file (pfile, inc)
*** 210,217 ****
      cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
  
    if (DO_NOT_REREAD (inc))
!     return 0;
! 
    if (inc->buffer == NULL)
      if (read_include_file (pfile, inc) == 0)
        return 0;
--- 212,225 ----
      cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
  
    if (DO_NOT_REREAD (inc))
!     {
!       if (inc->fd > 0)
!         {
!           close (inc->fd);
!           inc->fd = -1;
!         }
!       return 0;
!     }
    if (inc->buffer == NULL)
      if (read_include_file (pfile, inc) == 0)
        return 0;


More information about the Gcc-patches mailing list