This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fix file descriptor leak in cpplib


cpplib was changed back in April to keep searching for an include if
it encounters a directory.  For instance, if you have a file named
"usr" in your cwd, preprocessing

#include <usr>

with -I- -I/ -I. will find the file, not complain about being asked to
include a directory.

Unfortunately, the patch neglected to close the file descriptor for
the directory.

While testing a fix I discovered that we're not being sufficiently
careful about preserving // at the beginning of a pathname.

Bootstrapped i386-linux.

zw

	* cppfiles.c (open_file): If we've opened a directory by
	mistake, close it.
	(find_include_file): Avoid turning / into // or // into ///.

===================================================================
Index: cppfiles.c
--- cppfiles.c	2001/12/02 00:04:16	1.137
+++ cppfiles.c	2001/12/06 16:36:31
@@ -259,10 +259,13 @@ open_file (pfile, filename)
     {
       if (!S_ISDIR (file->st.st_mode))
 	return file;
+
       /* If it's a directory, we return null and continue the search
 	 as the file we're looking for may appear elsewhere in the
 	 search path.  */
       errno = ENOENT;
+      close (file->fd);
+      file->fd = -1;
     }
 
   file->err_no = errno;
@@ -556,9 +559,14 @@ find_include_file (pfile, header, type)
   name = (char *) alloca (strlen (fname) + pfile->max_include_len + 2);
   for (; path; path = path->next)
     {
-      memcpy (name, path->name, path->len);
-      name[path->len] = '/';
-      strcpy (&name[path->len + 1], fname);
+      int len = path->len;
+      memcpy (name, path->name, len);
+      /* Don't turn / into // or // into ///; // may be a namespace
+	 escape.  */
+      if (name[len-1] == '/')
+	len--;
+      name[len] = '/';
+      strcpy (&name[len + 1], fname);
       if (CPP_OPTION (pfile, remap))
 	n = remap_filename (pfile, name, path);
       else



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]