]> git.ipfire.org Git - thirdparty/git.git/commitdiff
shallow: fix NULL dereference
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 10 Jul 2026 11:39:35 +0000 (11:39 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Jul 2026 15:13:55 +0000 (08:13 -0700)
After `write_one_shallow()` calls `lookup_commit()` to find the commit
object for a shallow graft entry, it then checks `if (!c || ...)`.
Inside that block, when the VERBOSE flag is set, it prints the OID being
removed, via `c->object.oid`. But `c` can be NULL (the first condition
in the `||` check).

This happens when a shallow graft entry references a commit object that
is not in the object store (e.g., after a partial fetch or in a
corrupted repository). In that case, `lookup_commit()` returns NULL
because the object cannot be found, the SEEN_ONLY check correctly
decides to remove this entry from .git/shallow, but the verbose message
crashes before the removal can complete.

Use `graft->oid` instead of `c->object.oid` for the message. The graft
entry's OID is the same value (it was used as the lookup key) and is
always available regardless of whether the commit object exists.

Pointed out by Coverity.

Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
shallow.c

index 07cae44ae52a042ce17c87e82e8a2f80e387e94d..2f96db5170bcc870ce0660940f3a2d7c33c3d969 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -370,8 +370,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
                struct commit *c = lookup_commit(the_repository, &graft->oid);
                if (!c || !(c->object.flags & SEEN)) {
                        if (data->flags & VERBOSE)
-                               printf("Removing %s from .git/shallow\n",
-                                      oid_to_hex(&c->object.oid));
+                               printf("Removing %s from .git/shallow\n", hex);
                        return 0;
                }
        }