From Martin.Kraemer@Fujitsu-Siemens.com  Fri Mar 15 03:15:18 2002
Return-Path: <Martin.Kraemer@Fujitsu-Siemens.com>
Received: from naxos.pdb.sbs.de (naxos.pdb.sbs.de [192.109.3.5])
	by hub.freebsd.org (Postfix) with ESMTP id B66C137B402
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 15 Mar 2002 03:15:16 -0800 (PST)
Received: from trolli.pdb.fsc.net (ThisAddressDoesNotExist [172.25.97.20] (may be forged))
	by naxos.pdb.sbs.de (8.11.2/8.11.2) with ESMTP id g2FBEft02220;
	Fri, 15 Mar 2002 12:14:41 +0100
Received: from deejai2.mch.fsc.net (deejai2.mch.fsc.net [172.25.124.236])
	by trolli.pdb.fsc.net (8.9.3/8.9.3) with ESMTP id MAA01919;
	Fri, 15 Mar 2002 12:14:40 +0100
Received: (from martin@localhost)
	by deejai2.mch.fsc.net (8.11.6/8.11.6) id g2FBEcW12401;
	Fri, 15 Mar 2002 12:14:38 +0100 (CET)
	(envelope-from martin)
Message-Id: <200203151114.g2FBEcW12401@deejai2.mch.fsc.net>
Date: Fri, 15 Mar 2002 12:14:38 +0100 (CET)
From: Martin.Kraemer@Fujitsu-Siemens.com
Reply-To: <Martin.Kraemer@Fujitsu-Siemens.com>
To: FreeBSD-gnats-submit@freebsd.org, tcsh@mx.gw.com,
	tcsh-bugs@mx.gw.com, submit@bugs.debian.org
Cc: christos@zoulas.com
Subject: Wrong path reduction of dot-dot paths in dnormalize()
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         35921
>Category:       bin
>Synopsis:       Wrong path reduction of dot-dot paths in dnormalize()
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    jon
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Mar 15 03:20:01 PST 2002
>Closed-Date:    Sat Jul 17 20:45:49 GMT 2004
>Last-Modified:  Sat Jul 17 20:45:49 GMT 2004
>Originator:     
>Release:        FreeBSD 4.5-STABLE i386
>Organization:
Fujitsu-Siemens.com
>Environment:
System: FreeBSD deejai2.mch.fsc.net 4.5-STABLE FreeBSD 4.5-STABLE #6: Thu Jan 31 21:40:04 CET 2002 martin@deejai2.mch.fsc.net:/usr/src/sys/compile/DEEJAI4B i386

>Description:
	This patch tries to fix an error which I discovered
	when compiling a source file. I tried
	    % set symlinks = expand
	    % cc -I../.. blah.c
	and was astonished that the compiler complained about
	a nonexistant file "/home/martin".
	See below for the reason and fix.
	
	It manifests for command line arguments which end in
	"/.." (or contain "/../") but which are not valid
	paths, like "-I../..". This error only happens if
	"set symlinks = expand" is set.
	
	    % cd /usr/local/bin
	    % set symlinks = expand
	    % printf "%s\n" -I. -I../.. -I../../.. ../.. ..
	    -I.
	    /usr/local/bin
	    /usr/local
	    /usr
	    /usr/local
	
	Note that for "-I../.." and "-I../../..", something
	*very* strange has happened: their prefix "-I"
	vanished and they were replaced by an (off-by-one
	depth) absolute path. (That is done in dnormalize()
	which assumes that a relative path exists and can be
	shortened/normalized by removing redundant "../something"
	pairs.
	
	The fix tries to test whether it makes sense to call
	dnormalize() -- it only does if the path is a valid
	path in the first place.
	
	The output after applying the fix is:
	
	    % printf "%s\n" -I. -I../.. -I../../.. ../.. ..
	    -I.
	    -I../..
	    -I../../..
	    /usr
	    /usr/local
	which is what I expected.

>How-To-Repeat:
	% cd /usr/local/bin
	% set symlinks = expand
	% echo -I../..
	/usr/local/bin

>Fix:

Index: sh.dir.c
===================================================================
RCS file: /home/cvs/tcsh/sh.dir.c,v
retrieving revision 1.1.1.6
diff -u -r1.1.1.6 sh.dir.c
--- sh.dir.c	15 Mar 2002 10:30:34 -0000	1.1.1.6
+++ sh.dir.c	15 Mar 2002 11:04:23 -0000
@@ -415,6 +415,18 @@
 	        if ((TRM(cwd[(dotdot = (int) Strlen(cwd)) - 1])) == '/')
 		    cwd[--dotdot] = '\0';
 	    }
+	    /* Reduction of ".." following the stuff we collected in buf
+	     * only makes sense if the directory item in buf really exists.
+	     * Avoid reduction of "-I../.." (typical compiler call) to ""
+	     * or "/usr/nonexistant/../bin" to "/usr/bin":
+	     */
+	    if (cwd[0]) {
+	        struct stat exists;
+		if (0 != stat(short2str(cwd), &exists)) {
+		    xfree((ptr_t) cwd);
+		    return Strsave(start);
+		}
+	    }
 	    if (!*cp)
 	        break;
 	}
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->analyzed 
State-Changed-By: jon 
State-Changed-When: Sat Jul 6 02:00:28 PDT 2002 
State-Changed-Why:  

tcsh developers notified of problem and proposed solution. 


Responsible-Changed-From-To: freebsd-bugs->jon 
Responsible-Changed-By: jon 
Responsible-Changed-When: Sat Jul 6 02:00:28 PDT 2002 
Responsible-Changed-Why:  

tcsh developers notified of problem and proposed solution 

http://www.freebsd.org/cgi/query-pr.cgi?pr=35921 
State-Changed-From-To: analyzed->patched 
State-Changed-By: jon 
State-Changed-When: Sun Aug 4 05:27:21 PDT 2002 
State-Changed-Why:  
proposed changed incorporated into tcsh 6.12 and imported to -CURRENT. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=35921 
State-Changed-From-To: patched->closed 
State-Changed-By: maxim 
State-Changed-When: Sat Jul 17 20:45:13 GMT 2004 
State-Changed-Why:  
Fixed in -CURRENT and -STABLE about two years ago. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=35921 
>Unformatted:
 Package: tcsh
 Version: 6.11.0
 
