hash-object: fix buffer reuse with --path in a subdirectory
authorJeff King <peff@peff.net>
Tue, 21 Mar 2017 01:20:42 +0000 (21:20 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Mar 2017 18:12:52 +0000 (11:12 -0700)
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>
builtin/hash-object.c
t/t1007-hash-object.sh
index 9028e1fdccea2ad44a76792adc6e335fb44cfb5c..56df77b0c2a299fcba56a2542f7adb34869ced9f 100644 (file)
@@ -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)
index c5245c5cb4c1c086b6fe7205f34b9b5ca53e2e59..532682f51c4f9cefc34a44cf7e7f4231c60b7f65 100755 (executable)
@@ -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" &&