Blob Blame History Raw
From 08e287503fb4a8657962917891f2b7b90b713678 Mon Sep 17 00:00:00 2001
From: Peter Stephenson <pws@zsh.org>
Date: Tue, 13 Jun 2017 15:41:00 +0100
Subject: [PATCH] 41284: Fix NULL dereference in cd.

This happened in sh compatiblity mode if HOME was not set
and cd was used with no argument.

Upstream-commit: eb783754bdb74377f3cea4ceca9c23a02ea1bf58
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
---
 Src/builtin.c   | 11 ++++++++++-
 Test/B01cd.ztst |  4 ++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/Src/builtin.c b/Src/builtin.c
index ccfa745..caa4b64 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -842,8 +842,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;
@@ -898,6 +903,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 c7920dd..e56fd26 100644
--- a/Test/B01cd.ztst
+++ b/Test/B01cd.ztst
@@ -125,6 +125,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
-- 
2.14.3