]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Don't rewrite source file path if it's absolute and a symlink
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 8 Jul 2016 15:21:18 +0000 (17:21 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 8 Jul 2016 15:21:18 +0000 (17:21 +0200)
Fixes issue #111.

NEWS.txt
ccache.c
test.sh

index 849a10f1146f826365e99cb98a5274ee347d9db8..d1956611f6dcff1beaa68403550234c87ad3f554 100644 (file)
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -2,6 +2,19 @@ ccache news
 ===========
 
 
+ccache 3.1.12
+-------------
+Release date: unknown
+
+
+Bug fixes
+~~~~~~~~~
+
+- Fixed a bug where (due to ccache rewriting paths) the compiler could choose
+  incorrect include files if `CCACHE_BASEDIR` is used and the source file path
+  is absolute and is a symlink.
+
+
 ccache 3.1.11
 -------------
 Release date: 2015-03-07
index 1f182630d1b9f7bc26c844df0794f546963e4480..d3e523023d3678247cc9e2f7083dbaae61283e3a 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -2,7 +2,7 @@
  * ccache -- a fast C/C++ compiler cache
  *
  * Copyright (C) 2002-2007 Andrew Tridgell
- * Copyright (C) 2009-2015 Joel Rosdahl
+ * Copyright (C) 2009-2016 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -1747,8 +1747,17 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args,
                        goto out;
                }
 
-               /* Rewrite to relative to increase hit rate. */
-               input_file = make_relative_path(x_strdup(argv[i]));
+               lstat(argv[i], &st);
+               if (S_ISLNK(st.st_mode)) {
+                       /* Don't rewrite source file path if it's a symlink since
+                          make_relative_path resolves symlinks using realpath(3) and this leads
+                          to potentially choosing incorrect relative header files. See the
+                          "symlink to source file" test. */
+                       input_file = x_strdup(argv[i]);
+               } else {
+                       /* Rewrite to relative to increase hit rate. */
+                       input_file = make_relative_path(x_strdup(argv[i]));
+               }
        }
 
        if (!input_file) {
diff --git a/test.sh b/test.sh
index 5efbca4f78b6e2a8e122c1694c72101df019c96e..490e975762f7851eeac3b4048ca611034abfa589 100755 (executable)
--- a/test.sh
+++ b/test.sh
@@ -3,7 +3,7 @@
 # A simple test suite for ccache.
 #
 # Copyright (C) 2002-2007 Andrew Tridgell
-# Copyright (C) 2009-2015 Joel Rosdahl
+# Copyright (C) 2009-2016 Joel Rosdahl
 #
 # This program is free software; you can redistribute it and/or modify it under
 # the terms of the GNU General Public License as published by the Free Software
@@ -1837,6 +1837,59 @@ EOF
     checkstat 'cache miss' 2
 }
 
+symlinks_suite() {
+    ##################################################################
+    testname="symlink to source directory"
+
+    mkdir dir
+    cd dir
+    mkdir -p d1/d2
+    echo '#define A "OK"' >d1/h.h
+    cat <<EOF >d1/d2/c.c
+#include <stdio.h>
+#include "../h.h"
+int main() { printf("%s\n", A); }
+EOF
+    echo '#define A "BUG"' >h.h
+    ln -s d1/d2 d3
+
+    CCACHE_BASEDIR=/ $CCACHE $COMPILER -c $PWD/d3/c.c
+    $COMPILER -c $PWD/d3/c.c
+    $COMPILER c.o -o c
+    result=$(./c)
+    if [ "$result" != OK ]; then
+        test_failed "Incorrect header file used"
+    fi
+
+    cd ..
+    rm -rf dir
+
+    ##################################################################
+    testname="symlink to source file"
+
+    mkdir dir
+    cd dir
+    mkdir d
+    echo '#define A "BUG"' >d/h.h
+    cat <<EOF >d/c.c
+#include <stdio.h>
+#include "h.h"
+int main() { printf("%s\n", A); }
+EOF
+    echo '#define A "OK"' >h.h
+    ln -s d/c.c c.c
+
+    CCACHE_BASEDIR=/ $CCACHE $COMPILER -c $PWD/c.c
+    $COMPILER c.o -o c
+    result=$(./c)
+    if [ "$result" != OK ]; then
+        test_failed "Incorrect header file used"
+    fi
+
+    cd ..
+    rm -rf dir
+}
+
 ######################################################################
 # main program
 
@@ -1885,6 +1938,7 @@ readonly
 extrafiles
 cleanup
 pch
+symlinks
 "
 
 host_os="`uname -s`"