threads / patch / 58511

patchref-filter.c: fix a leak in get_head_description

Subject: [PATCH] ref-filter.c: fix a leak in get_head_description

## tl;dr

7 messages between Sep 24, 2022 and Oct 8, 2022. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Rubén Justo· Sep 24, 2022, 22:53 UTC · lore

In 2708ce62d2 (branch: sort detached HEAD based on a flag, 2021-01-07) a call to wt_status_state_free_buffers, responsible of freeing the resources that could be allocated in the local struct wt_status_state state, was eliminated.

The call to wt_status_state_free_buffers was introduced in 962dd7ebc3 (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27). This commit brings back that call in get_head_description.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 ref-filter.c | 2 ++
 1 file changed, 2 insertions(+)
Show changes to ref-filter.c +2 −0
diff --git a/ref-filter.c b/ref-filter.c
index fd1cb14b0f..914908fac5 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1722,6 +1722,8 @@ char *get_head_description(void)
 	} else
 		strbuf_addstr(&desc, _("(no branch)"));
 
+	wt_status_state_free_buffers(&state);
+
 	return strbuf_detach(&desc, NULL);
 }
 
-- 
2.36.1
Martin Ågren· Sep 26, 2022, 06:59 UTC · re: Rubén Justo · lore

Re: [PATCH] ref-filter.c: fix a leak in get_head_description

On Sun, 25 Sept 2022 at 00:53, Rubén Justo <rjusto@gmail.com> wrote:
Show 9 quoted lines
>
> In 2708ce62d2 (branch: sort detached HEAD based on a flag, 2021-01-07) a
> call to wt_status_state_free_buffers, responsible of freeing the
> resources that could be allocated in the local struct wt_status_state
> state, was eliminated.
>
> The call to wt_status_state_free_buffers was introduced in 962dd7ebc3
> (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27).  This
> commit brings back that call in get_head_description.
> +       wt_status_state_free_buffers(&state);
> +
>         return strbuf_detach(&desc, NULL);
>  }

Good catch, and excellent history digging. From the original submission [1] of the patch that dropped this call, I get the feeling that it was originally developed some time earlier. I suspect this call was then accidentally dropped in a rebase before submission.

FWIW, this patch is
Reviewed-by: Martin Ågren <martin.agren@gmail.com>
[1] https://lore.kernel.org/git/20210106100139.14651-1-avarab@gmail.com/
Martin
Junio C Hamano· Sep 26, 2022, 19:12 UTC · re: Martin Ågren · lore

Re: [PATCH] ref-filter.c: fix a leak in get_head_description

Martin Ågren <martin.agren@gmail.com> writes:
Show 28 quoted lines
> On Sun, 25 Sept 2022 at 00:53, Rubén Justo <rjusto@gmail.com> wrote:
>>
>> In 2708ce62d2 (branch: sort detached HEAD based on a flag, 2021-01-07) a
>> call to wt_status_state_free_buffers, responsible of freeing the
>> resources that could be allocated in the local struct wt_status_state
>> state, was eliminated.
>>
>> The call to wt_status_state_free_buffers was introduced in 962dd7ebc3
>> (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27).  This
>> commit brings back that call in get_head_description.
>
>> +       wt_status_state_free_buffers(&state);
>> +
>>         return strbuf_detach(&desc, NULL);
>>  }
>
> Good catch, and excellent history digging. From the original submission
> [1] of the patch that dropped this call, I get the feeling that it was
> originally developed some time earlier. I suspect this call was then
> accidentally dropped in a rebase before submission.
>
> FWIW, this patch is
>
> Reviewed-by: Martin Ågren <martin.agren@gmail.com>
>
> [1] https://lore.kernel.org/git/20210106100139.14651-1-avarab@gmail.com/
>
> Martin

Thanks, all. Will queue lest I forget, but I presume this is not release critical?

Martin Ågren· Sep 27, 2022, 05:59 UTC · re: Junio C Hamano · lore

Re: [PATCH] ref-filter.c: fix a leak in get_head_description

On Mon, 26 Sept 2022 at 21:12, Junio C Hamano <gitster@pobox.com> wrote:
>
> Thanks, all.  Will queue lest I forget, but I presume this is not
> release critical?

Correct. This regressed already in v2.30.1 and v2.31.0 after the leak was originally plugged in v2.29.0. So almost all recentish releases have this minor leak. I'm not even sure it's possible for a single git process to hit this leak more than once today.

Martin
Ævar Arnfjörð Bjarmason· Sep 26, 2022, 08:14 UTC · re: Rubén Justo · lore

Re: [PATCH] ref-filter.c: fix a leak in get_head_description

