All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Taylor Blau <me@ttaylorr.com>, Derrick Stolee <dstolee@microsoft.com>
Subject: [PATCH v2 5/6] commit-graph: tighten chain size check
Date: Thu, 28 Sep 2023 00:39:10 -0400	[thread overview]
Message-ID: <20230928043910.GE58367@coredump.intra.peff.net> (raw)
In-Reply-To: <20230928043746.GB57926@coredump.intra.peff.net>

When we open a commit-graph-chain file, if it's smaller than a single
entry, we just quietly treat that as ENOENT. That make some sense if the
file is truly zero bytes, but it means that "commit-graph verify" will
quietly ignore a file that contains garbage if that garbage happens to
be short.

Instead, let's only simulate ENOENT when the file is truly empty, and
otherwise return EINVAL. The normal graph-loading routines don't care,
but "commit-graph verify" will notice and complain about the difference.

It's not entirely clear to me that the 0-is-ENOENT case actually happens
in real life, so we could perhaps just eliminate this special-case
altogether. But this is how we've always behaved, so I'm preserving it
in the name of backwards compatibility (though again, it really only
matters for "verify", as the regular routines are happy to load what
they can).

Signed-off-by: Jeff King <peff@peff.net>
---
 commit-graph.c                | 10 ++++++++--
 t/t5324-split-commit-graph.sh | 12 ++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index 8b29c6de24..b1d3e5bf94 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -548,9 +548,15 @@ int open_commit_graph_chain(const char *chain_file,
 		close(*fd);
 		return 0;
 	}
-	if (st->st_size <= the_hash_algo->hexsz) {
+	if (st->st_size < the_hash_algo->hexsz) {
 		close(*fd);
-		errno = ENOENT;
+		if (!st->st_size) {
+			/* treat empty files the same as missing */
+			errno = ENOENT;
+		} else {
+			warning("commit-graph chain file too small");
+			errno = EINVAL;
+		}
 		return 0;
 	}
 	return 1;
diff --git a/t/t5324-split-commit-graph.sh b/t/t5324-split-commit-graph.sh
index a5ac0440f1..be58545810 100755
--- a/t/t5324-split-commit-graph.sh
+++ b/t/t5324-split-commit-graph.sh
@@ -342,6 +342,18 @@ test_expect_success 'verify after commit-graph-chain corruption (tip)' '
 	)
 '
 
+test_expect_success 'verify notices too-short chain file' '
+	git clone --no-hardlinks . verify-chain-short &&
+	(
+		cd verify-chain-short &&
+		git commit-graph verify &&
+		echo "garbage" >$graphdir/commit-graph-chain &&
+		test_must_fail git commit-graph verify 2>test_err &&
+		grep -v "^+" test_err >err &&
+		grep "commit-graph chain file too small" err
+	)
+'
+
 test_expect_success 'verify across alternates' '
 	git clone --no-hardlinks . verify-alt &&
 	(
-- 
2.42.0.773.ga6e30199be


  parent reply	other threads:[~2023-09-28  4:39 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-26  5:54 [PATCH 0/6] some "commit-graph verify" fixes for chains Jeff King
2023-09-26  5:56 ` [PATCH 1/6] commit-graph: factor out chain opening function Jeff King
2023-09-26  5:57 ` [PATCH 2/6] commit-graph: check mixed generation validation when loading chain file Jeff King
2023-09-26  5:58 ` [PATCH 3/6] t5324: harmonize sha1/sha256 graph chain corruption Jeff King
2023-09-26  6:00 ` [PATCH 4/6] commit-graph: detect read errors when verifying graph chain Jeff King
2023-09-26  6:03 ` [PATCH 5/6] commit-graph: tighten chain size check Jeff King
2023-09-26  6:04 ` [PATCH 6/6] commit-graph: report incomplete chains during verification Jeff King
2023-09-28  4:30   ` Jeff King
2023-09-28  4:37 ` [PATCH v2 0/6] some "commit-graph verify" fixes for chains Jeff King
2023-09-28  4:38   ` [PATCH v2 1/6] commit-graph: factor out chain opening function Jeff King
2023-09-28  4:38   ` [PATCH v2 2/6] commit-graph: check mixed generation validation when loading chain file Jeff King
2023-09-28  4:38   ` [PATCH v2 3/6] t5324: harmonize sha1/sha256 graph chain corruption Jeff King
2023-09-28  4:38   ` [PATCH v2 4/6] commit-graph: detect read errors when verifying graph chain Jeff King
2023-09-28  4:39   ` Jeff King [this message]
2023-09-28  4:39   ` [PATCH v2 6/6] commit-graph: report incomplete chains during verification Jeff King
2023-10-05 17:38   ` [PATCH v2 0/6] some "commit-graph verify" fixes for chains Taylor Blau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230928043910.GE58367@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=dstolee@microsoft.com \
    --cc=git@vger.kernel.org \
    --cc=me@ttaylorr.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.