git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Eric Wong <e@80x24.org>
Cc: git@vger.kernel.org, Derrick Stolee <derrickstolee@github.com>
Subject: Re: [PATCH] commit-reach: avoid NULL dereference
Date: Sat, 11 Feb 2023 14:43:17 -0800	[thread overview]
Message-ID: <xmqqcz6fesca.fsf@gitster.g> (raw)
In-Reply-To: <20230211111526.2028178-1-e@80x24.org> (Eric Wong's message of "Sat, 11 Feb 2023 11:15:26 +0000")

Eric Wong <e@80x24.org> 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?






  reply	other threads:[~2023-02-11 22:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-11 11:15 [PATCH] commit-reach: avoid NULL dereference Eric Wong
2023-02-11 22:43 ` Junio C Hamano [this message]
2023-02-13 13:58   ` Derrick Stolee
2023-02-13 17:29     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqcz6fesca.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=derrickstolee@github.com \
    --cc=e@80x24.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).