On Sun, Sep 25 2022, Rubén Justo wrote:
Show 27 quoted lines
> In 2708ce62d2 (branch: sort detached HEAD based on a flag, 2021-01-07) a
> call to wt_status_state_free_buffers, responsible of freeing the
> resources that could be allocated in the local struct wt_status_state
> state, was eliminated.
>
> The call to wt_status_state_free_buffers was introduced in 962dd7ebc3
> (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27).  This
> commit brings back that call in get_head_description.
>
> Signed-off-by: Rubén Justo <rjusto@gmail.com>
>
> ---
>  ref-filter.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/ref-filter.c b/ref-filter.c
> index fd1cb14b0f..914908fac5 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1722,6 +1722,8 @@ char *get_head_description(void)
>  	} else
>  		strbuf_addstr(&desc, _("(no branch)"));
>  
> +	wt_status_state_free_buffers(&state);
> +
>  	return strbuf_detach(&desc, NULL);
>  }

Thanks, this looks good to me. FWIW I have a local (still unsubmitted) series of leak fixes across the tree which fixed this leak, that commit is presented below as a "I've already been running with this for a while" data point.

Thanks!
-- >8 --
Subject: [PATCH] wt-status API users: use wt_status_state_free_buffers(), fix
 leaks

Fix a memory that was accidentally introduced in ref-filter.c in 2708ce62d21 (branch: sort detached HEAD based on a flag, 2021-01-07), and one that's been present in builtin/checkout.c since c45f0f525de (switch: reject if some operation is in progress, 2019-03-29).

In both cases we should be using the wt_status_state_free_buffers() API introduced in 962dd7ebc3e (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27).

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/checkout.c | 2 ++
 ref-filter.c       | 2 ++
 2 files changed, 4 insertions(+)
Show changes to 2 files +4 −0

builtin/checkout.c, ref-filter.c

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 54373424403..549c3d17a1a 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1469,6 +1469,8 @@ static void die_if_some_operation_in_progress(void)
 		      "or \"git worktree add\"."));
 	if (state.bisect_in_progress)
 		warning(_("you are switching branch while bisecting"));
+
+	wt_status_state_free_buffers(&state);
 }
 
 static int checkout_branch(struct checkout_opts *opts,
diff --git a/ref-filter.c b/ref-filter.c
index 45908d4bdfc..81278ec3cf7 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1722,6 +1722,8 @@ char *get_head_description(void)
 	} else
 		strbuf_addstr(&desc, _("(no branch)"));
 
+	wt_status_state_free_buffers(&state);
+
 	return strbuf_detach(&desc, NULL);
 }
Rubén Justo· Sep 26, 2022, 19:13 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: [PATCH] ref-filter.c: fix a leak in get_head_description

On 26/9/22 10:14, Ævar Arnfjörð Bjarmason wrote:
Show 5 quoted lines
> 
> Thanks, this looks good to me. FWIW I have a local (still unsubmitted)
> series of leak fixes across the tree which fixed this leak, that commit
> is presented below as a "I've already been running with this for a
> while" data point.

That call in die_if_some_operation_in_progress is a good addition to this fix. I haven't found a happy path that leaks there yet, but it is calling for it anyway. And the commit message looks good to me too. So you can add to that patch, if you want:

Reviewed-by: Rubén Justo <rjusto@gmail.com>
Thanks Martin for your review!
Un saludo.
Show 49 quoted lines
> 
> Thanks!
> 
> -- >8 --
> Subject: [PATCH] wt-status API users: use wt_status_state_free_buffers(), fix
>  leaks
> 
> Fix a memory that was accidentally introduced in ref-filter.c in
> 2708ce62d21 (branch: sort detached HEAD based on a flag, 2021-01-07),
> and one that's been present in builtin/checkout.c since
> c45f0f525de (switch: reject if some operation is in progress,
> 2019-03-29).
> 
> In both cases we should be using the wt_status_state_free_buffers()
> API introduced in 962dd7ebc3e (wt-status: introduce
> wt_status_state_free_buffers(), 2020-09-27).
> 
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  builtin/checkout.c | 2 ++
>  ref-filter.c       | 2 ++
>  2 files changed, 4 insertions(+)
> 
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index 54373424403..549c3d17a1a 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -1469,6 +1469,8 @@ static void die_if_some_operation_in_progress(void)
>  		      "or \"git worktree add\"."));
>  	if (state.bisect_in_progress)
>  		warning(_("you are switching branch while bisecting"));
> +
> +	wt_status_state_free_buffers(&state);
>  }
>  
>  static int checkout_branch(struct checkout_opts *opts,
> diff --git a/ref-filter.c b/ref-filter.c
> index 45908d4bdfc..81278ec3cf7 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1722,6 +1722,8 @@ char *get_head_description(void)
>  	} else
>  		strbuf_addstr(&desc, _("(no branch)"));
>  
> +	wt_status_state_free_buffers(&state);
> +
>  	return strbuf_detach(&desc, NULL);
>  }
> 
Rubén Justo· Oct 8, 2022, 00:35 UTC · re: Rubén Justo · lore

