From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: Kim Gybels <kgybels@infogroep.be>, Git List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>, Karsten Blees <blees@dcon.de>,
Johannes Sixt <j6t@kdbg.org>
Subject: Re: [PATCH] diff: ensure correct lifetime of external_diff_cmd
Date: Thu, 10 Jan 2019 16:47:02 +0100 (STD) [thread overview]
Message-ID: <nycvar.QRO.7.76.6.1901101646050.41@tvgsbejvaqbjf.bet> (raw)
In-Reply-To: <CAPig+cQKnEWb+co_NJ0UyZbXZrvx2KsbS_ZugdyjjYZcz8tjvw@mail.gmail.com>
Hi,
On Wed, 9 Jan 2019, Eric Sunshine wrote:
> On Wed, Jan 9, 2019 at 5:19 PM Kim Gybels <kgybels@infogroep.be> wrote:
> > According to getenv(3)'s notes:
> > [...]
> > Since strings returned by getenv() are allowed to change on subsequent
> > calls to getenv(), make sure to duplicate when caching external_diff_cmd
> > from environment.
> > [...]
> > Signed-off-by: Kim Gybels <kgybels@infogroep.be>
> > ---
> > diff --git a/diff.c b/diff.c
> > @@ -492,6 +492,9 @@ static const char *external_diff(void)
> > external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
> > if (!external_diff_cmd)
> > external_diff_cmd = external_diff_cmd_cfg;
> > + else
> > + external_diff_cmd = xstrdup(external_diff_cmd);
>
> Make sense.
>
> Not shown in the context is that 'external_diff_cmd' is static, so
> this is not (in the traditional sense) leaking the dup'd string.
Ah! And that also explains why we do not need to take care of releasing
the memory via `free()` (which is what I was wondering about).
> I do find that the logic is obscured by doing the xstrdup() in the
> 'else' arm; it would be easier to grok if the condition was reversed and
> xstrdup() done in the 'then' arm.
>
> However, you might also consider using xstrdup_or_null(), like this:
>
> external_diff_cmd = xstrdup_or_null(getenv(...));
> if (!external_diff_cmd)
> ...as before...
>
> > done_preparing = 1;
> > return external_diff_cmd;
> > }
I like this version slightly better, too.
Thanks for diagnosing and fixing this annoying bug!
Dscho
next prev parent reply other threads:[~2019-01-10 15:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-09 22:10 [PATCH] diff: ensure correct lifetime of external_diff_cmd Kim Gybels
2019-01-09 23:10 ` Eric Sunshine
2019-01-10 15:47 ` Johannes Schindelin [this message]
2019-01-10 18:27 ` Junio C Hamano
2019-01-11 20:26 ` [PATCH v2] " Kim Gybels
2019-01-12 2:44 ` 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
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=nycvar.QRO.7.76.6.1901101646050.41@tvgsbejvaqbjf.bet \
--to=johannes.schindelin@gmx.de \
--cc=blees@dcon.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=j6t@kdbg.org \
--cc=kgybels@infogroep.be \
--cc=sunshine@sunshineco.com \
/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).