]> git.ipfire.org Git - thirdparty/git.git/commit
branch: fix a leak in inherit_tracking
authorRubén Justo <rjusto@gmail.com>
Sun, 11 Jun 2023 18:50:16 +0000 (20:50 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 12 Jun 2023 22:05:44 +0000 (15:05 -0700)
commita88a3d7cd7cee64fd29fe2a4c6c7a0511f398bfb
tree6d647fc7465c3fe791df8ce8f22529ef13feba4f
parent1533bda7700d6aa4374509f9611ae52a4eb2bcda
branch: fix a leak in inherit_tracking

In d3115660b4 (branch: add flags and config to inherit tracking,
2021-12-20) a new option was introduced to allow creating a new branch,
inheriting the tracking of another branch.

The new code, strdup()'d the remote_name of the existing branch, but
unfortunately it was not freed, producing a leak.

   $ git remote add local .
   $ git update-ref refs/remotes/local/foo HEAD
   $ git branch --track bar local/foo
   branch 'bar' set up to track 'local/foo'.
   $ git branch --track=inherit baz bar
   branch 'baz' set up to track 'local/foo'.

   Direct leak of 6 byte(s) in 1 object(s) allocated from:
       ... in xstrdup wrapper.c
       ... in inherit_tracking branch.c
       ... in setup_tracking branch.c
       ... in create_branch branch.c
       ... in cmd_branch builtin/branch.c
       ... in run_builtin git.c

Actually, the string we're strdup()'ing is from the struct branch
returned by get_branch().  Which, in turn, retrieves the string from the
global "struct repository".  This makes perfectly valid to use the
string throughout the entire execution of create_branch().  There is no
need to duplicate it.

Let's fix the leak, removing the strdup().

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