]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: handle NULL return from repo_get_commit_tree()
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 10 Jul 2026 11:39:26 +0000 (11:39 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Jul 2026 15:13:54 +0000 (08:13 -0700)
The `repo_get_commit_tree()` function can return NULL when a commit's
tree object is not available (e.g., the commit was parsed but its
maybe_tree field is unset and the commit is not in the commit-graph). In
cmd_diff(), the return value is immediately dereferenced via ->object
without a NULL check, which would crash if the tree cannot be loaded.

Add an explicit NULL check and die with a descriptive message.

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>
builtin/diff.c

index 4b46e394cecb8dc4679a000d73c659394724dcbf..18b1083e984a3568c2745fb023996d4016e5928e 100644 (file)
@@ -579,9 +579,13 @@ int cmd_diff(int argc,
                obj = deref_tag(the_repository, obj, NULL, 0);
                if (!obj)
                        die(_("invalid object '%s' given."), name);
-               if (obj->type == OBJ_COMMIT)
-                       obj = &repo_get_commit_tree(the_repository,
-                                                   ((struct commit *)obj))->object;
+               if (obj->type == OBJ_COMMIT) {
+                       struct tree *tree = repo_get_commit_tree(
+                               the_repository, (struct commit *)obj);
+                       if (!tree)
+                               die(_("unable to read tree object for commit '%s'"), name);
+                       obj = &tree->object;
+               }
 
                if (obj->type == OBJ_TREE) {
                        if (sdiff.skip && bitmap_get(sdiff.skip, i))