t/helper: fix segfault in "dump-reftable -t"

The `test-tool dump-reftable` command can be used to dump the on-disk
contents of reftables. The "-t" subcommand specifically can be used to
dump a single table from disk.

When trying to use this subcommand though one will quickly realize that
it is broken, as it always segfaults. The root cause of this segfault is
that we try to detect the hash algorithm via the merged table's hash ID.
But that hash ID is not the same as Git's understanding of a hash ID,
and consequently we fail to look up the correct algorithm. This will
then lead to a segfault later on when we try to dereference a NULL
pointer.

This breakage went undetected until now because this particular
subcommand is not used anywhere in our test suite. So the obvious way to
fix the bug is by just removing the code outright. But in the next
commit we're about to add a user.

Fix the issue by properly converting between the two hash IDs.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
next
Patrick Steinhardt 2026-10-01 07:39:01 +02:00 committed by Junio C Hamano
parent c17b2d9f6b
commit f97936288d
1 changed files with 10 additions and 1 deletions

View File

@ -103,7 +103,16 @@ static int dump_table(struct reftable_merged_table *mt)
if (err < 0)
return err;

algop = &hash_algos[hash_algo_by_id(reftable_merged_table_hash_id(mt))];
switch (reftable_merged_table_hash_id(mt)) {
case REFTABLE_HASH_SHA1:
algop = &hash_algos[GIT_HASH_SHA1];
break;
case REFTABLE_HASH_SHA256:
algop = &hash_algos[GIT_HASH_SHA256];
break;
default:
die("unknown reftable hash function: %d", reftable_merged_table_hash_id(mt));
}

while (1) {
err = reftable_iterator_next_ref(&it, &ref);