public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* strip out ".." from gcc search paths
@ 2002-07-16 20:08 Alan Modra
  2002-07-17 11:15 ` Richard Henderson
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Alan Modra @ 2002-07-16 20:08 UTC (permalink / raw)
  To: gcc-patches

How many times have you run a new version of gcc from its build dir,
only to find you've picked up the wrong assembler?  This sort of
problem is due to gcc search paths being like:

/usr/local/lib/gcc-lib/powerpc64-linux/3.1.1/../../../../powerpc64-linux/bin/

which is fine after gcc has been installed.  Prior to installation,
the 3.1.1 dir might not exist, leading to gcc not being able to find
the correct assembler etc.

This patch also fixes a bug in the UPDATE_PATH_HOST_CANONICALIZE
invocation.

	* prefix.c (update_path): Strip ".." components when prior dir
	doesn't exist.  Pass correct var to UPDATE_PATH_HOST_CANONICALIZE.

Index: gcc/prefix.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/prefix.c,v
retrieving revision 1.34
diff -u -p -r1.34 prefix.c
--- gcc/prefix.c	20 May 2002 18:06:54 -0000	1.34
+++ gcc/prefix.c	17 Jul 2002 02:27:14 -0000
@@ -251,7 +251,7 @@ update_path (path, key)
   const char *path;
   const char *key;
 {
-  char *result;
+  char *result, *p;
 
   if (! strncmp (path, std_prefix, strlen (std_prefix)) && key != 0)
     {
@@ -271,9 +271,67 @@ update_path (path, key)
   else
     result = xstrdup (path);
 
+#ifndef ALWAYS_STRIP_DOTDOT
+#define ALWAYS_STRIP_DOTDOT 0
+#endif
+
+  p = result;
+  while (1)
+    {
+      char *src, *dest;
+
+      p = strchr (p, '.');
+      if (p == NULL)
+	break;
+      /* Get rid of a leading `./' and replace `/./' with `/'.  */
+      if (p[1] == '/' && (p == result || p[-1] == '/'))
+	{
+	  src = p + 2;
+	  /* Be careful about .//foo  */
+	  while (*src == '/')
+	    ++src;
+	  dest = p;
+	  while ((*dest++ = *src++) != 0)
+	    ;
+	}
+      /* Look for `/../'  */
+      else if (p[1] == '.' && p[2] == '/' && (p != result && p[-1] == '/'))
+	{
+	  *p = 0;
+	  if (!ALWAYS_STRIP_DOTDOT && access (result, X_OK) == 0)
+	    {
+	      *p = '.';
+	      p += 3;
+	    }
+	  else
+	    {
+	      /* We can't access the dir, so we won't be able to
+		 access dir/.. either.  Strip out dir/..  We know dir
+		 isn't `.' because we've rid ourselves of `.' path
+		 components above.  */
+	      dest = p - 1;
+	      while (dest != result && *dest == '/')
+		--dest;
+	      while (dest != result && dest[-1] != '/')
+		--dest;
+	      /* Don't strip leading `/'.  */
+	      while (*dest == '/')
+		++dest;
+	      src = p + 3;
+	      while (*src == '/')
+		++src;
+	      p = dest;
+	      while ((*dest++ = *src++) != 0)
+		;
+	    }
+	}
+      else
+	++p;
+    }
+
 #ifdef UPDATE_PATH_HOST_CANONICALIZE
   /* Perform host dependent canonicalization when needed.  */
-  UPDATE_PATH_HOST_CANONICALIZE (path);
+  UPDATE_PATH_HOST_CANONICALIZE (result);
 #endif
 
 #ifdef DIR_SEPARATOR_2

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strip out ".." from gcc search paths
  2002-07-16 20:08 strip out ".." from gcc search paths Alan Modra
@ 2002-07-17 11:15 ` Richard Henderson
  2002-07-17 21:01   ` Alan Modra
  2002-07-24  3:24 ` Alan Modra
  2002-07-27  4:09 ` Jason Merrill
  2 siblings, 1 reply; 8+ messages in thread
