From: Johannes Schindelin Date: Fri, 10 Jul 2026 11:39:26 +0000 (+0000) Subject: diff: handle NULL return from repo_get_commit_tree() X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=d07d09ee43b920f17d00a96ee29621e398136218;p=thirdparty%2Fgit.git diff: handle NULL return from repo_get_commit_tree() 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 Signed-off-by: Junio C Hamano --- diff --git a/builtin/diff.c b/builtin/diff.c index 4b46e394ce..18b1083e98 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -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))