Skip to content

Commit

Permalink
builtin/index-pack: fix segfaults when running outside of a repo
Browse files Browse the repository at this point in the history
It was reported that git-verify-pack(1) has started to crash with Git
v2.46.0 when run outside of a repository. This is another fallout from
c8aed5e (repository: stop setting SHA1 as the default object hash,
2024-05-07), where we have stopped setting the default hash algorithm
for `the_repository`. Consequently, code that relies on `the_hash_algo`
will now crash when it hasn't explicitly been initialized, which may be
the case when running outside of a Git repository.

The crash is not in git-verify-pack(1) but instead in git-index-pack(1),
which gets called by the former. Ideally, both of these programs should
be able to identify the hash algorithm used by the packfile and index
without having to rely on external information. But unfortunately, the
format for neither of them is completely self-describing, so it is not
possible to derive that information. This is a design issue that we
should address by introducing a new packfile version that encodes its
object hash.

For now though the more important fix is to not make either of these
programs crash anymore, which we do by falling back to SHA1 when the
object hash is unconfigured. This pessimizes reading packfiles which
use a different hash than SHA1, but restores previous behaviour.

Reported-by: Ilya K <me@0upti.me>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pks-t authored and gitster committed Sep 4, 2024
1 parent 39bf06a commit b2dbf97
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,15 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
if (!index_name && pack_name)
index_name = derive_filename(pack_name, "pack", "idx", &index_name_buf);

/*
* Packfiles and indices do not carry enough information to be able to
* identify their object hash. So when we are neither in a repository
* nor has the user told us which object hash to use we have no other
* choice but to guess the object hash.
*/
if (!the_repository->hash_algo)
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);

opts.flags &= ~(WRITE_REV | WRITE_REV_VERIFY);
if (rev_index) {
opts.flags |= verify ? WRITE_REV_VERIFY : WRITE_REV;
Expand Down
39 changes: 39 additions & 0 deletions t/t5300-pack-object.sh
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,43 @@ test_expect_success 'negative window clamps to 0' '
check_deltas stderr = 0
'

for hash in sha1 sha256
do
test_expect_success "verify-pack with $hash packfile" '
test_when_finished "rm -rf repo" &&
git init --object-format=$hash repo &&
test_commit -C repo initial &&
git -C repo repack -ad &&
git -C repo verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx &&
if test $hash = sha1
then
nongit git verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx
else
# We have no way to identify the hash used by packfiles
# or indices, so we always fall back to SHA1.
nongit test_must_fail git verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx &&
# But with an explicit object format we should succeed.
nongit git verify-pack --object-format=$hash "$(pwd)"/repo/.git/objects/pack/*.idx
fi
'

test_expect_success "index-pack outside of a $hash repository" '
test_when_finished "rm -rf repo" &&
git init --object-format=$hash repo &&
test_commit -C repo initial &&
git -C repo repack -ad &&
git -C repo index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack &&
if test $hash = sha1
then
nongit git index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack
else
# We have no way to identify the hash used by packfiles
# or indices, so we always fall back to SHA1.
nongit test_must_fail git index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack 2>err &&
# But with an explicit object format we should succeed.
nongit git index-pack --object-format=$hash --verify "$(pwd)"/repo/.git/objects/pack/*.pack
fi
'
done

test_done

0 comments on commit b2dbf97

Please sign in to comment.