git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/4] fsck: check rev-index position values

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Apr 17, 2023, 16:21 UTC
Message-ID
<adbe9c8ee90e087e864bd9e0c67338974b5dbc8d.1681748502.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1512.git.1681748502.gitgitgadget@gmail.com>
From: Derrick Stolee <derrickstolee@github.com>

When checking a rev-index file, it may be helpful to identify exactly which positions are incorrect. Compare the rev-index to a freshly-computed in-memory rev-index and report the comparison failures.

This additional check (on top of the checksum validation) can help find files that were corrupt by a single bit flip on-disk or perhaps were written incorrectly due to a bug in Git.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 pack-revindex.c          | 25 +++++++++++++++++++++----
 t/t5325-reverse-index.sh |  5 +++++
 2 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/pack-revindex.c b/pack-revindex.c
index 007a806994f..62a9846470c 100644
--- a/pack-revindex.c
+++ b/pack-revindex.c
@@ -310,16 +310,33 @@ int load_pack_revindex(struct repository *r, struct packed_git *p)
  */
 int verify_pack_revindex(struct packed_git *p)
 {
+	int res = 0;
+
 	/* Do not bother checking if not initialized. */
-	if (!p->revindex_map)
-		return 0;
+	if (!p->revindex_map || !p->revindex_data)
+		return res;
 
 	if (!hashfile_checksum_valid((const unsigned char *)p->revindex_map, p->revindex_size)) {
 		error(_("invalid checksum"));
-		return -1;
+		res = -1;
 	}
 
-	return 0;
+	/* This may fail due to a broken .idx. */
+	if (create_pack_revindex_in_memory(p))
+		return res;
+
+	for (size_t i = 0; i < p->num_objects; i++) {
+		uint32_t nr = p->revindex[i].nr;
+		uint32_t rev_val = get_be32(p->revindex_data + i);
+
+		if (nr != rev_val) {
+			error(_("invalid rev-index position at %"PRIu64": %"PRIu32" != %"PRIu32""),
+			      (uint64_t)i, nr, rev_val);
+			res = -1;
+		}
+	}
+
+	return res;
 }
 
 int load_midx_revindex(struct multi_pack_index *m)
diff --git a/t/t5325-reverse-index.sh b/t/t5325-reverse-index.sh
index 6b7c709a1f6..5c3c80f88f0 100755
--- a/t/t5325-reverse-index.sh
+++ b/t/t5325-reverse-index.sh
@@ -185,4 +185,9 @@ test_expect_success 'fsck catches invalid checksum' '
 		"invalid checksum"
 '
 
+test_expect_success 'fsck catches invalid row position' '
+	corrupt_rev_and_verify 14 "\07" \
+		"invalid rev-index position"
+'
+
 test_done
-- 
gitgitgadget
Previous: Taylor BlauNext: Junio C Hamano
Message 12 of 19 in “git fsck: check pack rev-index files”
  1. 0/4 git fsck: check pack rev-index filesDerrick Stolee via GitGitGadget, Apr 17, 2023
  2. 1/4 fsck: create scaffolding for rev-index checksDerrick Stolee via GitGitGadget, Apr 17, 2023
  3. Taylor BlauApr 17, 2023
  4. 2/4 fsck: check rev-index checksumsDerrick Stolee via GitGitGadget, Apr 17, 2023
  5. Junio C HamanoApr 17, 2023
  6. Derrick StoleeApr 18, 2023
  7. Taylor BlauApr 17, 2023
  8. Derrick StoleeApr 18, 2023
  9. Taylor BlauApr 18, 2023
  10. Derrick StoleeApr 18, 2023
  11. Taylor BlauApr 18, 2023
  12. 3/4 fsck: check rev-index position valuesDerrick Stolee via GitGitGadget, Apr 17, 2023
  13. Junio C HamanoApr 17, 2023
  14. Derrick StoleeApr 18, 2023
  15. Taylor BlauApr 17, 2023
  16. 4/4 fsck: validate .rev file headerDerrick Stolee via GitGitGadget, Apr 17, 2023
  17. Junio C HamanoApr 17, 2023
  18. Taylor BlauApr 18, 2023
  19. Junio C HamanoApr 18, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.