]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/hash-object: fix uninitialized hash function
authorPatrick Steinhardt <ps@pks.im>
Mon, 20 May 2024 23:14:33 +0000 (16:14 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 May 2024 16:05:13 +0000 (09:05 -0700)
The git-hash-object(1) command allows users to hash an object even
without a repository. Starting with c8aed5e8da (repository: stop setting
SHA1 as the default object hash, 2024-05-07), this will make us hit an
uninitialized hash function, which subsequently leads to a segfault.

Fix this by falling back to SHA-1 explicitly when running outside of
a Git repository. Users can use GIT_DEFAULT_HASH environment to
specify what hash algorithm they want, so arguably this code should
not be needed.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/hash-object.c
t/t1007-hash-object.sh
t/t1517-outside-repo.sh

index 82ca6d2bfdc7797a13646ff01271ceb803b0a973..c767414a0cc6ca995df001ecb9f621959f892bc6 100644 (file)
@@ -123,6 +123,9 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
        else
                prefix = setup_git_directory_gently(&nongit);
 
+       if (nongit && !the_hash_algo)
+               repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+
        if (vpath && prefix) {
                vpath_free = prefix_filename(prefix, vpath);
                vpath = vpath_free;
index 64aea3848606ccda03b5dd82f878e8d1e8351706..d73a5cc23705e2b697d56f961b27a6815725f8c4 100755 (executable)
@@ -260,4 +260,10 @@ test_expect_success '--literally with extra-long type' '
        echo example | git hash-object -t $t --literally --stdin
 '
 
+test_expect_success '--stdin outside of repository (uses SHA-1)' '
+       nongit git hash-object --stdin <hello >actual &&
+       echo "$(test_oid --hash=sha1 hello)" >expect &&
+       test_cmp expect actual
+'
+
 test_done
index 278ef57b3ab6cd8ad88342f1dce42151474f005a..2d8982d61a2373bff1127ecd4181f854e0137f7a 100755 (executable)
@@ -29,7 +29,7 @@ test_expect_success 'compute a patch-id outside repository (uses SHA-1)' '
        test_cmp patch-id.expect patch-id.actual
 '
 
-test_expect_failure 'hash-object outside repository (uses SHA-1)' '
+test_expect_success 'hash-object outside repository (uses SHA-1)' '
        nongit env GIT_DEFAULT_HASH=sha1 \
                git hash-object --stdin <sample.patch >hash.expect &&
        nongit \