From: Joel Rosdahl Date: Wed, 16 Jun 2021 16:19:04 +0000 (+0200) Subject: Don’t crash on relative PWD value X-Git-Tag: v4.4~200 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d720aed1843b47aafb2af8bfd15139228545e2b;p=thirdparty%2Fccache.git Don’t crash on relative PWD value 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. --- diff --git a/src/Util.cpp b/src/Util.cpp index f49708fd4..4a996e611 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 cbdd98f06..aa3d288f3 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 d1d0aef15..46d2a4258 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 }