Subject: Re: [PATCH] commit-reach: avoid NULL dereference

From: Junio C Hamano <[email-masked]>
Date: Sat, 11 Feb 2023 14:43:17 -0800

Eric Wong <[email-masked]> writes:

>  Not sure if somebody who understands the code better can come
>  up with a good standalone test case.  I figure using the top
>  loop as reference is sufficient evidence that this fix is needed.

Good comment.

>  commit-reach.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/commit-reach.c b/commit-reach.c
> index 2e33c599a82..1d7056338b7 100644
> --- a/commit-reach.c
> +++ b/commit-reach.c
> @@ -807,8 +807,12 @@ int can_all_from_reach_with_flag(struct object_array *from,
>  	clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
>  	free(list);
>  
> -	for (i = 0; i < from->nr; i++)
> -		from->objects[i].item->flags &= ~assign_flag;
> +	for (i = 0; i < from->nr; i++) {
> +		struct object *from_one = from->objects[i].item;
> +
> +		if (from_one)
> +			from_one->flags &= ~assign_flag;
> +	}

The flag clearing rule of this function smells somewhat iffy.  There
are three primary callers of the function:

 * commit-reach.c::can_all_from_reach() calls the function, but it
   has its own loop to clear the flag it asked the function to add.
   If the function uses the flag as a temporary mark and is designed
   to clear it from all the objects, as 4067a646 (commit-reach: fix
   memory and flag leaks, 2018-09-21) states, why should the caller
   have a separate loop to clear them?

 * fetch-pack.c::negotiate_using_fetch() calls this function in a
   loop, so it does depend on it to clear the flag upon returning.

 * upload-pack.c::ok_to_give_up() is a thin wrapper around this
   function and none callers of it have any logic to clear flag, so
   it clearly depends on the function to clear the flag.

The above seems to indicate that the expectation by callers is a bit
uneven.  Shouldn't the first onetrust the callee to clear the flag?

Even before 4067a646 (commit-reach: fix memory and flag leaks,
2018-09-21), the function had a call to clear_commit_marks() to
clear two bits it used temporarily.  The reason why 4067a646 needed
to add this additional flag clearing, whose NULL-dereference bug is
being fixed with the patch in this thread, is because it marks any
incoming object that peels to a non-commit (e.g. a blob, a tree, or
a tag that points at a non-commit) with the flag bit, but such a
non-commit object is not added to the list[] of commits to be
processed, before the main processing of this function.

		from_one = deref_tag(the_repository, from_one,
				     "a from object", 0);
		if (!from_one || from_one->type != OBJ_COMMIT) {
			/*
			 * no way to tell if this is reachable by
			 * looking at the ancestry chain alone, so
			 * leave a note to ourselves not to worry about
			 * this object anymore.
			 */
			from->objects[i].item->flags |= assign_flag;
			continue;
		}

		list[nr_commits] = (struct commit *)from_one;

But I am not sure if it is even necessary to smudge the flag for the
object that was a non-commit (or the tag that peeled down to a
non-commit).  The main process of this function is a history
traversal that stops when the "assign_flag" bit is already set on
the found object, but the object that was part of the incoming
objects (i.e. in from->objects[] array) that turned out not to be a
non-commit would not be discovered during this history walk, would
it?  In other words, if we walk from list[] that is an array or
commits to the parents (but not its trees and blobs), we won't
encounter anything but commit.  What does it help to smudge an
object that peeled down to a non-commit in the incoming set of
objects, if it would not appear in the walk from list[]?  It would
not stop the traversal by having the flag.

So I wonder if we can just stop smudging the assign_flag bit for
these objects in from->objects[] that do not make it into list[]
as a simpler fix?  Wouldn't that make the follow-up cleaning loop
added by 4067a646 (commit-reach: fix memory and flag leaks,
2018-09-21) unneeded?