Re: [PATCH v2] wt-status: using of wt_status_state_free_buffers

In 2708ce62d2 (branch: sort detached HEAD based on a flag, 2021-01-07) a call to wt_status_state_free_buffers, responsible of freeing the resources that could be allocated in the local struct wt_status_state state, was eliminated.

That introduced a leak that occurs with "branch list" in a detached HEAD state. The number of leaks depends on the references to "refname" in --sort and --format. With defaults, 3 are leaked: default sort is by "refname" and default format needs two references of "refname": %(refname:lstrip=2) and %(refname:rstrip-2).

This doesn't leak:
    $ git branch --sort objectname --format '%(objectname)'
This leak once:
    $ git branch --sort refname --format '%(refname)'
This leak twice:
    $ git branch --sort refname --format '%(refname:lstrip=2)'
Note that each different "atom" is only resolved once (see ref-filter.c).

The call to wt_status_state_free_buffers was introduced in 962dd7ebc3 (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27). Bring back that call in get_head_description.

Ævar Arnfjörð pointed out [1] that a similar leak is present in builtin/checkout.c:die_if_some_operation_in_progress(void), since c45f0f525de (switch: reject if some operation is in progress, 2019-03-29). The leak occurs once with "switch" while bisecting.

Lets call to wc_status_state_free_buffers() there too.
[1] 220926.86bkr24kjw.gmgdl@evledraar.gmail.com
Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
Changes since v1:
	- Add details about the leak
	- Fix the leak in builtin/checkout.c
Range-diff:
1:  86b7803955 ! 1:  3fa1ddbb30 ref-filter.c: fix a leak in get_head_description
    @@ Commit message
         resources that could be allocated in the local struct wt_status_state
         state, was eliminated.
     
    +    That introduced a leak that occurs with "branch list" in a detached HEAD
    +    state.  The number of leaks depends on the references to "refname" in
    +    --sort and --format.  With defaults, 3 are leaked: default sort is by
    +    "refname" and default format needs two references of "refname",
    +    %(refname:lstrip=2) and %(refname:rstrip-2).
    +
    +    This doesn't leak:
    +        $ git branch --sort objectname --format '%(objectname)'
    +
    +    This leak once:
    +        $ git branch --sort refname --format '%(refname)'
    +
    +    This leak twice:
    +        $ git branch --sort refname --format '%(refname:lstrip=2)'
    +
    +    Note that each different "atom" is only resolved once (see ref-filter.c).
    +
         The call to wt_status_state_free_buffers was introduced in 962dd7ebc3
    -    (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27).  This
    -    commit brings back that call in get_head_description.
    +    (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27).
    +    Bring back that call in get_head_description.
    +
    +    Ævar Arnfjörð pointed out [1] that a similar leak is present in
    +    builtin/checkout.c:die_if_some_operation_in_progress(void), since
    +    c45f0f525de (switch: reject if some operation is in progress,
    +    2019-03-29).  The leak ocurrs once with checkout while bisecting.
    +
    +    Lets call to wc_status_state_free_buffers() there too.
    +
    +    [1] 220926.86bkr24kjw.gmgdl@evledraar.gmail.com
     
         Signed-off-by: Rubén Justo <rjusto@gmail.com>
     
    + ## builtin/checkout.c ##
    +@@ builtin/checkout.c: static void die_if_some_operation_in_progress(void)
    + 		      "or \"git worktree add\"."));
    + 	if (state.bisect_in_progress)
    + 		warning(_("you are switching branch while bisecting"));
    ++
    ++	wt_status_state_free_buffers(&state);
    + }
    + 
    + static int checkout_branch(struct checkout_opts *opts,
    +
      ## ref-filter.c ##
     @@ ref-filter.c: char *get_head_description(void)
      	} else
 builtin/checkout.c | 2 ++
 ref-filter.c       | 2 ++
 2 files changed, 4 insertions(+)
Show changes to 2 files +4 −0

builtin/checkout.c, ref-filter.c

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2a132392fb..659dd5c430 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1470,6 +1470,8 @@ static void die_if_some_operation_in_progress(void)
 		      "or \"git worktree add\"."));
 	if (state.bisect_in_progress)
 		warning(_("you are switching branch while bisecting"));
+
+	wt_status_state_free_buffers(&state);
 }
 
 static int checkout_branch(struct checkout_opts *opts,
diff --git a/ref-filter.c b/ref-filter.c
index fd1cb14b0f..914908fac5 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1722,6 +1722,8 @@ char *get_head_description(void)
 	} else
 		strbuf_addstr(&desc, _("(no branch)"));
 
+	wt_status_state_free_buffers(&state);
+
 	return strbuf_detach(&desc, NULL);
 }
 
-- 
2.32.0

← back to recent threads