summaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorLibravatar Jeff King <peff@peff.net>2017-03-20 21:20:42 -0400
committerLibravatar Junio C Hamano <gitster@pobox.com>2017-03-21 11:12:52 -0700
commita1be47e4ca65718ec239e4b86a44e45220237aee (patch)
tree688e23ca3b924b229dbc2a8a94fdf4dab88ae294 /builtin
parentSync with 2.12.1 (diff)
downloadtgif-a1be47e4ca65718ec239e4b86a44e45220237aee.tar.xz
hash-object: fix buffer reuse with --path in a subdirectory
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/hash-object.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/builtin/hash-object.c b/builtin/hash-object.c
index 9028e1fdcc..56df77b0c2 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)