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

Re: [RFC memory leak?] Minor memory leak fix

From
Duy Nguyen <pclouds@gmail.com>
Date
Mar 11, 2014, 11:58 UTC
Message-ID
<CACsJy8Bp6rpe4hLg0eMBCjPjL5uDwXj-7ZVmZtUZvR26unAuoQ@mail.gmail.com>
In-Reply-To
<1394534706-28987-1-git-send-email-iveqy@iveqy.com>
On Tue, Mar 11, 2014 at 5:45 PM, Fredrik Gustafsson <iveqy@iveqy.com> wrote:
> Strbuf needs to be released even if it's locally declared.

"path" is declared static. So yes it's a leak but the leak is minimum. Your patch would make more sense if "static" is gone and it's leaked after every write_archive_entry call.

Show 28 quoted lines
>
> Signed-off-by: Fredrik Gustafsson <iveqy@iveqy.com>
> ---
>  archive.c | 21 +++++++++++++++------
>  1 file changed, 15 insertions(+), 6 deletions(-)
>
> diff --git a/archive.c b/archive.c
> index 346f3b2..d6d56e6 100644
> --- a/archive.c
> +++ b/archive.c
> @@ -113,6 +113,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
>         struct git_attr_check check[2];
>         const char *path_without_prefix;
>         int err;
> +       int to_ret = 0;
>
>         args->convert = 0;
>         strbuf_reset(&path);
> @@ -126,8 +127,10 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
>
>         setup_archive_check(check);
>         if (!git_check_attr(path_without_prefix, ARRAY_SIZE(check), check)) {
> -               if (ATTR_TRUE(check[0].value))
> -                       return 0;
> +               if (ATTR_TRUE(check[0].value)) {
> +                       to_ret = 0;
> +                       goto cleanup;
> +               }
to_ret is already 0 so I think "goto cleanup;" is enough.
Show 16 quoted lines
>                 args->convert = ATTR_TRUE(check[1].value);
>         }
>
> @@ -135,14 +138,20 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
>                 if (args->verbose)
>                         fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
>                 err = write_entry(args, sha1, path.buf, path.len, mode);
> -               if (err)
> -                       return err;
> -               return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
> +               if (err) {
> +                       to_ret = err;
> +                       goto cleanup;
> +               }
> +               to_ret = (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
> +               goto cleanup;

Maybe if (err) to_ret = ...; else to_ret = ...; so we only need one "goto cleanup" statement. Going even further:

to_ret = write_entry(...); if (!to_ret) to_ret = (S_ISDIR(...)); goto cleanup;

Show 19 quoted lines
>         }
>
>         if (args->verbose)
>                 fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
> -       return write_entry(args, sha1, path.buf, path.len, mode);
> +       to_ret = write_entry(args, sha1, path.buf, path.len, mode);
> +cleanup:
> +       strbuf_release(&path);
> +       return to_ret;
>  }
>
>  int write_archive_entries(struct archiver_args *args,
> --
> 1.8.3.1.490.g39d9b24.dirty
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
-- 
Duy
Previous: Fredrik GustafssonNext: Fredrik Gustafsson
Message 2 of 6 in “[RFC memory leak?] Minor memory leak fix”
  1. Fredrik GustafssonMar 11, 2014
  2. Duy NguyenMar 11, 2014
  3. [[RFC memory leak, v.2]] Minor memory leak fixFredrik Gustafsson, Mar 11, 2014
  4. René ScharfeMar 11, 2014
  5. Fredrik GustafssonMar 11, 2014
  6. Junio C HamanoMar 11, 2014

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.