git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org, johannes.schindelin@gmx.net,
	Derrick Stolee <dstolee@microsoft.com>
Subject: Re: [PATCH] unpack-trees: correctly compute result count
Date: Fri, 10 Jan 2020 11:33:27 -0800	[thread overview]
Message-ID: <xmqqftgnm7i0.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20200110103729.GA470836@coredump.intra.peff.net> (Jeff King's message of "Fri, 10 Jan 2020 05:37:29 -0500")

Jeff King <peff@peff.net> writes:

> On Fri, Jan 10, 2020 at 01:59:30AM +0000, Derrick Stolee via GitGitGadget wrote:
>
>>     Here is a very small fix to the cone-mode pattern-matching in
>>     unpack-trees.c. Johannes found this while running a Coverity scan for
>>     other issues. He alerted me to the problem and I could immediately see 
>>     my error here. In creating this patch, most of my time was spent asking
>>     "how did this work before?" and "why didn't this hurt performance?"
>>     Hopefully my commit message explains this thoroughly enough.
>
> Yes, it makes perfect sense (and as soon as I saw the explanation of the
> problem, my immediate response was also "wait, how did this even work").
>
> And the patch itself looks good.
>
>>     As for making it into the release, I don't know. The change is small, it
>>     has a very limited scope, but this flaw is also not really hurting
>>     anything in a major way.
>
> I could go either way.
>
> This counts as something small and obvious enough that I'd consider
> slipping it in at the last minute if it were fixing a bad bug. But given
> how minor the bug is, being conservative makes sense to me, if only
> because it's good to exercise our release discipline muscles. :)

Heh.

On one hand, it is obvious, even to a mindless compiler, that the
author meant to count how many elements of cache[] array have been
processed in the loop, so it is clear that the patch makes the code
reflect the author's original intention better.

On the other hand, the code that reflects the author's original
intention has never been tested in the field---it could be possible
that the author thought cache[0] thru cache_end[0] have been
processed, but for some subtlety, only a very early part of the
range was correctly processed, and returning smaller range may have
been hiding that subtle bug ;-)

I do not see any such subtle bug in this particular case, so I am
somewhat tempted to say "it is clear that the 'fix' makes the code
do what the author wanted to do, *and* what the author wanted to do
seems sane, so let's apply it".

Having said that, the earlier part of the patch, i.e.

 	if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) {
 		struct cache_entry **ce = cache;
-		rc = (cache_end - cache) / sizeof(struct cache_entry *);
+		rc = cache_end - cache;
 
 		while (ce < cache_end) {
 			(*ce)->ce_flags &= ~clear_mask;
 			ce++;
 		}

wouldn't have been needed any fix if it were actually *counting*,
which is what clear_ce_flags_dir() promises its callers it does,
instead of cheating with a subtraction. i.e.

	if (... RECURSIVE) {
		rc = 0;
		while (ce < cache_end) {
			clear;
			ce++;
			rc++;
		}
	}

and that may have been more future-proof way, in that the body of
the while loop may in the future decide to leave early etc. and
actually counting how far the processing progressed would be less
error prone.

ALso, the computation of cache_end earlier in the function looks
suspiciously similar to the code Emily recently touched to avoid
segfaulting against inconsistent index state, where it used
the entry count in the cache_tree structure (when it is valid) to
determine how many entries to skip over.  We may want to see if we
can apply the same optimization here.

Thanks.  Will queue but will not merge to 'master' ;-)

      parent reply	other threads:[~2020-01-10 19:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-10  1:59 [PATCH] unpack-trees: correctly compute result count Derrick Stolee via GitGitGadget
2020-01-10 10:37 ` Jeff King
2020-01-10 11:24   ` Derrick Stolee
2020-01-10 11:30     ` Derrick Stolee
2020-01-10 19:33   ` Junio C Hamano [this message]

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=xmqqftgnm7i0.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=dstolee@microsoft.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=johannes.schindelin@gmx.net \
    --cc=peff@peff.net \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).