From 2df269a3121889ebcdfa5d98dfb4d675f690e039 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Fri, 27 Jul 2012 15:59:13 +0200 Subject: [PATCH] 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. --- ccache.c | 16 ++++++++++++++-- util.c | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) 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) -- 2.47.2