zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] Re: `pwd -P` with systemd-homed causes inconsistent cwd state
Date: Mon, 30 Oct 2023 20:46:13 -0700	[thread overview]
Message-ID: <CAH+w=7bcBOGxYzVYSxaNu_8hEE2mtL7GS0vbAjWfQrYs2R1juA@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7YYuFa71r4c3XKZgrgsMEy9_ifQeMjYMBsOGQtM0qEhJQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 549 bytes --]

On Sun, Oct 22, 2023 at 11:59 AM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> So this reaches the "return NULL" at the end of zgetdir(), after the
> comment "Something bad happened." ?
>
> Or conversely this is a consequence of zgetdir() not checking the
> return value from zchdir(), which it doesn't in a couple of places?

Lacking a response to this and unable to test this directly, I offer
the following patch, which attempts to cover all those cases.

I forced HAVE_GETCWD to be false and ran make check with no issues.

[-- Attachment #2: zgetdir-lost.txt --]
[-- Type: text/plain, Size: 1661 bytes --]

diff --git a/Src/compat.c b/Src/compat.c
index 817bb4aaf..fbed7d80f 100644
--- a/Src/compat.c
+++ b/Src/compat.c
@@ -403,12 +403,25 @@ zgetdir(struct dirsav *d)
 		buf[--pos] = '/';
 	    if (d) {
 #ifndef HAVE_FCHDIR
-		zchdir(buf + pos);
+		if (zchdir(buf + pos) < 0) {
+		    /*
+		     * The reason for all this chdir-ing is to get the
+		     * absolute name of the current directory, so if we
+		     * cannot chdir to that name we have to assume our
+		     * directory is lost.  See "something bad" below.
+		     */
+		    noholdintr();
+		    return NULL;
+		}
 		noholdintr();
 #endif
 		return d->dirname = ztrdup(buf + pos);
 	    }
-	    zchdir(buf + pos);
+	    if (zchdir(buf + pos) < 0) {
+		/* Current directory lost */
+		noholdintr();
+		return NULL;
+	    }
 	    noholdintr();
 	    return buf + pos;
 	}
@@ -477,14 +490,22 @@ zgetdir(struct dirsav *d)
     if (d) {
 #ifndef HAVE_FCHDIR
 	if (buf[pos])
-	    zchdir(buf + pos + 1);
+	    if (zchdir(buf + pos + 1) < 0) {
+		/* Current directory lost */
+		noholdintr();
+		return NULL;
+	    }
 	noholdintr();
 #endif
 	return NULL;
     }
 
     if (buf[pos])
-	zchdir(buf + pos + 1);
+	if (zchdir(buf + pos + 1) < 0) {
+	    /* Current directory lost */
+	    noholdintr();
+	    return NULL;
+	}
     noholdintr();
 
 #else  /* __CYGWIN__, USE_GETCWD cases */
@@ -544,7 +565,11 @@ zgetcwd(void)
     }
 #endif /* HAVE_GETCWD */
     if (!ret)
-	ret = unmeta(pwd);
+	if (chdir(ret = unmeta(pwd)) < 0) {
+	    zwarn("%e: current directory %s lost, using /",
+		  errno, ret);
+	    chdir(ret = dupstring("/"));
+	}
     if (!ret || *ret == '\0')
 	ret = dupstring(".");
     return ret;

  reply	other threads:[~2023-10-31  3:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-21  3:18 Kyle Laker
2023-10-21  4:05 ` Bart Schaefer
2023-10-21 16:26   ` Kyle Laker
2023-10-22 18:59     ` Bart Schaefer
2023-10-31  3:46       ` Bart Schaefer [this message]
2023-11-02  2:54         ` [PATCH] " Kyle Laker
2023-11-03  4:28           ` Bart Schaefer
2023-11-05 14:16             ` Kyle Laker
2023-11-05 16:17               ` Bart Schaefer
2023-11-05 21:10                 ` Bart Schaefer
2023-11-20 22:23                   ` Jan Alexander Steffens (heftig)
2023-11-23 17:53                     ` Bart Schaefer

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='CAH+w=7bcBOGxYzVYSxaNu_8hEE2mtL7GS0vbAjWfQrYs2R1juA@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /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).