git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] ref-filter: free item->value and item->value->s

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 11, 2018, 01:19 UTC
Message-ID
<xmqqzhvli8kl.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<0102016657e7d087-be1f65a3-be3e-4b15-95f1-7570d258c70d-000000@eu-west-1.amazonses.com>
Olga Telezhnaya <olyatelezhnaya@gmail.com> writes:
> Release item->value.
> Initialize item->value->s dynamically and then release its resources.
> Release some local variables.
Again, "why" is lacking.
Show 8 quoted lines
> @@ -1373,36 +1379,31 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
>  		}
>  	} else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
>  		if (stat_tracking_info(branch, &num_ours, &num_theirs,
> -				       NULL, AHEAD_BEHIND_FULL) < 0)
> +				       NULL, AHEAD_BEHIND_FULL) < 0) {
> +			*s = xstrdup("");
>  			return;

It is a bit sad that we need to sprinkle xstrdup() all over the place, but I do not offhand think of a better alternative to ensure that it is safe to blindly free the .s field.

Show 5 quoted lines
> -		if (explicit)
> -			*s = xstrdup(remote);
> -		else
> -			*s = "";
> +		*s = explicit ? xstrdup(remote) : xstrdup("");

Next time, please avoid mixing this kind of unrelated changes with the main theme (i.e. "the original had allocated and static pieces of memory pointed by the same variable, which made it impossible to blindly free it, so make sure everything is allocated"). It makes it harder to let reviewers' eyes coast over the patch.

I say "Next time" because the change is already written this time, and I already spent time to see it was an OK change. By the way,

	*s = xstrdup(explicit ? remote : "");
is probably shorter.
Show 5 quoted lines
> @@ -1562,10 +1566,11 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
>  				if (!refname)
>  					continue;
>  			}
> +			free((char *)v->s); // we will definitely re-init it on the next line
No // comment, please.
Show 9 quoted lines
>  static void free_array_item(struct ref_array_item *item)
>  {
>  	free((char *)item->symref);
> +	if (item->value) {
> +		free((char *)item->value->s);
> +		free(item->value);
> +	}
>  	free(item);
>  }
OK.
Previous: Olga TelezhnayaNext: Jeff King
Message 5 of 11 in “ref-filter: free memory from used_atom”
  1. 1/3 ref-filter: free memory from used_atomOlga Telezhnaya, Oct 9, 2018
  2. 2/3 ls-remote: release memory instead of UNLEAKOlga Telezhnaya, Oct 9, 2018
  3. Junio C HamanoOct 11, 2018
  4. 3/3 ref-filter: free item->value and item->value->sOlga Telezhnaya, Oct 9, 2018
  5. Junio C HamanoOct 11, 2018
  6. Jeff KingOct 12, 2018
  7. Junio C HamanoOct 11, 2018
  8. Junio C HamanoOct 12, 2018
  9. Jeff KingOct 18, 2018
  10. Junio C HamanoOct 18, 2018
  11. Оля ТележнаяOct 18, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.