git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH] Do not skip prefix_filename() when there is no prefix
Date: Sun, 22 May 2016 18:12:03 +0700	[thread overview]
Message-ID: <20160522111203.26193-1-pclouds@gmail.com> (raw)

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

                 reply	other threads:[~2016-05-22 11:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160522111203.26193-1-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).