From b54f5ba56e6315028186af5d9e3b914dfae5a74f Mon Sep 17 00:00:00 2001
From: Fabian Homborg <FHomborg@gmail.com>
Date: Fri, 14 Feb 2020 17:18:27 +0100
Subject: [PATCH 1/4] parser: Stop crashing when cwd isn't readable
Everything seems to be working, so instead of crashing just print an
error and return.
Fixes #6597
(cherry picked from commit ceba851d44dea68b9e1b2b551e37fd6834767357)
---
src/parser.cpp | 2 +-
tests/checks/init-unreadable-cwd.fish | 17 +++++++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 tests/checks/init-unreadable-cwd.fish
diff --git a/src/parser.cpp b/src/parser.cpp
index 381b8ac06..1eae37392 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -85,7 +85,7 @@ parser_t::parser_t(std::shared_ptr<env_stack_t> vars) : variables(std::move(vars
int cwd = open_cloexec(".", O_RDONLY);
if (cwd < 0) {
perror("Unable to open the current working directory");
- abort();
+ return;
}
libdata().cwd_fd = std::make_shared<const autoclose_fd_t>(cwd);
}
diff --git a/tests/checks/init-unreadable-cwd.fish b/tests/checks/init-unreadable-cwd.fish
new file mode 100644
index 000000000..fee220847
--- /dev/null
+++ b/tests/checks/init-unreadable-cwd.fish
@@ -0,0 +1,17 @@
+#RUN: %fish -C 'set -g fish %fish' %s
+# Test that fish doesn't crash if cwd is unreadable at the start (#6597)
+
+set -l oldpwd $PWD
+set -l tmpdir (mktemp -d)
+
+# $fish might be a relative path (e.g. "../test/root/bin/fish")
+set -l fish (builtin realpath $fish)
+cd $tmpdir
+chmod 000 .
+$fish -c 'echo Look Ma! No crashing!'
+#CHECK: Look Ma! No crashing!
+#CHECKERR: Unable to open the current working directory: Permission denied
+
+# Careful here, Solaris' rm tests if the directory is in $PWD, so we need to cd back
+cd $oldpwd
+rmdir $tmpdir
--
2.25.0