From: Richard Henderson @ 2002-07-17 11:15 UTC (permalink / raw)
  To: gcc-patches

On Wed, Jul 17, 2002 at 12:01:17PM +0930, Alan Modra wrote:
> +      if (p[1] == '/' && (p == result || p[-1] == '/'))

Should be using IS_DIR_SEPARATOR throughout.


r~

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strip out ".." from gcc search paths
  2002-07-17 11:15 ` Richard Henderson
@ 2002-07-17 21:01   ` Alan Modra
  2002-07-17 22:44     ` Richard Henderson
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Modra @ 2002-07-17 21:01 UTC (permalink / raw)
  To: Richard Henderson, gcc-patches

On Wed, Jul 17, 2002 at 11:11:43AM -0700, Richard Henderson wrote:
> On Wed, Jul 17, 2002 at 12:01:17PM +0930, Alan Modra wrote:
> > +      if (p[1] == '/' && (p == result || p[-1] == '/'))
> 
> Should be using IS_DIR_SEPARATOR throughout.

Right.  Is the patch OK with that change?

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strip out ".." from gcc search paths
  2002-07-17 21:01   ` Alan Modra
@ 2002-07-17 22:44     ` Richard Henderson
  0 siblings, 0 replies; 8+ messages in thread
From: Richard Henderson @ 2002-07-17 22:44 UTC (permalink / raw)
  To: gcc-patches

On Thu, Jul 18, 2002 at 01:21:58PM +0930, Alan Modra wrote:
> Right.  Is the patch OK with that change?

Yeah.


r~

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strip out ".." from gcc search paths
  2002-07-16 20:08 strip out ".." from gcc search paths Alan Modra
  2002-07-17 11:15 ` Richard Henderson
@ 2002-07-24  3:24 ` Alan Modra
  2002-07-27 16:37   ` Richard Henderson
  2002-07-27  4:09 ` Jason Merrill
  2 siblings, 1 reply; 8+ messages in thread
From: Alan Modra @ 2002-07-24  3:24 UTC (permalink / raw)
  To: gcc-patches

This modifies update_path `..' stripping code to make it a little
safer.  Since someone found a problem with stripping leading `.'s,
I've been worried there are other latent bugs lurking.

gcc/ChangeLog
	* prefix.c (update_path): Don't strip single `.' path components
	unless stripping a later `..' component.  Exit loop as soon as
	a valid path is found.

OK mainline?

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

Index: gcc/prefix.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/prefix.c,v
retrieving revision 1.36
diff -u -p -r1.36 prefix.c
--- gcc/prefix.c	19 Jul 2002 13:24:55 -0000	1.36
+++ gcc/prefix.c	24 Jul 2002 03:08:02 -0000
@@ -284,49 +284,39 @@ update_path (path, key)
       p = strchr (p, '.');
       if (p == NULL)
 	break;
-      /* Get rid of a leading `./' and replace `/./' with `/', when
-	 such components are followed with another `.'.  */
-      if (IS_DIR_SEPARATOR (p[1])
-	  && (p == result || IS_DIR_SEPARATOR (p[-1])))
-	{
-	  src = p + 2;
-	  /* Be careful about .//foo  */
-	  while (IS_DIR_SEPARATOR (*src))
-	    ++src;
-	  if (*src == '.')
-	    {
-	      dest = p;
-	      while ((*dest++ = *src++) != 0)
-		;
-	    }
-	  else
-	    ++p;
-	}
       /* Look for `/../'  */
