From 2d720aed1843b47aafb2af8bfd15139228545e2b Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 16 Jun 2021 18:19:04 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20crash=20on=20relative=20PWD=20v?= =?UTF-8?q?alue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Even though PWD “shall represent an absolute pathname of the current working directory”[1], we shouldn’t crash if a user sets it to a relative path. [1]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03 Fixes #860. --- src/Util.cpp | 2 +- test/run | 1 + test/suites/basedir.bash | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Util.cpp b/src/Util.cpp index f49708fd46..4a996e6116 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -617,7 +617,7 @@ get_apparent_cwd(const std::string& actual_cwd) return actual_cwd; #else auto pwd = getenv("PWD"); - if (!pwd) { + if (!pwd || !Util::is_absolute_path(pwd)) { return actual_cwd; } diff --git a/test/run b/test/run index cbdd98f062..aa3d288f3e 100755 --- a/test/run +++ b/test/run @@ -356,6 +356,7 @@ reset_environment() { unset TERM unset XDG_CACHE_HOME unset XDG_CONFIG_HOME + export PWD=$(pwd) export CCACHE_DETECT_SHEBANG=1 export CCACHE_DIR=$ABS_TESTDIR/.ccache diff --git a/test/suites/basedir.bash b/test/suites/basedir.bash index d1d0aef153..46d2a4258e 100644 --- a/test/suites/basedir.bash +++ b/test/suites/basedir.bash @@ -311,4 +311,36 @@ EOF expect_stat 'cache miss' 1 expect_equal_content reference.stderr ccache.stderr fi + + # ------------------------------------------------------------------------- + TEST "Relative PWD" + + cd dir1 + CCACHE_BASEDIR="$(pwd)" PWD=. $CCACHE_COMPILE -I$(pwd)/include -c src/test.c + expect_stat 'cache hit (direct)' 0 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 1 + + cd ../dir2 + CCACHE_BASEDIR="$(pwd)" PWD=. $CCACHE_COMPILE -I$(pwd)/include -c src/test.c + expect_stat 'cache hit (direct)' 1 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 1 + + # ------------------------------------------------------------------------- + TEST "Unset PWD" + + unset PWD + + cd dir1 + CCACHE_BASEDIR="$(pwd)" $CCACHE_COMPILE -I$(pwd)/include -c src/test.c + expect_stat 'cache hit (direct)' 0 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 1 + + cd ../dir2 + CCACHE_BASEDIR="$(pwd)" $CCACHE_COMPILE -I$(pwd)/include -c src/test.c + expect_stat 'cache hit (direct)' 1 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 1 }