Subject: [PATCH] Do not skip prefix_filename() when there is no prefix

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 22 May 2016 18:12:03 +0700

On UNIX, when there is no prefix, prefix_filename() returns the input string
as-is. Avoiding it only makes sense before 9e5f5d4 (prefix_filename():
safely handle the case where pfx_len=0 - 2010-10-17) because the function
back then could try to dereference a NULL pointer.

On Windows, even when there is no prefix, prefix_filename() can still do
some slash conversion. Arguably it's not the best place for that (too
subtle and probably can't cover all paths) but it's how it is.

Let's always call prefix_filename() to make Windows users a tiny bit happier.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 I left out worktree.c on purpose because that call site is handled
 separately [1]. In fact this patch is the result of Junio's comment
 on [1] (its earlier iteration).

 [1] http://mid.gmane.org/[email-masked]

 builtin/apply.c       | 2 +-
 builtin/hash-object.c | 2 +-
 diff-no-index.c       | 2 +-
 parse-options.c       | 9 ++++++---
 setup.c               | 8 ++++----
 5 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 8e4da2e..08f8db3 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4615,7 +4615,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			errs |= apply_patch(0, "<stdin>", options);
 			read_stdin = 0;
 			continue;
-		} else if (0 < prefix_length)
+		} else
 			arg = prefix_filename(prefix, prefix_length, arg);
 
 		fd = open(arg, O_RDONLY);
diff --git a/builtin/hash-object.c b/builtin/hash-object.c
index f7d3567..45be93a 100644
--- a/builtin/hash-object.c
+++ b/builtin/hash-object.c
@@ -110,7 +110,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
 	if (flags & HASH_WRITE_OBJECT) {
 		prefix = setup_git_directory();
 		prefix_length = prefix ? strlen(prefix) : 0;
-		if (vpath && prefix)
+		if (vpath)
 			vpath = prefix_filename(prefix, prefix_length, vpath);
 	}
 
diff --git a/diff-no-index.c b/diff-no-index.c
index 1f8999b..76cc80c 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -268,7 +268,7 @@ void diff_no_index(struct rev_info *revs,
 			 * path that is "-", spell it as "./-".
 			 */
 			p = file_from_standard_input;
-		else if (prefixlen)
+		else
 			p = xstrdup(prefix_filename(prefix, prefixlen, p));
 		paths[i] = p;
 	}
diff --git a/parse-options.c b/parse-options.c
index 47a9192..8b54317 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -37,10 +37,13 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
 
 static void fix_filename(const char *prefix, const char **file)
 {
-	if (!file || !*file || !prefix || is_absolute_path(*file)
-	    || !strcmp("-", *file))
+	int prefix_length;
+
+	if (!file || !*file || is_absolute_path(*file) || !strcmp("-", *file))
 		return;
-	*file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
+
+	prefix_length = prefix ? strlen(prefix) : 0;
+	*file = xstrdup(prefix_filename(prefix, prefix_length, *file));
 }
 
 static int opt_command_mode_error(const struct option *opt,
diff --git a/setup.c b/setup.c
index c86bf5c..291385e 100644
--- a/setup.c
+++ b/setup.c
@@ -141,10 +141,10 @@ int check_filename(const char *prefix, const char *arg)
 		if (arg[2] == '\0') /* ":/" is root dir, always exists */
 			return 1;
 		name = arg + 2;
-	} else if (prefix)
-		name = prefix_filename(prefix, strlen(prefix), arg);
-	else
-		name = arg;
+	} else {
+		int prefix_length = prefix ? strlen(prefix) : 0;
+		name = prefix_filename(prefix, prefix_length, arg);
+	}
 	if (!lstat(name, &st))
 		return 1; /* file exists */
 	if (errno == ENOENT || errno == ENOTDIR)
-- 
2.8.2.524.g6ff3d78