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

Re: [PATCH 1/2] unpack-trees: don't leak memory in verify_clean_subdirectory()

From
Elijah Newren <newren@gmail.com>
Date
Oct 6, 2021, 15:58 UTC
Message-ID
<CABPp-BH0ca=kx85kfqh7GGRqEzUpzd4dUr58M5WrUmYXQs0e4w@mail.gmail.com>
In-Reply-To
<patch-1.2-e5ef1be2aa9-20211006T093405Z-avarab@gmail.com>
On Wed, Oct 6, 2021 at 2:40 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
Show 69 quoted lines
>
> Fix two different but related memory leaks in
> verify_clean_subdirectory(). We leaked both the "pathbuf" if
> read_directory() returned non-zero, and we never cleaned up our own
> "struct dir_struct" either.
>
>  * "pathbuf": When the read_directory() call followed by the
>    free(pathbuf) was added in c81935348be (Fix switching to a branch
>    with D/F when current branch has file D., 2007-03-15) we didn't
>    bother to free() before we called die().
>
>    But when this code was later libified in 203a2fe1170 (Allow callers
>    of unpack_trees() to handle failure, 2008-02-07) we started to leak
>    as we returned data to the caller. This fixes that memory leak,
>    which can be observed under SANITIZE=leak with e.g. the
>    "t1001-read-tree-m-2way.sh" test.
>
>  * "struct dir_struct": We've leaked the dir_struct ever since this
>    code was added back in c81935348be.
>
>    When that commit was written there wasn't an equivalent of
>    dir_clear(). Since it was added in 270be816049 (dir.c: provide
>    clear_directory() for reclaiming dir_struct memory, 2013-01-06)
>    we've omitted freeing the memory allocated here.
>
>    This memory leak could also be observed under SANITIZE=leak and the
>    "t1001-read-tree-m-2way.sh" test.
>
> This makes all the test in "t1001-read-tree-m-2way.sh" pass under
> "GIT_TEST_PASSING_SANITIZE_LEAK=true", we'd previously die in tests
> 25, 26 & 28.
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  t/t1001-read-tree-m-2way.sh | 2 ++
>  unpack-trees.c              | 3 ++-
>  2 files changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/t/t1001-read-tree-m-2way.sh b/t/t1001-read-tree-m-2way.sh
> index 1057a96b249..d1115528cb9 100755
> --- a/t/t1001-read-tree-m-2way.sh
> +++ b/t/t1001-read-tree-m-2way.sh
> @@ -20,6 +20,8 @@ In the test, these paths are used:
>         rezrov  - in H, deleted in M
>         yomin   - not in H or M
>  '
> +
> +TEST_PASSES_SANITIZE_LEAK=true
>  . ./test-lib.sh
>  . "$TEST_DIRECTORY"/lib-read-tree.sh
>
> diff --git a/unpack-trees.c b/unpack-trees.c
> index a7e1712d236..89ca95ce90b 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -2156,9 +2156,10 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
>         if (o->dir)
>                 d.exclude_per_dir = o->dir->exclude_per_dir;
>         i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);
> +       dir_clear(&d);
> +       free(pathbuf);
>         if (i)
>                 return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
> -       free(pathbuf);
>         return cnt;
>  }
>
> --
> 2.33.0.1441.gbbcdb4c3c66
Nicely explained; and nice clean patch.
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 3 of 17 in “dir & unpak-trees: memory-leak fixes”
  1. 0/2 dir & unpak-trees: memory-leak fixesÆvar Arnfjörð Bjarmason, Oct 6, 2021
  2. 1/2 unpack-trees: don't leak memory in verify_clean_subdirectory()Ævar Arnfjörð Bjarmason, Oct 6, 2021
  3. Elijah NewrenOct 6, 2021
  4. 2/2 built-ins & lib: plug memory leaks with unpack_trees_options_release()Ævar Arnfjörð Bjarmason, Oct 6, 2021
  5. Elijah NewrenOct 6, 2021
  6. Elijah NewrenOct 6, 2021
  7. 0/3 unpack-trees: memory-leak fixesÆvar Arnfjörð Bjarmason, Oct 7, 2021
  8. 2/3 sequencer: add a "goto cleanup" to do_reset()Ævar Arnfjörð Bjarmason, Oct 7, 2021
  9. Elijah NewrenOct 7, 2021
  10. 1/3 unpack-trees: don't leak memory in verify_clean_subdirectory()Ævar Arnfjörð Bjarmason, Oct 7, 2021
  11. Junio C HamanoOct 7, 2021
  12. 3/3 sequencer: fix a memory leak in do_reset()Ævar Arnfjörð Bjarmason, Oct 7, 2021
  13. Elijah NewrenOct 7, 2021
  14. 0/3 unpack-trees: memory-leak fixesÆvar Arnfjörð Bjarmason, Oct 13, 2021
  15. 1/3 unpack-trees: don't leak memory in verify_clean_subdirectory()Ævar Arnfjörð Bjarmason, Oct 13, 2021
  16. 2/3 sequencer: add a "goto cleanup" to do_reset()Ævar Arnfjörð Bjarmason, Oct 13, 2021
  17. 3/3 sequencer: fix a memory leak in do_reset()Ævar Arnfjörð Bjarmason, Oct 13, 2021

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.