git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Brandon Casey <bcasey@nvidia.com>
To: <gitster@pobox.com>
Cc: <git@vger.kernel.org>, Brandon Casey <drafnel@gmail.com>,
	<johan@herland.net>
Subject: [PATCH 1/2] builtin/checkout.c: don't leak memory in check_tracking_name
Date: Mon, 17 Jun 2013 18:40:49 -0700	[thread overview]
Message-ID: <1371519650-17869-1-git-send-email-bcasey@nvidia.com> (raw)

From: Brandon Casey <drafnel@gmail.com>

remote_find_tracking() populates the query struct with an allocated
string in the dst member.  So, we do not need to xstrdup() the string,
since we can transfer ownership from the query struct (which will go
out of scope at the end of this function) to our callback struct, but
we must free the string if it will not be used so we will not leak
memory.

Let's do so.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
 builtin/checkout.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index f5b50e5..3be0018 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -838,13 +838,16 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	memset(&query, 0, sizeof(struct refspec));
 	query.src = cb->src_ref;
 	if (remote_find_tracking(remote, &query) ||
-	    get_sha1(query.dst, cb->dst_sha1))
+	    get_sha1(query.dst, cb->dst_sha1)) {
+		free(query.dst);
 		return 0;
+	}
 	if (cb->dst_ref) {
+		free(query.dst);
 		cb->unique = 0;
 		return 0;
 	}
-	cb->dst_ref = xstrdup(query.dst);
+	cb->dst_ref = query.dst;
 	return 0;
 }
 
-- 
1.8.2.415.g63cec41

             reply	other threads:[~2013-06-18  1:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-18  1:40 Brandon Casey [this message]
2013-06-18  1:40 ` [PATCH 2/2] t/t9802: explicitly name the upstream branch to use as a base Brandon Casey
2013-06-18 13:42   ` Pete Wyckoff
2013-06-18 16:17     ` Junio C Hamano
2013-06-18  6:15 ` [PATCH 1/2] builtin/checkout.c: don't leak memory in check_tracking_name Jeff King

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=1371519650-17869-1-git-send-email-bcasey@nvidia.com \
    --to=bcasey@nvidia.com \
    --cc=drafnel@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johan@herland.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).