zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers <zsh-workers@sunsite.dk>
Subject: Re: cd -s symlink hangs (sometimes?)
Date: Tue, 24 Mar 2009 16:02:15 +0000	[thread overview]
Message-ID: <20090324160215.21c7236b@news01> (raw)
In-Reply-To: <090324081538.ZM6216@torch.brasslantern.com>

On Tue, 24 Mar 2009 08:15:38 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Mar 24, 12:46pm, Peter Stephenson wrote:
> }
> } For the diagnostics and pwd, I've just borrowed what the recursive
> } handling in zsh/files does, which is cd to /, set pwd consistently,
> } and report the error.
> 
> This is calling zerr(), which sets errflag, which means a script that
> encounters this problem would abort at that point, correct?  (That
> seems to be the behavior in a few simple tests I did.)

Yes, this seems serious enough.

> I'd be worried that if the only error is that chdir returns nonzero,
> changing into the root directory creates a security problem (what if
> the user is privileged and the next command is "rm -rf *" etc.).
> 
> Maybe it would be better to first attempt chdir($HOME) and only fall
> back on "/" if that also fails.

That makes sense, that is what "cd" on its own does, after all.

> Also, should $OLDPWD be getting updated?
> I'm undecided on that.  We did change directories, even though not to the
> desired destination.

Codewise, we're a little bit too low down to deal with this easily and
correctly.  I think the only consistent way of doing this would be to call
cd_new_pd(), which we currently only do on a successful cd or pushd.
Anything else looks to me like an inconsistent half measure.  We would then
be executing chpwd (etc.); this might be correct (fixing up a terminal
header, for example), or it might do stuff the user didn't want.  It's hard
to decide.

Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.219
diff -u -r1.219 utils.c
--- Src/utils.c	24 Mar 2009 12:52:07 -0000	1.219
+++ Src/utils.c	24 Mar 2009 15:58:30 -0000
@@ -5493,16 +5493,30 @@
     }
     if (restoredir(d)) {
 	int restoreerr = errno;
+	int i;
 	/*
 	 * Failed to restore the directory.
 	 * Just be definite, cd to root and report the result.
 	 */
-	zsfree(pwd);
-	pwd = ztrdup("/");
-	if (chdir(pwd) < 0)
+	for (i = 0; i < 2; i++) {
+	    const char *cdest;
+	    if (i)
+		cdest = "/";
+	    else {
+		if (!home)
+		    continue;
+		cdest = home;
+	    }
+	    zsfree(pwd);
+	    pwd = ztrdup(cdest);
+	    if (chdir(pwd) == 0)
+		break;
+	}
+	if (i == 2)
 	    zerr("lost current directory, failed to cd to /: %e", errno);
 	else
-	    zerr("lost current directory: %e: changed to /", restoreerr);
+	    zerr("lost current directory: %e: changed to `%s'", restoreerr,
+		pwd);
 	if (d == &ds)
 	    zsfree(ds.dirname);
 #ifdef HAVE_FCHDIR


-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


  reply	other threads:[~2009-03-24 16:07 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-20 21:12 Mikael Magnusson
2009-03-20 22:48 ` Peter Stephenson
2009-03-20 23:15   ` Mikael Magnusson
2009-03-22 12:54     ` Peter Stephenson
2009-03-22 23:05       ` Mikael Magnusson
2009-03-23 10:49         ` Peter Stephenson
2009-03-23 11:46           ` Mikael Magnusson
2009-03-23 12:27             ` Peter Stephenson
2009-03-24 12:46               ` Peter Stephenson
2009-03-24 15:15                 ` Bart Schaefer
2009-03-24 16:02                   ` Peter Stephenson [this message]
2009-04-06 11:07                 ` Mikael Magnusson
2009-04-06 11:13                   ` Peter Stephenson
2009-03-23 15:18       ` Bart Schaefer
2009-03-23 15:39         ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090324160215.21c7236b@news01 \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).