]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit-graph: tighten chain size check
authorJeff King <peff@peff.net>
Thu, 28 Sep 2023 04:39:10 +0000 (00:39 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Sep 2023 14:00:43 +0000 (07:00 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
commit-graph.c
t/t5324-split-commit-graph.sh

index 8b29c6de24659f19121893b3b3d0a20a27b4e5e1..b1d3e5bf945d4fe6a079e3c1056c84d93b32cf2c 100644 (file)
@@ -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;
index a5ac0440f1a1ebb4fea97337cd536f51d6570287..be585458105cda2246c10aeedbae219b992bd028 100755 (executable)
@@ -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 &&
        (