"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
fix got_packidx_match_id_str_prefix()
To:
gameoftrees@openbsd.org
Date:
Mon, 3 Jan 2022 14:40:06 +0100

Download raw body.

Thread
An object ID which begins with "00" cannot be used from the command line.

$ got log -c 00b9e9a4562e4f0acdca9b52fe1de269d7f8ba94
got: commit 00b9e9a4562e4f0acdca9b52fe1de269d7f8ba94: object not found

$ got cat 00b9e9a4562e4f0acdca9b52fe1de269d7f8ba94
got: 00b9e9a4562e4f0acdca9b52fe1de269d7f8ba94: object not found

However, this commit object does indeed exist:

$ gotadmin ls .git/objects/pack/pack-3822a2686597f4ca864b9b18f8b5836af5407da2.pack | head -n1
00b9e9a4562e4f0acdca9b52fe1de269d7f8ba94 commit at 12 size 195

This bug triggered test failures in the following histedit.sh tests:

  test_histedit_fold_last_commit
  test_histedit_fold_missing_commit

You would need to be very lucky in order to reproduce this. The failures
cannot be easily repeated because timestamps factor into the commit hash.
I saw these failures scroll by while working on a new revision of my pack
temp file patch.

A fix is below. When the object ID starts with a 0 we can simply search
the pack index from the beginning, and avoid indexing the array with a
negative number.

ok?

diff a8a244dd9f028cb4a3a7f05a51ec1bdcb96f3195 /home/stsp/src/got
blob - b23b1379d56dac2d5b31ae48e64080a8c5cb768f
file + lib/pack.c
--- lib/pack.c
+++ lib/pack.c
@@ -523,7 +523,7 @@ got_packidx_match_id_str_prefix(struct got_object_id_q
 	char hex[3];
 	size_t prefix_len = strlen(id_str_prefix);
 	struct got_packidx_object_id *oid;
-	uint32_t i;
+	uint32_t i = 0;
 
 	STAILQ_INIT(matched_ids);
 
@@ -536,7 +536,8 @@ got_packidx_match_id_str_prefix(struct got_object_id_q
 	if (!got_parse_xdigit(&id0, hex))
 		return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
 
-	i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
+	if (id0 > 0)
+		i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
 	oid = &packidx->hdr.sorted_ids[i];
 	while (i < totobj && oid->sha1[0] == id0) {
 		char id_str[SHA1_DIGEST_STRING_LENGTH];