]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Don’t crash on relative PWD value
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 16 Jun 2021 16:19:04 +0000 (18:19 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 16 Jun 2021 18:07:26 +0000 (20:07 +0200)
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
test/run
test/suites/basedir.bash

index f49708fd4653d35352a3e54d776e82a73372e844..4a996e61168983cc3bff64f45b16f0234dfe23a1 100644 (file)
@@ -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;
   }
 
index cbdd98f0622ff4355587fb8c8781e2f2fb811b44..aa3d288f3eefeca2328d7de351523b57cb312173 100755 (executable)
--- 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
index d1d0aef153cac45be92cf92bc64826230d01fc67..46d2a4258eeadea316306adb0629abf68ca6f3f8 100644 (file)
@@ -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
 }