git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] Do not skip prefix_filename() when there is no prefix
@ 2016-05-22 11:12 Nguyễn Thái Ngọc Duy
  0 siblings, 0 replies; only message in thread
From: Nguyễn Thái Ngọc Duy @ 2016-05-22 11:12 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

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 <pclouds@gmail.com>
---
 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/20160522093356.22389-7-pclouds@gmail.com

 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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-05-22 11:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-22 11:12 [PATCH] Do not skip prefix_filename() when there is no prefix Nguyễn Thái Ngọc Duy

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).