From: Joel Rosdahl Date: Fri, 8 Jul 2016 15:21:18 +0000 (+0200) Subject: Don't rewrite source file path if it's absolute and a symlink X-Git-Tag: v3.1.12~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49c9f13e6f825460218d6c54f3878215c0e54e4d;p=thirdparty%2Fccache.git Don't rewrite source file path if it's absolute and a symlink Fixes issue #111. --- diff --git a/NEWS.txt b/NEWS.txt index 849a10f11..d1956611f 100644 --- 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 diff --git a/ccache.c b/ccache.c index 1f182630d..d3e523023 100644 --- 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 5efbca4f7..490e97576 100755 --- 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 <d1/d2/c.c +#include +#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 <d/c.c +#include +#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`"