{"thread":{"id":"9852","subject":"[PATCH 2/2] Backup the array passed to fetch_pack so we can free items","startedAt":"2007-09-14T22:57:11Z","lastAt":"2007-09-14T22:57:11Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"53079","messageId":"20070914225711.GB16512@spearce.org","threadId":"9852","inReplyTo":null,"subject":"[PATCH 2/2] Backup the array passed to fetch_pack so we can free items","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-09-14T22:57:11Z","receivedAt":"2007-09-14T22:57:11Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"fetch_pack() can call remove_duplicates() on its input array and\nthis will possibly overwrite an earlier entry with a later one if\nthere are any duplicates in the input array.  In such a case the\ncaller here might then attempt to free an item multiple times as\nit goes through its cleanup.\n\nI also forgot to free the heads array we pass down into fetch_pack()\nwhen I introduced the allocation of it in this function during my\nbuiltin-fetch cleanup series.  Better free it while we are here\nworking on related memory management fixes.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n transport.c |    7 +++++--\n 1 files changed, 5 insertions(+), 2 deletions(-)\n\ndiff --git a/transport.c b/transport.c\nindex 0882edd..0338ed4 100644\n--- a/transport.c\n+++ b/transport.c\n@@ -311,6 +311,7 @@ static int fetch_refs_via_pack(struct transport *transport,\n {\n \tstruct git_transport_data *data = transport->data;\n \tchar **heads = xmalloc(nr_heads * sizeof(*heads));\n+\tchar **origh = xmalloc(nr_heads * sizeof(*origh));\n \tstruct ref *refs;\n \tchar *dest = xstrdup(transport->url);\n \tstruct fetch_pack_args args;\n@@ -329,11 +330,13 @@ static int fetch_refs_via_pack(struct transport *transport,\n \tsetup_fetch_pack(&args);\n \n \tfor (i = 0; i < nr_heads; i++)\n-\t\theads[i] = xstrdup(to_fetch[i]->name);\n+\t\torigh[i] = heads[i] = xstrdup(to_fetch[i]->name);\n \trefs = fetch_pack(dest, nr_heads, heads, &transport->pack_lockfile);\n \n \tfor (i = 0; i < nr_heads; i++)\n-\t\tfree(heads[i]);\n+\t\tfree(origh[i]);\n+\tfree(origh);\n+\tfree(heads);\n \tfree_refs(refs);\n \tfree(dest);\n \treturn 0;\n-- \n1.5.3.1.921.g8c3b\n"}]}