f686d7
From 08e287503fb4a8657962917891f2b7b90b713678 Mon Sep 17 00:00:00 2001
f686d7
From: Peter Stephenson <pws@zsh.org>
f686d7
Date: Tue, 13 Jun 2017 15:41:00 +0100
f686d7
Subject: [PATCH] 41284: Fix NULL dereference in cd.
f686d7
f686d7
This happened in sh compatiblity mode if HOME was not set
f686d7
and cd was used with no argument.
f686d7
f686d7
Upstream-commit: eb783754bdb74377f3cea4ceca9c23a02ea1bf58
f686d7
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
f686d7
---
f686d7
 Src/builtin.c   | 11 ++++++++++-
f686d7
 Test/B01cd.ztst |  4 ++++
f686d7
 2 files changed, 14 insertions(+), 1 deletion(-)
f686d7
f686d7
diff --git a/Src/builtin.c b/Src/builtin.c
f686d7
index ccfa745..caa4b64 100644
f686d7
--- a/Src/builtin.c
f686d7
+++ b/Src/builtin.c
f686d7
@@ -842,8 +842,13 @@ cd_get_dest(char *nam, char **argv, int hard, int func)
f686d7
 	    dir = nextnode(firstnode(dirstack));
f686d7
 	if (dir)
f686d7
 	    zinsertlinknode(dirstack, dir, getlinknode(dirstack));
f686d7
-	else if (func != BIN_POPD)
f686d7
+	else if (func != BIN_POPD) {
f686d7
+	    if (!home) {
f686d7
+		zwarnnam(nam, "HOME not set");
f686d7
+		return NULL;
f686d7
+	    }
f686d7
 	    zpushnode(dirstack, ztrdup(home));
f686d7
+	}
f686d7
     } else if (!argv[1]) {
f686d7
 	int dd;
f686d7
 	char *end;
f686d7
@@ -898,6 +903,10 @@ cd_get_dest(char *nam, char **argv, int hard, int func)
f686d7
     if (!dir) {
f686d7
 	dir = firstnode(dirstack);
f686d7
     }
f686d7
+    if (!dir || !getdata(dir)) {
f686d7
+	DPUTS(1, "Directory not set, not detected early enough");
f686d7
+	return NULL;
f686d7
+    }
f686d7
     if (!(dest = cd_do_chdir(nam, getdata(dir), hard))) {
f686d7
 	if (!target)
f686d7
 	    zsfree(getlinknode(dirstack));
f686d7
diff --git a/Test/B01cd.ztst b/Test/B01cd.ztst
f686d7
index c7920dd..e56fd26 100644
f686d7
--- a/Test/B01cd.ztst
f686d7
+++ b/Test/B01cd.ztst
f686d7
@@ -125,6 +125,10 @@ F:something is broken.  But you already knew that.
f686d7
 0:
f686d7
 ?(eval):cd:3: not a directory: link_to_nonexistent
f686d7
 
f686d7
+ (unset HOME; ARGV0=sh $ZTST_testdir/../Src/zsh -c cd)
f686d7
+1:Implicit cd with unset HOME.
f686d7
+?zsh:cd:1: HOME not set
f686d7
+
f686d7
 %clean
f686d7
 # This optional section cleans up after the test, if necessary,
f686d7
 # e.g. killing processes etc.  This is in addition to the removal of *.tmp
f686d7
-- 
f686d7
2.14.3
f686d7