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