git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Felipe Contreras <felipe.contreras@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 2/2] remote: fix trivial memory leak
Date: Tue, 24 Sep 2013 01:19:08 -0400	[thread overview]
Message-ID: <20130924051908.GG2766@sigill.intra.peff.net> (raw)
In-Reply-To: <1379772563-11000-3-git-send-email-felipe.contreras@gmail.com>

On Sat, Sep 21, 2013 at 09:09:23AM -0500, Felipe Contreras wrote:

> diff --git a/remote.c b/remote.c
> index efcba93..654e7f5 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -480,7 +480,6 @@ static void read_config(void)
>  	int flag;
>  	if (default_remote_name) /* did this already */
>  		return;
> -	default_remote_name = xstrdup("origin");
>  	current_branch = NULL;
>  	head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
>  	if (head_ref && (flag & REF_ISSYMREF) &&
> @@ -489,6 +488,8 @@ static void read_config(void)
>  			make_branch(head_ref + strlen("refs/heads/"), 0);
>  	}
>  	git_config(handle_config, NULL);
> +	if (!default_remote_name)
> +		default_remote_name = xstrdup("origin");
>  	alias_all_urls();

I wondered if we might also leak when seeing duplicate config options
(i.e., leaking the old one when replacing it with the new). But we don't
actually strdup() the configured remote names, but instead just point
into the "struct branch", which owns the data.

So I think an even better fix would be:

-- >8 --
Subject: remote: do not copy "origin" string literal

Our default_remote_name starts at "origin", but may be
overridden by the config file. In the former case, we
allocate a new string, but in the latter case, we point to
the remote name in an existing "struct branch".

This gives the variable inconsistent free() semantics (we
are sometimes responsible for freeing the string and
sometimes pointing to somebody else's storage), and causes a
small leak when the allocated string is overridden by
config.

We can fix both by simply dropping the extra copy and
pointing to the string literal.

Noticed-by: Felipe Contreras <felipe.contreras@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
---
 remote.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/remote.c b/remote.c
index e9fedfa..9f1a8aa 100644
--- a/remote.c
+++ b/remote.c
@@ -483,7 +483,7 @@ static void read_config(void)
 	int flag;
 	if (default_remote_name) /* did this already */
 		return;
-	default_remote_name = xstrdup("origin");
+	default_remote_name = "origin";
 	current_branch = NULL;
 	head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
 	if (head_ref && (flag & REF_ISSYMREF) &&
-- 
1.8.4.rc3.19.g9da5bf6

  reply	other threads:[~2013-09-24  5:19 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-21 14:09 [PATCH 0/2] fetch: trivial fixes Felipe Contreras
2013-09-21 14:09 ` [PATCH 1/2] fetch: add missing documentation Felipe Contreras
2013-09-24  5:03   ` Jeff King
2013-09-24  5:23     ` Felipe Contreras
2013-09-24  5:30       ` Jeff King
2013-09-24  5:36         ` Felipe Contreras
2013-09-24  5:40           ` Jeff King
2013-09-24  5:57             ` Felipe Contreras
2013-09-24  6:10               ` Jeff King
2013-09-24  6:31                 ` Felipe Contreras
2013-09-24  6:54                   ` Jeff King
2013-09-24  7:41                     ` Felipe Contreras
2013-09-21 14:09 ` [PATCH 2/2] remote: fix trivial memory leak Felipe Contreras
2013-09-24  5:19   ` Jeff King [this message]
2013-10-15 21:50     ` Junio C Hamano

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130924051908.GG2766@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=felipe.contreras@gmail.com \
    --cc=git@vger.kernel.org \
    /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.
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 NNTP newsgroup(s).