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

Re: [PATCH] diff: ensure correct lifetime of external_diff_cmd

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jan 9, 2019, 23:10 UTC
Message-ID
<CAPig+cQKnEWb+co_NJ0UyZbXZrvx2KsbS_ZugdyjjYZcz8tjvw@mail.gmail.com>
In-Reply-To
<20190109221007.21624-1-kgybels@infogroep.be>
On Wed, Jan 9, 2019 at 5:19 PM Kim Gybels <kgybels@infogroep.be> wrote:
Show 15 quoted lines
> 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.

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;
>  }
Previous: Kim GybelsNext: Johannes Schindelin
Message 2 of 6 in “diff: ensure correct lifetime of external_diff_cmd”
  1. diff: ensure correct lifetime of external_diff_cmdKim Gybels, Jan 9, 2019
  2. Eric SunshineJan 9, 2019
  3. Johannes SchindelinJan 10, 2019
  4. Junio C HamanoJan 10, 2019
  5. diff: ensure correct lifetime of external_diff_cmdKim Gybels, Jan 11, 2019
  6. Junio C HamanoJan 12, 2019

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.