# [PATCH] remote.c: free previous results when looking for a ref match

3 messages from 2016-10-08 to 2016-10-10. Participants: Stefan Beller, René Scharfe.
Thread: https://gitlist.dev/t/44250

## Stefan Beller, 2016-10-07 23:58

Subject: [PATCH] remote.c: free previous results when looking for a ref match
Message-ID: <20161007235857.20382-1-sbeller@google.com>
URL: https://gitlist.dev/e/20161007235857.20382-1-sbeller%40google.com

```
Signed-off-by: Stefan Beller <sbeller@google.com>
---
 remote.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/remote.c b/remote.c
index ad6c542..5f9afb4 100644
--- a/remote.c
+++ b/remote.c
@@ -833,6 +833,8 @@ static int match_name_with_pattern(const char *key, const char *name,
 		strbuf_add(&sb, value, vstar - value);
 		strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
 		strbuf_addstr(&sb, vstar + 1);
+		if (*result)
+			free(*result);
 		*result = strbuf_detach(&sb, NULL);
 	}
 	return ret;
@@ -1262,6 +1264,8 @@ static char *get_ref_match(const struct refspec *rs, int rs_nr, const struct ref
 		 */
 		if (!send_mirror && !starts_with(ref->name, "refs/heads/"))
 			return NULL;
+		if (name)
+			free(name);
 		name = xstrdup(ref->name);
 	}
 	if (ret_pat)
-- 
2.10.1.353.g1629400


```

## René Scharfe, 2016-10-08 13:38

Subject: Re: [PATCH] remote.c: free previous results when looking for a ref match
Message-ID: <e9f58e04-a534-6794-feac-f6a47ca8bb65@web.de>
URL: https://gitlist.dev/e/e9f58e04-a534-6794-feac-f6a47ca8bb65%40web.de
In-Reply-To: <20161007235857.20382-1-sbeller@google.com>

```
Am 08.10.2016 um 01:58 schrieb Stefan Beller:
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  remote.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/remote.c b/remote.c
> index ad6c542..5f9afb4 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -833,6 +833,8 @@ static int match_name_with_pattern(const char *key, const char *name,
>  		strbuf_add(&sb, value, vstar - value);
>  		strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
>  		strbuf_addstr(&sb, vstar + 1);
> +		if (*result)
> +			free(*result);

free(3) can handle NULL pointers; this check is not necessary.

Is it wise to release memory for callers?  I'd expect them to be 
responsible for that.  Some of them can pass uninitialized pointers; 
this is not allowed anymore after the change.

>  		*result = strbuf_detach(&sb, NULL);
>  	}
>  	return ret;
> @@ -1262,6 +1264,8 @@ static char *get_ref_match(const struct refspec *rs, int rs_nr, const struct ref
>  		 */
>  		if (!send_mirror && !starts_with(ref->name, "refs/heads/"))
>  			return NULL;
> +		if (name)
> +			free(name);

Again, this check is not necessary.  If I read the code correctly the 
pointer could be uninitialized at that point, though, causing free(3) to 
crash.

>  		name = xstrdup(ref->name);
>  	}
>  	if (ret_pat)
>


```

## Stefan Beller, 2016-10-10 17:03

Subject: Re: [PATCH] remote.c: free previous results when looking for a ref match
Message-ID: <CAGZ79kZ3n8iNqLrNA0WOMEs7zLTMHmLxc-WAo_fgj1uLQtwv5g@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kZ3n8iNqLrNA0WOMEs7zLTMHmLxc-WAo_fgj1uLQtwv5g%40mail.gmail.com
In-Reply-To: <e9f58e04-a534-6794-feac-f6a47ca8bb65@web.de>

```
On Sat, Oct 8, 2016 at 6:38 AM, René Scharfe <l.s.r@web.de> wrote:

>
> Again, this check is not necessary.  If I read the code correctly the
> pointer could be uninitialized at that point, though, causing free(3) to
> crash.

yep, this patch is bogus.

```
