Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: NULL pointer dereference in zsh 5.3.1 with builtin chdir



On Tue, 13 Jun 2017 15:20:07 +0100
Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> The following includes a specific fix for HOME not being set and a
> catch-all lower down if dir isn't set for some other reason (paranoia).

Sorry... should therefore return immediately on the first occurrence,
not wait for the catch-all.

pws

diff --git a/Src/builtin.c b/Src/builtin.c
index 0b39494..2e72ba2 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -880,8 +880,13 @@ cd_get_dest(char *nam, char **argv, int hard, int func)
 	    dir = nextnode(firstnode(dirstack));
 	if (dir)
 	    zinsertlinknode(dirstack, dir, getlinknode(dirstack));
-	else if (func != BIN_POPD)
+	else if (func != BIN_POPD) {
+	    if (!home) {
+		zwarnnam(nam, "HOME not set");
+		return NULL;
+	    }
 	    zpushnode(dirstack, ztrdup(home));
+	}
     } else if (!argv[1]) {
 	int dd;
 	char *end;
@@ -936,6 +941,10 @@ cd_get_dest(char *nam, char **argv, int hard, int func)
     if (!dir) {
 	dir = firstnode(dirstack);
     }
+    if (!dir || !getdata(dir)) {
+	DPUTS(1, "Directory not set, not detected early enough");
+	return NULL;
+    }
     if (!(dest = cd_do_chdir(nam, getdata(dir), hard))) {
 	if (!target)
 	    zsfree(getlinknode(dirstack));
diff --git a/Test/B01cd.ztst b/Test/B01cd.ztst
index 94447e7..8d4f095 100644
--- a/Test/B01cd.ztst
+++ b/Test/B01cd.ztst
@@ -137,6 +137,10 @@ F:something is broken.  But you already knew that.
 0:
 ?(eval):cd:3: not a directory: link_to_nonexistent
 
+ (unset HOME; ARGV0=sh $ZTST_testdir/../Src/zsh -c cd)
+1:Implicit cd with unset HOME.
+?zsh:cd:1: HOME not set
+
 %clean
 # This optional section cleans up after the test, if necessary,
 # e.g. killing processes etc.  This is in addition to the removal of *.tmp



Messages sorted by: Reverse Date, Date, Thread, Author