Skip to contentSkip to conversation

ref-filter.c: fix a leak in get_head_description

PATCH In review Rubén Justo 4 participants 7 messages Sep 24, 2022 – Oct 8, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ref-filter.c: fix a leak in get_head_description

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.

---
 ref-filter.c | 2 ++
 1 file changed, 2 insertions(+)
ref-filter.c +2 -0
index fd1cb14b0f..914908fac5 100644
@@ -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);
 }
 
Signature · 1 line
2.36.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Rubén Justo

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.

ref-filter.c char *get_head_description(void) · line 1728

Quoted 4 lines
ref-filter.c +2 -0
+       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

[1] https://lore.kernel.org/git/[email protected]/

Martin

#2 lore raw
Quoted 21 lines · Martin Ågren
Quoted 10 lines · Rubén Justo

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.

ref-filter.c char *get_head_description(void) · line 1728

Quoted 4 lines
ref-filter.c +2 -0
+       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.

Quoted 7 lines

FWIW, this patch is

[1] https://lore.kernel.org/git/[email protected]/

Martin

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

#3 lore raw
Quoted 3 lines · Junio C Hamano

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

#4 lore raw
In reply to Rubén Justo
Quoted 20 lines · Rubén Justo

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.

---
 ref-filter.c | 2 ++
 1 file changed, 2 insertions(+)
ref-filter.c +0 -0
index fd1cb14b0f..914908fac5 100644

ref-filter.c char *get_head_description(void) · line 1728

Quoted 7 lines
ref-filter.c +2 -0
@@ -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).

---
 builtin/checkout.c | 2 ++
 ref-filter.c       | 2 ++
 2 files changed, 4 insertions(+)
builtin/checkout.c +2 -0
index 54373424403..549c3d17a1a 100644
@@ -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,
ref-filter.c +2 -0
index 45908d4bdfc..81278ec3cf7 100644
@@ -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);
 }
author Reviewed-by
#5 lore raw
Quoted 5 lines · Ævar Arnfjörð Bjarmason

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:

Thanks Martin for your review!

Un saludo.

Quoted 49 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).

---
 builtin/checkout.c | 2 ++
 ref-filter.c       | 2 ++
 2 files changed, 4 insertions(+)
builtin/checkout.c +2 -0
index 54373424403..549c3d17a1a 100644
@@ -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,
ref-filter.c +2 -0
index 45908d4bdfc..81278ec3cf7 100644
@@ -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);
 }
 
#6 lore raw

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

In reply to Rubén Justo

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] [email protected]

---

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] [email protected]

+ ## 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(+)
builtin/checkout.c +2 -0
index 2a132392fb..659dd5c430 100644
@@ -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,
ref-filter.c +2 -0
index fd1cb14b0f..914908fac5 100644
@@ -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);
 }
 
Signature · 1 line
2.32.0