From: Joel Rosdahl Date: Fri, 27 Jul 2012 13:59:13 +0000 (+0200) Subject: Canonicalize paths when computing path relative to base directory X-Git-Tag: v3.1.8~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2df269a3;p=thirdparty%2Fccache.git Canonicalize paths when computing path relative to base directory This fixes a bug when current working directory contains a "/./" part. Based on a patch by Eric Blau. --- diff --git a/ccache.c b/ccache.c index 79221b8b1..1f8ee64eb 100644 --- a/ccache.c +++ b/ccache.c @@ -386,14 +386,18 @@ ignore: static char * make_relative_path(char *path) { - char *relpath; + char *relpath, *canon_path; if (!base_dir || !str_startswith(path, base_dir)) { return path; } if (!current_working_dir) { - current_working_dir = get_cwd(); + char *cwd = get_cwd(); + if (cwd) { + current_working_dir = x_realpath(cwd); + free(cwd); + } if (!current_working_dir) { cc_log("Unable to determine current working directory: %s", strerror(errno)); @@ -401,6 +405,14 @@ make_relative_path(char *path) } } + canon_path = x_realpath(path); + if (canon_path) { + free(path); + path = canon_path; + } else { + /* path doesn't exist, so leave it as it is. */ + } + relpath = get_relative_path(current_working_dir, path); free(path); return relpath; diff --git a/util.c b/util.c index 3086b2c62..acfcd9db6 100644 --- a/util.c +++ b/util.c @@ -984,7 +984,8 @@ common_dir_prefix_length(const char *s1, const char *s2) } /* - * Compute a relative path from from to to. Caller frees. + * Compute a relative path from from to to. Assumes that both from and to are + * well-formed and canonical. Caller frees. */ char * get_relative_path(const char *from, const char *to)