]> git.ipfire.org Git - thirdparty/git.git/commit - branch.c
branch: fix a leak in setup_tracking
authorRubén Justo <rjusto@gmail.com>
Sun, 11 Jun 2023 18:50:36 +0000 (20:50 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 12 Jun 2023 22:06:13 +0000 (15:06 -0700)
commit861c56f6f9e6d886421849a117afa60308fdba3b
tree3532b5619e3cefbded0d51d14b97d926be6a239a
parentcaee1d669c937a6b9d871901acbf9a5643a3fd9f
branch: fix a leak in setup_tracking

The commit d3115660b4 (branch: add flags and config to inherit tracking,
2021-12-20) replaced in "struct tracking", the member "char *src" by a
new "struct string_list *srcs".

This caused a modification in find_tracked_branch().  The string
returned by remote_find_tracking(), previously assigned to "src", is now
added to the string_list "srcs".

That string_list is initialized with STRING_LIST_INIT_DUP, which means
that what is added is not the given string, but a duplicate.  Therefore,
the string returned by remote_find_tracking() is leaked.

The leak can be reviewed with:

   $ git branch foo
   $ git remote add local .
   $ git fetch local
   $ git branch --track bar local/foo

   Direct leak of 24 byte(s) in 1 object(s) allocated from:
       ... in xrealloc wrapper.c
       ... in strbuf_grow strbuf.c
       ... in strbuf_add strbuf.c
       ... in match_name_with_pattern remote.c
       ... in query_refspecs remote.c
       ... in remote_find_tracking remote.c
       ... in find_tracked_branch branch.c
       ... in for_each_remote remote.c
       ... in setup_tracking branch.c
       ... in create_branch branch.c
       ... in cmd_branch builtin/branch.c
       ... in run_builtin git.c

Let's fix the leak, using the "_nodup" API to add to the string_list.
This way, the string itself will be added instead of being strdup()'d.
And when the string_list is cleared, the string will be freed.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
branch.c