git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 1/6] hash-object: fix buffer reuse with --path in a subdirectory
Date: Mon, 20 Mar 2017 21:20:42 -0400	[thread overview]
Message-ID: <20170321012042.jb5biz2min7a34gu@sigill.intra.peff.net> (raw)
In-Reply-To: <20170321011838.rdhnbfwbigm4s4e3@sigill.intra.peff.net>

The hash-object command uses prefix_filename() without
duplicating its return value. Since that function returns a
static buffer, the value is overwritten by subsequent calls.

This can cause incorrect results when we use --path along
with hashing a file by its relative path, both of which need
to call prefix_filename(). We overwrite the filename
computed for --path, effectively ignoring it.

We can fix this by calling xstrdup on the return value. Note
that we don't bother freeing the "vpath" instance, as it
remains valid until the program exit.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/hash-object.c  |  7 +++++--
 t/t1007-hash-object.sh | 10 ++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/builtin/hash-object.c b/builtin/hash-object.c
index 9028e1fdc..56df77b0c 100644
--- a/builtin/hash-object.c
+++ b/builtin/hash-object.c
@@ -115,7 +115,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
 
 	prefix_length = prefix ? strlen(prefix) : 0;
 	if (vpath && prefix)
-		vpath = prefix_filename(prefix, prefix_length, vpath);
+		vpath = xstrdup(prefix_filename(prefix, prefix_length, vpath));
 
 	git_config(git_default_config, NULL);
 
@@ -144,11 +144,14 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
 
 	for (i = 0 ; i < argc; i++) {
 		const char *arg = argv[i];
+		char *to_free = NULL;
 
 		if (0 <= prefix_length)
-			arg = prefix_filename(prefix, prefix_length, arg);
+			arg = to_free =
+				xstrdup(prefix_filename(prefix, prefix_length, arg));
 		hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
 			    flags, literally);
+		free(to_free);
 	}
 
 	if (stdin_paths)
diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh
index c5245c5cb..532682f51 100755
--- a/t/t1007-hash-object.sh
+++ b/t/t1007-hash-object.sh
@@ -134,6 +134,16 @@ test_expect_success 'gitattributes also work in a subdirectory' '
 	)
 '
 
+test_expect_success '--path works in a subdirectory' '
+	(
+		cd subdir &&
+		path1_sha=$(git hash-object --path=../file1 ../file0) &&
+		path0_sha=$(git hash-object --path=../file0 ../file1) &&
+		test "$file0_sha" = "$path0_sha" &&
+		test "$file1_sha" = "$path1_sha"
+	)
+'
+
 test_expect_success 'check that --no-filters option works' '
 	nofilters_file1=$(git hash-object --no-filters file1) &&
 	test "$file0_sha" = "$nofilters_file1" &&
-- 
2.12.1.683.gcd02edfec


  reply	other threads:[~2017-03-21  1:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-21  1:18 [PATCH 0/6] prefix_filename cleanups Jeff King
2017-03-21  1:20 ` Jeff King [this message]
2017-03-21  1:21 ` [PATCH 2/6] prefix_filename: move docstring to header file Jeff King
2017-03-21  1:22 ` [PATCH 3/6] prefix_filename: drop length parameter Jeff King
2017-03-21  1:28 ` [PATCH 4/6] prefix_filename: return newly allocated string Jeff King
2017-03-21 18:14   ` Junio C Hamano
2017-03-21 18:23     ` Jeff King
2017-03-21 23:32       ` Jeff King
2017-03-21  1:30 ` [PATCH 5/6] prefix_filename: simplify windows #ifdef Jeff King
2017-03-21  1:31 ` [PATCH 6/6] bundle: use prefix_filename with bundle path Jeff King
2017-03-21 17:53 ` [PATCH 0/6] prefix_filename cleanups Junio C Hamano

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=20170321012042.jb5biz2min7a34gu@sigill.intra.peff.net \
    --to=peff@peff.net \
    --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).