Subject: Re: [PATCH 1/4] filter_ref: avoid overwriting ref->old_sha1 with garbage

From: Junio C Hamano <[email-masked]>
Date: Thu, 19 Mar 2015 14:09:44 -0700

Jeff King <[email-masked]> writes:

> I wonder if the thinking in the original was that it was our
> responsibility here to make sure that ref->old_sha1 was filled in.

I am reasonably sure that is the (perhaps mistaken) reasoning behind
the use of old_sha1 as the second parameter to get_sha1_hex().

> It is
> always filled in by the caller who gives us "sought", which makes sense
> to me (this matches the rest of the "sought" entries, which come from
> reading the remote's ref list, and of course must fill in old_sha1 from
> that list).

I see that sought is populated by reading the command line of "git
fetch-pack", and for a 40-hex request ref->old_sha1 is already
filled there, so I agree that it is redundant to try filling it in
filter_refs().

Thanks.

>  fetch-pack.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/fetch-pack.c b/fetch-pack.c
> index 655ee64..058c258 100644
> --- a/fetch-pack.c
> +++ b/fetch-pack.c
> @@ -544,10 +544,14 @@ static void filter_refs(struct fetch_pack_args *args,
>  	/* Append unmatched requests to the list */
>  	if (allow_tip_sha1_in_want) {
>  		for (i = 0; i < nr_sought; i++) {
> +			unsigned char sha1[20];
> +
>  			ref = sought[i];
>  			if (ref->matched)
>  				continue;
> -			if (get_sha1_hex(ref->name, ref->old_sha1))
> +			if (get_sha1_hex(ref->name, sha1) ||
> +			    ref->name[40] != '\0' ||
> +			    hashcmp(sha1, ref->old_sha1))
>  				continue;
>  
>  			ref->matched = 1;