-      else if (p[1] == '.'
-	       && IS_DIR_SEPARATOR (p[2])
-	       && (p != result && IS_DIR_SEPARATOR (p[-1])))
+      if (p[1] == '.'
+	  && IS_DIR_SEPARATOR (p[2])
+	  && (p != result && IS_DIR_SEPARATOR (p[-1])))
 	{
 	  *p = 0;
 	  if (!ALWAYS_STRIP_DOTDOT && access (result, X_OK) == 0)
 	    {
 	      *p = '.';
-	      p += 3;
+	      break;
 	    }
 	  else
 	    {
 	      /* We can't access the dir, so we won't be able to
-		 access dir/.. either.  Strip out dir/..  We know dir
-		 isn't `.' because we've rid ourselves of `.' path
-		 components above.  */
-	      dest = p - 1;
-	      while (dest != result && IS_DIR_SEPARATOR (*dest))
-		--dest;
-	      while (dest != result && !IS_DIR_SEPARATOR (dest[-1]))
-		--dest;
-	      /* Don't strip leading `/'.  */
-	      while (IS_DIR_SEPARATOR (*dest))
-		++dest;
+		 access dir/.. either.  Strip out `dir/..'.  If `dir'
+		 turns out to be `.', strip one more path component.  */
+	      dest = p;
+	      do
+		{
+		  --dest;
+		  while (dest != result && IS_DIR_SEPARATOR (*dest))
+		    --dest;
+		  while (dest != result && !IS_DIR_SEPARATOR (dest[-1]))
+		    --dest;
+		}
+	      while (dest != result && *dest == '.');
+	      /* If we have something like `./..' or `/..', don't
+		 strip anything more.  */
+	      if (*dest == '.' || IS_DIR_SEPARATOR (*dest))
+		{
+		  *p = '.';
+		  break;
+		}
 	      src = p + 3;
 	      while (IS_DIR_SEPARATOR (*src))
 		++src;

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strip out ".." from gcc search paths
  2002-07-16 20:08 strip out ".." from gcc search paths Alan Modra
  2002-07-17 11:15 ` Richard Henderson
  2002-07-24  3:24 ` Alan Modra
@ 2002-07-27  4:09 ` Jason Merrill
  2002-07-27  6:18   ` Alan Modra
  2 siblings, 1 reply; 8+ messages in thread
From: Jason Merrill @ 2002-07-27  4:09 UTC (permalink / raw)
  To: gcc-patches

Hmm.  I believe we deliberately didn't do this in the past, to avoid
getting confused by symlinks.

Jason

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strip out ".." from gcc search paths
  2002-07-27  4:09 ` Jason Merrill
@ 2002-07-27  6:18   ` Alan Modra
  0 siblings, 0 replies; 8+ messages in thread
From: Alan Modra @ 2002-07-27  6:18 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Sat, Jul 27, 2002 at 11:44:59AM +0100, Jason Merrill wrote:
> Hmm.  I believe we deliberately didn't do this in the past, to avoid
> getting confused by symlinks.

Yes, I understand.  The code only strips out `dir/../' when `dir'
can't be accessed, ie. doesn't exist.  The latest patch, which is
how I should have written it in the first place :), is even more
cautious about tweaking the path.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strip out ".." from gcc search paths
  2002-07-24  3:24 ` Alan Modra
@ 2002-07-27 16:37   ` Richard Henderson
  0 siblings, 0 replies; 8+ messages in thread
From: Richard Henderson @ 2002-07-27 16:37 UTC (permalink / raw)
  To: gcc-patches

On Wed, Jul 24, 2002 at 06:05:06PM +0930, Alan Modra wrote:
> 	* prefix.c (update_path): Don't strip single `.' path components
> 	unless stripping a later `..' component.  Exit loop as soon as
> 	a valid path is found.

Ok.


r~

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2002-07-27 23:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-16 20:08 strip out ".." from gcc search paths Alan Modra
2002-07-17 11:15 ` Richard Henderson
2002-07-17 21:01   ` Alan Modra
2002-07-17 22:44     ` Richard Henderson
2002-07-24  3:24 ` Alan Modra
2002-07-27 16:37   ` Richard Henderson
2002-07-27  4:09 ` Jason Merrill
2002-07-27  6:18   ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).