Skip to contentSkip to conversation

tests: mark as passing with SANITIZE=leak

PATCH Superseded Rubén Justo 3 participants 47 messages Jun 11, 2023 – Jun 17, 2023

lore
Share & tools
lore
#0 lore raw

The goal in this series is to pass t3200 with SANITIZE=leak.

As a result of the fixes, other tests also pass.

This is the list of tests that no longer trigger any leak after this series:

+ t1507-rev-parse-upstream.sh
+ t1508-at-combinations.sh
+ t1514-rev-parse-push.sh
+ t2027-checkout-track.sh
+ t3200-branch.sh
+ t3204-branch-name-interpretation.sh
+ t5404-tracking-branches.sh
+ t5517-push-mirror.sh
+ t5525-fetch-tagopt.sh
+ t6040-tracking-info.sh
+ t7508-status.sh

Each of the commits (except 11/11) fixes a leak. They have no dependencies on each other. As a result, they can be reordered.

To review one leak, the commit can be moved to the tip or reverted. E.g. to review: "branch: fix a leak in check_tracking_branch", this can be used:

  $ git revert --no-edit HEAD~3
  $ make SANITIZE=leak test T=t3200-branch.sh 

Also, each commit have a minimal script in the message that can be used to reproduce the leak.

Rubén Justo (11):
rev-parse: fix a leak with --abbrev-ref
config: fix a leak in git_config_copy_or_rename_section_in_file
remote: fix a leak in query_matches_negative_refspec
branch: fix a leak in dwim_and_setup_tracking
branch: fix a leak in setup_tracking
branch: fix a leak in cmd_branch
branch: fix a leak in inherit_tracking
branch: fix a leak in check_tracking_branch
branch: fix a leak in setup_tracking
config: fix a leak in git_config_copy_or_rename_section_in_file
tests: mark as passing with SANITIZE=leak

 branch.c                              | 14 +++++++++-----
 builtin/branch.c                      |  2 +-
 builtin/rev-parse.c                   |  7 +++++--
 config.c                              |  2 ++
 remote.c                              |  2 +-
 t/t1507-rev-parse-upstream.sh         |  1 +
 t/t1508-at-combinations.sh            |  1 +
 t/t1514-rev-parse-push.sh             |  1 +
 t/t2027-checkout-track.sh             |  1 +
 t/t3200-branch.sh                     |  1 +
 t/t3204-branch-name-interpretation.sh |  1 +
 t/t5404-tracking-branches.sh          |  1 +
 t/t5517-push-mirror.sh                |  1 +
 t/t5525-fetch-tagopt.sh               |  1 +
 t/t6040-tracking-info.sh              |  1 +
 t/t7508-status.sh                     |  1 +
 16 files changed, 29 insertions(+), 9 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/11 rev-parse: fix a leak with --abbrev-ref

To handle "--abbrev-ref" we use shorten_unambiguous_ref(). This function takes a refname and returns a shortened refname, which is a newly allocated string that needs to be freed.

Unfortunately, the refname variable is reused to receive the shortened one. Therefore, we lose the original refname, which needs to be freed as well, producing a leak.

This leak can be reviewed with:

   $ for a in {1..10}; do git branch foo_${a}; done
   $ git rev-parse --abbrev-ref refs/heads/foo_{1..10}

Direct leak of 171 byte(s) in 10 object(s) allocated from:
... in xstrdup wrapper.c
... in expand_ref refs.c
... in repo_dwim_ref refs.c
... in show_rev builtin/rev-parse.c
... in cmd_rev_parse builtin/rev-parse.c
... in run_builtin git.c

We have this leak since a45d34691e (rev-parse: --abbrev-ref option to shorten ref name, 2009-04-13) when "--abbrev-ref" was introduced.

Let's fix it.

---
 builtin/rev-parse.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
builtin/rev-parse.c +5 -2
index 852e49e340..9fd7095431 100644
@@ -141,7 +141,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 	if ((symbolic || abbrev_ref) && name) {
 		if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
 			struct object_id discard;
-			char *full;
+			char *full, *to_free = NULL;
 
 			switch (repo_dwim_ref(the_repository, name,
 					      strlen(name), &discard, &full,
@@ -156,9 +156,11 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				 */
 				break;
 			case 1: /* happy */
-				if (abbrev_ref)
+				if (abbrev_ref) {
+					to_free = full;
 					full = shorten_unambiguous_ref(full,
 						abbrev_ref_strict);
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */
@@ -166,6 +168,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				break;
 			}
 			free(full);
+			free(to_free);
 		} else {
 			show_with_type(type, name);
 		}
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH 01/11] rev-parse: fix a leak with --abbrev-ref

Quoted 7 lines · Rubén Justo

To handle "--abbrev-ref" we use shorten_unambiguous_ref(). This function takes a refname and returns a shortened refname, which is a newly allocated string that needs to be freed.

Unfortunately, the refname variable is reused to receive the shortened one. Therefore, we lose the original refname, which needs to be freed as well, producing a leak.

Makes sense, but...

Quoted 27 lines
builtin/rev-parse.c +4 -2
index 852e49e340..9fd7095431 100644
@@ -141,7 +141,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 	if ((symbolic || abbrev_ref) && name) {
 		if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
 			struct object_id discard;
-			char *full;
+			char *full, *to_free = NULL;
 
 			switch (repo_dwim_ref(the_repository, name,
 					      strlen(name), &discard, &full,
@@ -156,9 +156,11 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				 */
 				break;
 			case 1: /* happy */
-				if (abbrev_ref)
+				if (abbrev_ref) {
+					to_free = full;
 					full = shorten_unambiguous_ref(full,
 						abbrev_ref_strict);
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */

builtin/rev-parse.c static void show_rev(int type, const struct object_id *oid, const char *name) · line 174

Quoted 7 lines
builtin/rev-parse.c +1 -0
@@ -166,6 +168,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				break;
 			}
 			free(full);
+			free(to_free);
 		} else {
 			show_with_type(type, name);
 		}

The lifetime of "to_free" is much longer than it needs to be here. We'd usually use a "to_free" pattern when the memory is aliased to another (usually const) pointer, and the allocation needs to live as long as that other pointer. But here, nobody cares about the old value as soon as we replace it. We could almost just free() it ahead of time, but of course we also need to pass it to the shortening function. So maybe:

builtin/rev-parse.c +5 -2
index 852e49e340..6dc8548e1f 100644
@@ -156,9 +156,12 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				 */
 				break;
 			case 1: /* happy */
-				if (abbrev_ref)
-					full = shorten_unambiguous_ref(full,
+				if (abbrev_ref) {
+					char *old = full;
+					full = shorten_unambiguous_ref(old,
 						abbrev_ref_strict);
+					free(old);
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */

-Peff

#3 lore raw

Re: [PATCH 01/11] rev-parse: fix a leak with --abbrev-ref

Quoted 9 lines · Jeff King
builtin/rev-parse.c +0 -1
index 852e49e340..6dc8548e1f 100644
@@ -156,9 +156,12 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				 */
 				break;
 			case 1: /* happy */
-				if (abbrev_ref)

builtin/rev-parse.c static void show_rev(int type, const struct object_id *oid, const char *name) · line 167

Quoted 10 lines
builtin/rev-parse.c +5 -1
-					full = shorten_unambiguous_ref(full,
+				if (abbrev_ref) {
+					char *old = full;
+					full = shorten_unambiguous_ref(old,
 						abbrev_ref_strict);
+					free(old);
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */

Simpler. Nice.

Thanks!

#4 lore raw

PATCH 2/11 config: fix a leak in git_config_copy_or_rename_section_in_file

In reply to Rubén Justo

In 52d59cc645 (branch: add a --copy (-c) option to go with --move (-m), 2017-06-18) a new strbuf variable was introduced, but not released.

Thus, when copying a branch that has any configuration, we have a leak.

   $ git branch foo
   $ git config branch.foo.some-key some_value
   $ git branch -c foo bar

Direct leak of 65 byte(s) in 1 object(s) allocated from:
... in xrealloc wrapper.c
... in strbuf_grow strbuf.c
... in strbuf_vaddf strbuf.c
... in strbuf_addf strbuf.c
... in store_create_section config.c
... in git_config_copy_or_rename_section_in_file config.c ... in git_config_copy_section_in_file config.c ... in git_config_copy_section config.c
... in copy_or_rename_branch builtin/branch.c ... in cmd_branch builtin/branch.c
... in run_builtin git.c

Let's fix that leak.

---
 config.c | 1 +
 1 file changed, 1 insertion(+)
config.c +1 -0
index b79baf83e3..39a7d7422c 100644
@@ -3879,6 +3879,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 	free(filename_buf);
 	config_store_data_clear(&store);
 	strbuf_release(&buf);
+	strbuf_release(&copystr);
 	return ret;
 }
 
Signature · 1 line
2.40.1
#5 lore raw

Re: [PATCH 02/11] config: fix a leak in git_config_copy_or_rename_section_in_file

Quoted 9 lines · Rubén Justo

In 52d59cc645 (branch: add a --copy (-c) option to go with --move (-m), 2017-06-18) a new strbuf variable was introduced, but not released.

Thus, when copying a branch that has any configuration, we have a leak.

   $ git branch foo
   $ git config branch.foo.some-key some_value
   $ git branch -c foo bar

Looks good. Thanks for digging up the commit which introduced the problem. I always find that gives me more confidence that the leak is just a simple "oops, we forgot to free", and there isn't something more subtle going on.

-Peff

#6 lore raw

PATCH 3/11 remote: fix a leak in query_matches_negative_refspec

In reply to Rubén Justo

In c0192df630 (refspec: add support for negative refspecs, 2020-09-30) query_matches_negative_refspec() was introduced.

The function was implemented as a two-loop process, where the former loop accumulates and the latter evaluates. To accumulate, a string_list is used.

Within the first loop, there are three cases where a string is added to the string_list. Two of them add strings that do not need to be freed. But in the third case, the string added is returned by match_name_with_pattern(), which needs to be freed.

The string_list is initialized with STRING_LIST_INIT_NODUP, i.e. when cleared, the strings added are not freed. Therefore, the string returned by match_name_with_pattern() is not freed, so we have a leak.

   $ git remote add local .
   $ git update-ref refs/remotes/local/foo HEAD
   $ 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_matches_negative_refspec 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

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_matches_negative_refspec remote.c ... in query_refspecs remote.c
... in remote_find_tracking remote.c
... in check_tracking_branch branch.c
... in for_each_remote remote.c
... in validate_remote_tracking_branch branch.c ... in dwim_branch_start branch.c
... in create_branch branch.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

An interesting point to note is that while string_list_append() is used in the first two cases described, string_list_append_nodup() is used in the third. This seems to indicate an intention to delegate the responsibility for freeing the string, to the string_list. As if the string_list had been initialized with STRING_LIST_INIT_DUP, i.e. the strings are strdup()'d when added (except if the "_nodup" API is used) and freed when cleared.

Switching to STRING_LIST_INIT_DUP fixes the leak and probably is what we wanted to do originally. Let's do it.

---
 remote.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
remote.c +1 -1
index 0764fca0db..1bcd36e358 100644
@@ -890,7 +890,7 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite
 {
 	int i, matched_negative = 0;
 	int find_src = !query->src;
-	struct string_list reversed = STRING_LIST_INIT_NODUP;
+	struct string_list reversed = STRING_LIST_INIT_DUP;
 	const char *needle = find_src ? query->dst : query->src;
 
 	/*
Signature · 1 line
2.40.1
#7 lore raw

Re: [PATCH 03/11] remote: fix a leak in query_matches_negative_refspec

Quoted 10 lines · Rubén Justo

An interesting point to note is that while string_list_append() is used in the first two cases described, string_list_append_nodup() is used in the third. This seems to indicate an intention to delegate the responsibility for freeing the string, to the string_list. As if the string_list had been initialized with STRING_LIST_INIT_DUP, i.e. the strings are strdup()'d when added (except if the "_nodup" API is used) and freed when cleared.

Switching to STRING_LIST_INIT_DUP fixes the leak and probably is what we wanted to do originally. Let's do it.

Ah, clearing a NODUP string-list strikes again. :) I concur with your reasoning here; switching it to DUP is the best fix.

-Peff

#8 lore raw

Re: [PATCH 03/11] remote: fix a leak in query_matches_negative_refspec

Quoted 10 lines · Jeff King
Quoted 8 lines · Rubén Justo

An interesting point to note is that while string_list_append() is used in the first two cases described, string_list_append_nodup() is used in the third. This seems to indicate an intention to delegate the responsibility for freeing the string, to the string_list. As if the string_list had been initialized with STRING_LIST_INIT_DUP, i.e. the strings are strdup()'d when added (except if the "_nodup" API is used) and freed when cleared.

Quoted 4 lines
Quoted 2 lines

Switching to STRING_LIST_INIT_DUP fixes the leak and probably is what we wanted to do originally. Let's do it.

Ah, clearing a NODUP string-list strikes again. :) I concur with your

Yes. "_nodup" with NODUP string_list, shows red flags, imo.

Quoted 1 line

reasoning here; switching it to DUP is the best fix.

Thank you for reviewing the change.

#9 lore raw

PATCH 4/11 branch: fix a leak in dwim_and_setup_tracking

In reply to Rubén Justo

In e89f151db1 (branch: move --set-upstream-to behavior to dwim_and_setup_tracking(), 2022-01-28) the string returned by dwim_branch_start() was not freed, resulting in a memory leak.

It can be reviewed with:

   $ git remote add local .
   $ git update-ref refs/remotes/local/foo HEAD
   $ git branch --set-upstream-to local/foo foo

Direct leak of 23 byte(s) in 1 object(s) allocated from:
... in xstrdup wrapper.c
... in expand_ref refs.c
... in repo_dwim_ref refs.c
... in dwim_branch_start branch.c
... in dwim_and_setup_tracking branch.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

Let's free it now.

---
 branch.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
branch.c +2 -1
index ba3914adf5..a7333a4c32 100644
@@ -638,9 +638,10 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
 			     const char *orig_ref, enum branch_track track,
 			     int quiet)
 {
-	char *real_orig_ref;
+	char *real_orig_ref = NULL;
 	dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
 	setup_tracking(new_ref, real_orig_ref, track, quiet);
+	free(real_orig_ref);
 }
 
 /**
Signature · 1 line
2.40.1
#10 lore raw

Re: [PATCH 04/11] branch: fix a leak in dwim_and_setup_tracking

Quoted 3 lines · Rubén Justo

In e89f151db1 (branch: move --set-upstream-to behavior to dwim_and_setup_tracking(), 2022-01-28) the string returned by dwim_branch_start() was not freed, resulting in a memory leak.

Yep, looks good. One small comment:

Quoted 5 lines
branch.c +0 -0
index ba3914adf5..a7333a4c32 100644

branch.c void dwim_and_setup_tracking(struct repository *r, const char *new_ref, · line 645

Quoted 9 lines
branch.c +2 -1
@@ -638,9 +638,10 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
 			     const char *orig_ref, enum branch_track track,
 			     int quiet)
 {
-	char *real_orig_ref;
+	char *real_orig_ref = NULL;
 	dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
 	setup_tracking(new_ref, real_orig_ref, track, quiet);
+	free(real_orig_ref);
 }

Adding the NULL-initialization signals that we expect that real_orig_ref might sometimes _not_ be filled in by dwim_branch_start(). But I think it always will be; and if it weren't, that would make passing it to setup_tracking() rather suspicious.

So I don't think the NULL initialization is wrong, and certainly it is more defensive. But I find it a little misleading.

-Peff

#11 lore raw

Re: [PATCH 04/11] branch: fix a leak in dwim_and_setup_tracking

Quoted 28 lines · Jeff King
Quoted 3 lines · Rubén Justo

In e89f151db1 (branch: move --set-upstream-to behavior to dwim_and_setup_tracking(), 2022-01-28) the string returned by dwim_branch_start() was not freed, resulting in a memory leak.

Yep, looks good. One small comment:

Quoted 5 lines
branch.c +0 -0
index ba3914adf5..a7333a4c32 100644

branch.c void dwim_and_setup_tracking(struct repository *r, const char *new_ref, · line 645

Quoted 9 lines
branch.c +2 -1
@@ -638,9 +638,10 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
 			     const char *orig_ref, enum branch_track track,
 			     int quiet)
 {
-	char *real_orig_ref;
+	char *real_orig_ref = NULL;
 	dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
 	setup_tracking(new_ref, real_orig_ref, track, quiet);
+	free(real_orig_ref);
 }

Adding the NULL-initialization signals that we expect that real_orig_ref might sometimes _not_ be filled in by dwim_branch_start(). But I think it always will be; and if it weren't, that would make passing it to setup_tracking() rather suspicious.

Quoted 2 lines

So I don't think the NULL initialization is wrong, and certainly it is more defensive. But I find it a little misleading.

I don't have any objection to this. But I've seen Junio has already merged this to 'next'. I'm not going to re-roll this commit. Sorry.

Thank you for your review.

#12 lore raw

PATCH 5/11 branch: fix a leak in setup_tracking

In reply to Rubén Justo

In bdaf1dfae7 (branch: new autosetupmerge option "simple" for matching branches, 2022-04-29) a new exit for setup_tracking() missed the clean-up, producing a leak.

   $ git config branch.autoSetupMerge simple
   $ git remote add local .
   $ git update-ref refs/remotes/local/foo HEAD
   $ git branch bar local/foo

Direct leak of 384 byte(s) in 1 object(s) allocated from:
... in xrealloc wrapper.c
... in string_list_append_nodup string-list.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 builtinbranch.c
... in run_builtin git.c

Indirect 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 builtinbranch.c
... in run_builtin git.c

The return introduced in bdaf1dfae7 was to avoid setting up the tracking, but even in that case it is still necessary to do the clean-up. Let's do it.

---
 branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
branch.c +1 -1
index a7333a4c32..ff81c2266a 100644
@@ -333,7 +333,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
 		if (!skip_prefix(tracking.srcs->items[0].string,
 				 "refs/heads/", &tracked_branch) ||
 		    strcmp(tracked_branch, new_ref))
-			return;
+			goto cleanup;
 	}
 
 	if (tracking.srcs->nr < 1)
Signature · 1 line
2.40.1
#13 lore raw

Re: [PATCH 05/11] branch: fix a leak in setup_tracking

Quoted 3 lines · Rubén Justo

The return introduced in bdaf1dfae7 was to avoid setting up the tracking, but even in that case it is still necessary to do the clean-up. Let's do it.

That may be a sign that the return introduced by that commit is in the wrong spot (i.e., could we be checking it earlier and returning before doing the work that led to the allocations?).

But I didn't look too carefully, and I think for the purposes of your series it is OK to simply fix the leak without digging too far.

I'll cc the author (and quote the patch below) though, as sometimes in cases like these they may be interested in looking deeper themselves.

-Peff

Quoted 20 lines
---
 branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
branch.c +1 -1
index a7333a4c32..ff81c2266a 100644
@@ -333,7 +333,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
 		if (!skip_prefix(tracking.srcs->items[0].string,
 				 "refs/heads/", &tracked_branch) ||
 		    strcmp(tracked_branch, new_ref))
-			return;
+			goto cleanup;
 	}
 
 	if (tracking.srcs->nr < 1)
Signature · 1 line
2.40.1
#14 lore raw

Re: [PATCH 05/11] branch: fix a leak in setup_tracking

Quoted 6 lines · Jeff King
Quoted 3 lines · Rubén Justo

The return introduced in bdaf1dfae7 was to avoid setting up the tracking, but even in that case it is still necessary to do the clean-up. Let's do it.

Quoted 6 lines

That may be a sign that the return introduced by that commit is in the wrong spot (i.e., could we be checking it earlier and returning before doing the work that led to the allocations?).

But I didn't look too carefully, and I think for the purposes of your series it is OK to simply fix the leak without digging too far.

Thanks.

Quoted 3 lines

I'll cc the author (and quote the patch below) though, as sometimes in cases like these they may be interested in looking deeper themselves.

Perfect. Thank you.

#15 lore raw

PATCH 6/11 branch: fix a leak in cmd_branch

In reply to Rubén Justo

In 98e7ab6d42 (for-each-ref: delay parsing of --sort=<atom> options, 2021-10-20) a new string_list was introduced to accumulate any "branch.sort" setting.

That string_list is cleared in ref_sorting_options(), which is only called when processing the "--list" sub-command. Therefore, with other sub-command, while having any sort option set, a leak is produced, e.g.:

   $ git config branch.sort invalid_sort_option
   $ git branch --edit-description

Direct leak of 384 byte(s) in 1 object(s) allocated from:
... in xrealloc wrapper.c
... in string_list_append_nodup string-list.c ... in string_list_append string-list.c
... in git_branch_config builtin/branch.c ... in configset_iter config.c
... in repo_config config.c
... in git_config config.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

Indirect leak of 20 byte(s) in 1 object(s) allocated from:
... in xstrdup wrapper.c
... in string_list_append string-list.c
... in git_branch_config builtin/branch.c ... in configset_iter config.c
... in repo_config config.c
... in git_config config.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

We don't have a common clean-up section in cmd_branch(). To avoid refactoring and keep the fix simple, and while we find a better solution, let's silence the leak-hunter making the list static.

---
 builtin/branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/branch.c +1 -1
index e6c2655af6..759480fe8d 100644
@@ -709,7 +709,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	enum branch_track track;
 	struct ref_filter filter;
 	static struct ref_sorting *sorting;
-	struct string_list sorting_options = STRING_LIST_INIT_DUP;
+	static struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	struct ref_format format = REF_FORMAT_INIT;
 
 	struct option options[] = {
Signature · 1 line
2.40.1
#16 lore raw

Re: [PATCH 06/11] branch: fix a leak in cmd_branch

Quoted 3 lines · Rubén Justo

We don't have a common clean-up section in cmd_branch(). To avoid refactoring and keep the fix simple, and while we find a better solution, let's silence the leak-hunter making the list static.

Gross. :)

If we are just going to annotate here, I'd prefer to use UNLEAK(). It makes it more obvious this is about leak-checking, and doesn't imply that we otherwise care about the lifetime of this field. Like:

builtin/branch.c +2 -0
index e6c2655af6..075e580d22 100644
@@ -832,6 +832,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (list)
 		setup_auto_pager("branch", 1);
 
+	UNLEAK(sorting_options);
+
 	if (delete) {
 		if (!argc)
 			die(_("branch name required"));

builtin/branch.c int cmd_branch(int argc, const char **argv, const char *prefix) · line 712

Quoted 10 lines
builtin/branch.c +1 -1
index e6c2655af6..759480fe8d 100644
@@ -709,7 +709,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	enum branch_track track;
 	struct ref_filter filter;
 	static struct ref_sorting *sorting;
-	struct string_list sorting_options = STRING_LIST_INIT_DUP;
+	static struct string_list sorting_options = STRING_LIST_INIT_DUP;

It's interesting that the ref_sorting pointer is also static. This goes back to aedcb7dc75 (branch.c: use 'ref-filter' APIs, 2015-09-23). I wonder if it was trying to accomplish the same thing (back then we added directly to the list), though back then we did not have any real leak-detection tools set up.

It should probably move into the "if (list)" block, but that is orthogonal to your patch.

As for the more complete fix, I think we'd perhaps want something like this, which would also detect when --sort is used in a non-list mode (as your example shows, it's a little complicated because we stuff configured entries into the same list):

builtin/branch.c +9 -1
index e6c2655af6..bcd2d9e1d9 100644
@@ -708,8 +708,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	    recurse_submodules_explicit = 0;
 	enum branch_track track;
 	struct ref_filter filter;
-	static struct ref_sorting *sorting;
 	struct string_list sorting_options = STRING_LIST_INIT_DUP;
+	size_t nr_configured_sorting_options;
 	struct ref_format format = REF_FORMAT_INIT;
 
 	struct option options[] = {
@@ -773,6 +773,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		usage_with_options(builtin_branch_usage, options);
 
 	git_config(git_branch_config, &sorting_options);
+	nr_configured_sorting_options = sorting_options.nr;
 
 	track = git_branch_track;
 
@@ -832,6 +833,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (list)
 		setup_auto_pager("branch", 1);
 
+	if (!list) {
+		if (sorting_options.nr != nr_configured_sorting_options)
+			die(_("--sort does not make sense when not listing"));
+		string_list_clear(&sorting_options, 0);
+	}
+
 	if (delete) {
 		if (!argc)
 			die(_("branch name required"));
@@ -840,6 +847,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		print_current_branch_name();
 		return 0;
 	} else if (list) {
+		struct ref_sorting *sorting;
 		/*  git branch --list also shows HEAD when it is detached */
 		if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
 			filter.kind |= FILTER_REFS_DETACHED_HEAD;

-Peff

#17 lore raw

Re: [PATCH 06/11] branch: fix a leak in cmd_branch

Quoted 7 lines · Jeff King
Quoted 3 lines · Rubén Justo

We don't have a common clean-up section in cmd_branch(). To avoid refactoring and keep the fix simple, and while we find a better solution, let's silence the leak-hunter making the list static.

Gross. :)

XD

Quoted 10 lines

If we are just going to annotate here, I'd prefer to use UNLEAK(). It makes it more obvious this is about leak-checking, and doesn't imply that we otherwise care about the lifetime of this field. Like:

builtin/branch.c +0 -0
index e6c2655af6..075e580d22 100644

builtin/branch.c int cmd_branch(int argc, const char **argv, const char *prefix) · line 839

Quoted 8 lines
builtin/branch.c +2 -0
@@ -832,6 +832,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (list)
 		setup_auto_pager("branch", 1);
 
+	UNLEAK(sorting_options);
+
 	if (delete) {
 		if (!argc)
 			die(_("branch name required"));

OK. Will do this.

Thank you for reviewing the change with a wide view.

#18 lore raw

PATCH 7/11 branch: fix a leak in inherit_tracking

In reply to Rubén Justo

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().

---
 branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
branch.c +1 -1
index ff81c2266a..19d606d360 100644
@@ -233,7 +233,7 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
 		return -1;
 	}
 
-	tracking->remote = xstrdup(branch->remote_name);
+	tracking->remote = branch->remote_name;
 	for (i = 0; i < branch->merge_nr; i++)
 		string_list_append(tracking->srcs, branch->merge_name[i]);
 	return 0;
Signature · 1 line
2.40.1
#19 lore raw

Re: [PATCH 07/11] branch: fix a leak in inherit_tracking

Quoted 7 lines · Rubén Justo

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().

Yep, good reasoning. I agree this is a good way to fix the leak.

-Peff

#20 lore raw

PATCH 8/11 branch: fix a leak in check_tracking_branch

In reply to Rubén Justo

Let's fix a leak we have in check_tracking_branch() since the function was introduced in 41c21f22d0 (branch.c: Validate tracking branches with refspecs instead of refs/remotes/*, 2013-04-21).

The leak can be reviewed with:

   $ git remote add local .
   $ git update-ref refs/remotes/local/foo HEAD
   $ 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 check_tracking_branch branch.c
... in for_each_remote remote.c
... in validate_remote_tracking_branch branch.c ... in dwim_branch_start branch.c
... in create_branch branch.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

---
 branch.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
branch.c +4 -1
index 19d606d360..09b9563ae7 100644
@@ -480,9 +480,12 @@ static int check_tracking_branch(struct remote *remote, void *cb_data)
 {
 	char *tracking_branch = cb_data;
 	struct refspec_item query;
+	int res;
 	memset(&query, 0, sizeof(struct refspec_item));
 	query.dst = tracking_branch;
-	return !remote_find_tracking(remote, &query);
+	res = !remote_find_tracking(remote, &query);
+	free(query.src);
+	return res;
 }
 
 static int validate_remote_tracking_branch(char *ref)
Signature · 1 line
2.40.1
#21 lore raw

Re: [PATCH 08/11] branch: fix a leak in check_tracking_branch

Quoted 6 lines · Rubén Justo
branch.c +0 -0
index 19d606d360..09b9563ae7 100644
@@ -480,9 +480,12 @@ static int check_tracking_branch(struct remote *remote, void *cb_data)
 {

branch.c static int check_tracking_branch(struct remote *remote, void *cb_data) · line 489

Quoted 10 lines
branch.c +4 -1
 	char *tracking_branch = cb_data;
 	struct refspec_item query;
+	int res;
 	memset(&query, 0, sizeof(struct refspec_item));
 	query.dst = tracking_branch;
-	return !remote_find_tracking(remote, &query);
+	res = !remote_find_tracking(remote, &query);
+	free(query.src);
+	return res;
 }

OK, so we expect remote_find_tracking() to fill in the "src" field, but we don't actually care about the value here (we are just validating). It probably doesn't fill in the value for some error cases, but then we'd be left with a NULL, which is OK to feed to free(). Makes sense. I double-checked that it always allocates "src" when it is assigned to, so I think this fix is good.

-Peff

#22 lore raw

PATCH 9/11 branch: fix a leak in setup_tracking

In reply to Rubén Justo

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.

---
 branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
branch.c +1 -1
index 09b9563ae7..d88f50a48a 100644
@@ -37,7 +37,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 	if (!remote_find_tracking(remote, &tracking->spec)) {
 		switch (++tracking->matches) {
 		case 1:
-			string_list_append(tracking->srcs, tracking->spec.src);
+			string_list_append_nodup(tracking->srcs, tracking->spec.src);
 			tracking->remote = remote->name;
 			break;
 		case 2:
Signature · 1 line
2.40.1
#23 lore raw

Re: [PATCH 09/11] branch: fix a leak in setup_tracking

Quoted 11 lines · Rubén Justo

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.

Your fix makes sense. I had to stare at the existing code for a long time to make sure the _other_ side of the switch wasn't leaking, but it works by falling through "case 2" into "default", which frees tracking->spec.src. So this should plug the leak completely.

-Peff

#24 lore raw

PATCH 10/11 config: fix a leak in git_config_copy_or_rename_section_in_file

In reply to Rubén Justo

A branch can have its configuration spread over several configuration sections. This situation was already foreseen in 52d59cc645 (branch:
add a --copy (-c) option to go with --move (-m), 2017-06-18) when "branch -c" was introduced.

Unfortunately, a leak was also introduced:

   $ git branch foo
   $ cat >> .git/config <<EOF
   [branch "foo"]
   	some-key-a = a value
   [branch "foo"]
   	some-key-b = b value
   [branch "foo"]
   	some-key-c = c value
   EOF
   $ git branch -c foo bar

Direct leak of 130 byte(s) in 2 object(s) allocated from:
... in xrealloc wrapper.c
... in strbuf_grow strbuf.c
... in strbuf_vaddf strbuf.c
... in strbuf_addf strbuf.c
... in store_create_section config.c
... in git_config_copy_or_rename_section_in_file config.c ... in git_config_copy_section_in_file config.c ... in git_config_copy_section config.c
... in copy_or_rename_branch builtin/branch.c ... in cmd_branch builtin/branch.c
... in run_builtin git.c

Let's fix it.

---
 config.c | 1 +
 1 file changed, 1 insertion(+)
config.c +1 -0
index 39a7d7422c..207e4394a3 100644
@@ -3833,6 +3833,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 						output[0] = '\t';
 					}
 				} else {
+					strbuf_release(&copystr);
 					copystr = store_create_section(new_name, &store);
 				}
 			}
Signature · 1 line
2.40.1
#25 lore raw

Re: [PATCH 10/11] config: fix a leak in git_config_copy_or_rename_section_in_file

config.c static int git_config_copy_or_rename_section_in_file(const char *config_filename · line 3839

Quoted 12 lines · Rubén Justo
config.c +1 -0
index 39a7d7422c..207e4394a3 100644
@@ -3833,6 +3833,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 						output[0] = '\t';
 					}
 				} else {
+					strbuf_release(&copystr);
 					copystr = store_create_section(new_name, &store);
 				}
 			}

Wow, I did a double-take on this code. It is uncommon in our codebase to assign a struct by value in a function return like this, and doubly weird to assign a strbuf (since the whole point of strbuf is to use the opaque functions to make sure we aren't overwriting existing allocations or aliasing pointers).

I think your fix here is the correct thing if we aren't going to clean up the code further.

The more usual thing for our codebase would be refactoring like:

config.c +15 -14
index b79baf83e3..f5a7cced7c 100644
@@ -3140,37 +3140,36 @@ static int write_error(const char *filename)
 	return 4;
 }
 
-static struct strbuf store_create_section(const char *key,
-					  const struct config_store_data *store)
+static void store_create_section(const char *key,
+				 const struct config_store_data *store,
+				 struct strbuf *sb)
 {
 	const char *dot;
 	size_t i;
-	struct strbuf sb = STRBUF_INIT;
 
 	dot = memchr(key, '.', store->baselen);
 	if (dot) {
-		strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
+		strbuf_addf(sb, "[%.*s \"", (int)(dot - key), key);
 		for (i = dot - key + 1; i < store->baselen; i++) {
 			if (key[i] == '"' || key[i] == '\\')
-				strbuf_addch(&sb, '\\');
-			strbuf_addch(&sb, key[i]);
+				strbuf_addch(sb, '\\');
+			strbuf_addch(sb, key[i]);
 		}
-		strbuf_addstr(&sb, "\"]\n");
+		strbuf_addstr(sb, "\"]\n");
 	} else {
-		strbuf_addch(&sb, '[');
-		strbuf_add(&sb, key, store->baselen);
-		strbuf_addstr(&sb, "]\n");
+		strbuf_addch(sb, '[');
+		strbuf_add(sb, key, store->baselen);
+		strbuf_addstr(sb, "]\n");
 	}
-
-	return sb;
 }
 
 static ssize_t write_section(int fd, const char *key,
 			     const struct config_store_data *store)
 {
-	struct strbuf sb = store_create_section(key, store);
+	struct strbuf sb = STRBUF_INIT;
 	ssize_t ret;
 
+	store_create_section(key, store, &sb);
 	ret = write_in_full(fd, sb.buf, sb.len);
 	strbuf_release(&sb);
 
@@ -3833,7 +3832,9 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 						output[0] = '\t';
 					}
 				} else {
-					copystr = store_create_section(new_name, &store);
+					strbuf_reset(&copystr);
+					store_create_section(new_name, &store,
+							     &copystr);
 				}
 			}
 			remove = 0;
#26 lore raw

Re: [PATCH 10/11] config: fix a leak in git_config_copy_or_rename_section_in_file

Quoted 81 lines · Jeff King

config.c static int git_config_copy_or_rename_section_in_file(const char *config_filename · line 3839

Quoted 12 lines · Rubén Justo
config.c +1 -0
index 39a7d7422c..207e4394a3 100644
@@ -3833,6 +3833,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 						output[0] = '\t';
 					}
 				} else {
+					strbuf_release(&copystr);
 					copystr = store_create_section(new_name, &store);
 				}
 			}

Wow, I did a double-take on this code. It is uncommon in our codebase to assign a struct by value in a function return like this, and doubly weird to assign a strbuf (since the whole point of strbuf is to use the opaque functions to make sure we aren't overwriting existing allocations or aliasing pointers).

I think your fix here is the correct thing if we aren't going to clean up the code further.

The more usual thing for our codebase would be refactoring like:

config.c +12 -13
index b79baf83e3..f5a7cced7c 100644
@@ -3140,37 +3140,36 @@ static int write_error(const char *filename)
 	return 4;
 }
 
-static struct strbuf store_create_section(const char *key,
-					  const struct config_store_data *store)
+static void store_create_section(const char *key,
+				 const struct config_store_data *store,
+				 struct strbuf *sb)
 {
 	const char *dot;
 	size_t i;
-	struct strbuf sb = STRBUF_INIT;
 
 	dot = memchr(key, '.', store->baselen);
 	if (dot) {
-		strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
+		strbuf_addf(sb, "[%.*s \"", (int)(dot - key), key);
 		for (i = dot - key + 1; i < store->baselen; i++) {
 			if (key[i] == '"' || key[i] == '\\')
-				strbuf_addch(&sb, '\\');
-			strbuf_addch(&sb, key[i]);
+				strbuf_addch(sb, '\\');
+			strbuf_addch(sb, key[i]);
 		}
-		strbuf_addstr(&sb, "\"]\n");
+		strbuf_addstr(sb, "\"]\n");
 	} else {
-		strbuf_addch(&sb, '[');
-		strbuf_add(&sb, key, store->baselen);
-		strbuf_addstr(&sb, "]\n");
+		strbuf_addch(sb, '[');
+		strbuf_add(sb, key, store->baselen);
+		strbuf_addstr(sb, "]\n");
 	}
-
-	return sb;
 }
 
 static ssize_t write_section(int fd, const char *key,
 			     const struct config_store_data *store)
 {
-	struct strbuf sb = store_create_section(key, store);
+	struct strbuf sb = STRBUF_INIT;
 	ssize_t ret;
 
+	store_create_section(key, store, &sb);
 	ret = write_in_full(fd, sb.buf, sb.len);
 	strbuf_release(&sb);
 

config.c static int git_config_copy_or_rename_section_in_file(const char *config_filename · line 3840

Quoted 10 lines
config.c +3 -1
@@ -3833,7 +3832,9 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 						output[0] = '\t';
 					}
 				} else {
-					copystr = store_create_section(new_name, &store);
+					strbuf_reset(&copystr);
+					store_create_section(new_name, &store,
+							     &copystr);
 				}
 			}
 			remove = 0;

I have a draft with the exact same change. I chose the simple fix, though. I wasn't expecting your review :)

I'll definitely re-roll with this.

Thanks!

#27 lore raw

PATCH 11/11 tests: mark as passing with SANITIZE=leak

In reply to Rubén Justo

The tests listed below, since previous commits, no longer trigger any leak.

+ t1507-rev-parse-upstream.sh
+ t1508-at-combinations.sh
+ t1514-rev-parse-push.sh
+ t2027-checkout-track.sh
+ t3200-branch.sh
+ t3204-branch-name-interpretation.sh
+ t5404-tracking-branches.sh
+ t5517-push-mirror.sh
+ t5525-fetch-tagopt.sh
+ t6040-tracking-info.sh
+ t7508-status.sh

Let's mark them with "TEST_PASSES_SANITIZE_LEAK=true" to notice and fix promptly any new leak that may be introduced and triggered by them, in the future.

As a reference, the list has been formed using:

  $ GIT_TEST_PASSING_SANITIZE_LEAK=check make SANITIZE=leak test
---
 t/t1507-rev-parse-upstream.sh         | 1 +
 t/t1508-at-combinations.sh            | 1 +
 t/t1514-rev-parse-push.sh             | 1 +
 t/t2027-checkout-track.sh             | 1 +
 t/t3200-branch.sh                     | 1 +
 t/t3204-branch-name-interpretation.sh | 1 +
 t/t5404-tracking-branches.sh          | 1 +
 t/t5517-push-mirror.sh                | 1 +
 t/t5525-fetch-tagopt.sh               | 1 +
 t/t6040-tracking-info.sh              | 1 +
 t/t7508-status.sh                     | 1 +
 11 files changed, 11 insertions(+)
t/t1507-rev-parse-upstream.sh +1 -0
index cb9ef7e329..b9af6b3ac0 100755
@@ -5,6 +5,7 @@ test_description='test <branch>@{upstream} syntax'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 
t/t1508-at-combinations.sh +1 -0
index 87a4286414..e841309d0e 100755
@@ -4,6 +4,7 @@ test_description='test various @{X} syntax combinations together'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 check() {
t/t1514-rev-parse-push.sh +1 -0
index d868a08110..a835a196aa 100755
@@ -4,6 +4,7 @@ test_description='test <branch>@{push} syntax'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 resolve () {
t/t2027-checkout-track.sh +1 -0
index dca35aa3e3..a8bbc60954 100755
@@ -5,6 +5,7 @@ test_description='tests for git branch --track'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
t/t3200-branch.sh +1 -0
index 98b6c8ac34..daf1666df7 100755
@@ -8,6 +8,7 @@ test_description='git branch assorted tests'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-rebase.sh
 
t/t3204-branch-name-interpretation.sh +1 -0
index 3399344f25..594e3e43e1 100755
@@ -9,6 +9,7 @@ This script aims to check the behavior of those corner cases.
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 expect_branch() {
t/t5404-tracking-branches.sh +1 -0
index cc07889667..51737eeafe 100755
@@ -5,6 +5,7 @@ test_description='tracking branch update checks for git push'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
t/t5517-push-mirror.sh +1 -0
index a448e169bd..6d4944a728 100755
@@ -5,6 +5,7 @@ test_description='pushing to a mirror repository'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 D=$(pwd)
t/t5525-fetch-tagopt.sh +1 -0
index 45815f7378..3a28f1ded5 100755
@@ -2,6 +2,7 @@
 
 test_description='tagopt variable affects "git fetch" and is overridden by commandline.'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 setup_clone () {
t/t6040-tracking-info.sh +1 -0
index a313849406..7ddbd96e58 100755
@@ -5,6 +5,7 @@ test_description='remote tracking stats'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 advance () {
t/t7508-status.sh +1 -0
index aed07c5b62..4c1f03e609 100755
@@ -5,6 +5,7 @@
 
 test_description='git status'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-terminal.sh
 
Signature · 1 line
2.40.1
#28 lore raw
In reply to Rubén Justo

On 11-jun-2023 20:29:09, Rubén Justo wrote:

Sorry. This should have been:

[PATCH 00/11] tests: mark as passing with SANITIZE=leak

I'll resend with the correct subject, but will wait some time for reviews.

Quoted 65 lines

The goal in this series is to pass t3200 with SANITIZE=leak.

As a result of the fixes, other tests also pass.

This is the list of tests that no longer trigger any leak after this series:

+ t1507-rev-parse-upstream.sh
+ t1508-at-combinations.sh
+ t1514-rev-parse-push.sh
+ t2027-checkout-track.sh
+ t3200-branch.sh
+ t3204-branch-name-interpretation.sh
+ t5404-tracking-branches.sh
+ t5517-push-mirror.sh
+ t5525-fetch-tagopt.sh
+ t6040-tracking-info.sh
+ t7508-status.sh

Each of the commits (except 11/11) fixes a leak. They have no dependencies on each other. As a result, they can be reordered.

To review one leak, the commit can be moved to the tip or reverted. E.g. to review: "branch: fix a leak in check_tracking_branch", this can be used:

  $ git revert --no-edit HEAD~3
  $ make SANITIZE=leak test T=t3200-branch.sh 

Also, each commit have a minimal script in the message that can be used to reproduce the leak.

Rubén Justo (11):
rev-parse: fix a leak with --abbrev-ref
config: fix a leak in git_config_copy_or_rename_section_in_file
remote: fix a leak in query_matches_negative_refspec
branch: fix a leak in dwim_and_setup_tracking
branch: fix a leak in setup_tracking
branch: fix a leak in cmd_branch
branch: fix a leak in inherit_tracking
branch: fix a leak in check_tracking_branch
branch: fix a leak in setup_tracking
config: fix a leak in git_config_copy_or_rename_section_in_file
tests: mark as passing with SANITIZE=leak

 branch.c                              | 14 +++++++++-----
 builtin/branch.c                      |  2 +-
 builtin/rev-parse.c                   |  7 +++++--
 config.c                              |  2 ++
 remote.c                              |  2 +-
 t/t1507-rev-parse-upstream.sh         |  1 +
 t/t1508-at-combinations.sh            |  1 +
 t/t1514-rev-parse-push.sh             |  1 +
 t/t2027-checkout-track.sh             |  1 +
 t/t3200-branch.sh                     |  1 +
 t/t3204-branch-name-interpretation.sh |  1 +
 t/t5404-tracking-branches.sh          |  1 +
 t/t5517-push-mirror.sh                |  1 +
 t/t5525-fetch-tagopt.sh               |  1 +
 t/t6040-tracking-info.sh              |  1 +
 t/t7508-status.sh                     |  1 +
 16 files changed, 29 insertions(+), 9 deletions(-)
Signature · 1 line
2.40.1
#29 lore raw
In reply to Rubén Justo
Quoted 2 lines · Rubén Justo

Each of the commits (except 11/11) fixes a leak. They have no dependencies on each other. As a result, they can be reordered.

Thanks for a pleasant read. I think each case is correct, though I left a few minor comments. Some of them are things I think are worth including in a re-roll. Others are things that we _could_ go further to refactor for clarity, but I am OK if you want to stay focused on the minimal leak-fixes.

-Peff

#30 lore raw
Quoted 5 lines · Jeff King
Quoted 2 lines · Rubén Justo

Each of the commits (except 11/11) fixes a leak. They have no dependencies on each other. As a result, they can be reordered.

Quoted 8 lines

Thanks for a pleasant read. I think each case is correct, though I left a few minor comments. Some of them are things I think are worth including in a re-roll. Others are things that we _could_ go further to refactor for clarity, but I am OK if you want to stay focused on the minimal leak-fixes.

-Peff

Thank you for your prompt reviews, and sorry for my late responses.

#31 lore raw
In reply to Rubén Justo
Quoted 3 lines · Rubén Justo

The goal in this series is to pass t3200 with SANITIZE=leak.

As a result of the fixes, other tests also pass.

Thanks for working on these. I've seen Peff's reviews and picked up only the ones that are unambiguously good, dropping the rest that you may want to update. And as a consequence, 11/11 is also not included in the set that has been queued. Please do include that final step when you resend updated bits that I dropped here.

Thanks.

#32 lore raw

v2 cover tests: mark as passing with SANITIZE=leak

In reply to Rubén Justo

The goal in this series is to pass t3200 with SANITIZE=leak.

As a result of the fixes, other tests also pass.

This is the list of tests that no longer trigger any leak after this series:

+ t1507-rev-parse-upstream.sh
+ t1508-at-combinations.sh
+ t1514-rev-parse-push.sh
+ t2027-checkout-track.sh
+ t3200-branch.sh
+ t3204-branch-name-interpretation.sh
+ t5404-tracking-branches.sh
+ t5517-push-mirror.sh
+ t5525-fetch-tagopt.sh
+ t6040-tracking-info.sh
+ t7508-status.sh

Each of the commits (except 5/5) fixes a leak. They have no dependencies on each other. As a result, they can be reordered.

To review one leak, the commit can be moved to the tip or reverted. E.g. to review: "branch: fix a leak in setup_tracking", this can be used:

  $ git revert --no-edit HEAD~3
  $ make SANITIZE=leak test T=t3200-branch.sh

Also, each commit have a minimal script in the message that can be used to reproduce the leak.

This is the second version of this series. However, a subset of the patches from the first version have already been merged to 'next'. Therefore, those are not included here.

These are the rest of them, which address Peff's reviews.

Thanks.

Rubén Justo (5):
rev-parse: fix a leak with --abbrev-ref
branch: fix a leak in setup_tracking
branch: fix a leak in cmd_branch
config: fix a leak in git_config_copy_or_rename_section_in_file
tests: mark as passing with SANITIZE=leak

 branch.c                              |  2 +-
 builtin/branch.c                      |  2 ++
 builtin/rev-parse.c                   |  5 ++++-
 config.c                              | 29 ++++++++++++++-------------
 t/t1507-rev-parse-upstream.sh         |  1 +
 t/t1508-at-combinations.sh            |  1 +
 t/t1514-rev-parse-push.sh             |  1 +
 t/t2027-checkout-track.sh             |  1 +
 t/t3200-branch.sh                     |  1 +
 t/t3204-branch-name-interpretation.sh |  1 +
 t/t5404-tracking-branches.sh          |  1 +
 t/t5517-push-mirror.sh                |  1 +
 t/t5525-fetch-tagopt.sh               |  1 +
 t/t6040-tracking-info.sh              |  1 +
 t/t7508-status.sh                     |  1 +
 15 files changed, 33 insertions(+), 16 deletions(-)
Signature · 1 line
2.40.1
#33 lore raw

PATCH v2 1/5 rev-parse: fix a leak with --abbrev-ref

To handle "--abbrev-ref" we use shorten_unambiguous_ref(). This function takes a refname and returns a shortened refname, which is a newly allocated string that needs to be freed.

Unfortunately, the refname variable is reused to receive the shortened one. Therefore, we lose the original refname, which needs to be freed as well, producing a leak.

This leak can be reviewed with:

   $ for a in {1..10}; do git branch foo_${a}; done
   $ git rev-parse --abbrev-ref refs/heads/foo_{1..10}

Direct leak of 171 byte(s) in 10 object(s) allocated from:
... in xstrdup wrapper.c
... in expand_ref refs.c
... in repo_dwim_ref refs.c
... in show_rev builtin/rev-parse.c
... in cmd_rev_parse builtin/rev-parse.c
... in run_builtin git.c

We have this leak since a45d34691e (rev-parse: --abbrev-ref option to shorten ref name, 2009-04-13) when "--abbrev-ref" was introduced.

Let's fix it.

---
 builtin/rev-parse.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
builtin/rev-parse.c +4 -1
index 852e49e340..d2eb239a08 100644
@@ -156,9 +156,12 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				 */
 				break;
 			case 1: /* happy */
-				if (abbrev_ref)
+				if (abbrev_ref) {
+					char *old = full;
 					full = shorten_unambiguous_ref(full,
 						abbrev_ref_strict);
+					free(old);
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */
Signature · 1 line
2.40.1
#34 lore raw

PATCH v2 2/5 branch: fix a leak in setup_tracking

In reply to Rubén Justo

In bdaf1dfae7 (branch: new autosetupmerge option "simple" for matching branches, 2022-04-29) a new exit for setup_tracking() missed the clean-up, producing a leak.

   $ git config branch.autoSetupMerge simple
   $ git remote add local .
   $ git update-ref refs/remotes/local/foo HEAD
   $ git branch bar local/foo

Direct leak of 384 byte(s) in 1 object(s) allocated from:
... in xrealloc wrapper.c
... in string_list_append_nodup string-list.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 builtinbranch.c
... in run_builtin git.c

Indirect 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 builtinbranch.c
... in run_builtin git.c

The return introduced in bdaf1dfae7 was to avoid setting up the tracking, but even in that case it is still necessary to do the clean-up. Let's do it.

---
 branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
branch.c +1 -1
index 427bde896f..d88f50a48a 100644
@@ -333,7 +333,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
 		if (!skip_prefix(tracking.srcs->items[0].string,
 				 "refs/heads/", &tracked_branch) ||
 		    strcmp(tracked_branch, new_ref))
-			return;
+			goto cleanup;
 	}
 
 	if (tracking.srcs->nr < 1)
Signature · 1 line
2.40.1
#35 lore raw

PATCH v2 3/5 branch: fix a leak in cmd_branch

In reply to Rubén Justo

In 98e7ab6d42 (for-each-ref: delay parsing of --sort=<atom> options, 2021-10-20) a new string_list was introduced to accumulate any "branch.sort" setting.

That string_list is cleared in ref_sorting_options(), which is only called when processing the "--list" sub-command. Therefore, with other sub-command, while having any sort option set, a leak is produced, e.g.:

   $ git config branch.sort invalid_sort_option
   $ git branch --edit-description

Direct leak of 384 byte(s) in 1 object(s) allocated from:
... in xrealloc wrapper.c
... in string_list_append_nodup string-list.c ... in string_list_append string-list.c
... in git_branch_config builtin/branch.c ... in configset_iter config.c
... in repo_config config.c
... in git_config config.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

Indirect leak of 20 byte(s) in 1 object(s) allocated from:
... in xstrdup wrapper.c
... in string_list_append string-list.c
... in git_branch_config builtin/branch.c ... in configset_iter config.c
... in repo_config config.c
... in git_config config.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

We don't have a common clean-up section in cmd_branch(). To avoid refactoring, keep the fix simple, and while we find a better solution which hopefuly will avoid entirely that string_list, when no sort options are needed; let's squelch the leak sanitizer using UNLEAK().

---
 builtin/branch.c | 2 ++
 1 file changed, 2 insertions(+)
builtin/branch.c +2 -0
index e6c2655af6..075e580d22 100644
@@ -832,6 +832,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (list)
 		setup_auto_pager("branch", 1);
 
+	UNLEAK(sorting_options);
+
 	if (delete) {
 		if (!argc)
 			die(_("branch name required"));
Signature · 1 line
2.40.1
#36 lore raw

PATCH v2 4/5 config: fix a leak in git_config_copy_or_rename_section_in_file

In reply to Rubén Justo

A branch can have its configuration spread over several configuration sections. This situation was already foreseen in 52d59cc645 (branch:
add a --copy (-c) option to go with --move (-m), 2017-06-18) when "branch -c" was introduced.

Unfortunately, a leak was also introduced:

   $ git branch foo
   $ cat >> .git/config <<EOF
   [branch "foo"]
   	some-key-a = a value
   [branch "foo"]
   	some-key-b = b value
   [branch "foo"]
   	some-key-c = c value
   EOF
   $ git branch -c foo bar

Direct leak of 130 byte(s) in 2 object(s) allocated from:
... in xrealloc wrapper.c
... in strbuf_grow strbuf.c
... in strbuf_vaddf strbuf.c
... in strbuf_addf strbuf.c
... in store_create_section config.c
... in git_config_copy_or_rename_section_in_file config.c ... in git_config_copy_section_in_file config.c ... in git_config_copy_section config.c
... in copy_or_rename_branch builtin/branch.c ... in cmd_branch builtin/branch.c
... in run_builtin git.c

Let's fix it.

Let's also modify the function store_create_section() so that it stops returning a strbuf, which is an uncommon pattern in our code base, and instead, starts receiving the strbuf to use from its caller.

---
 config.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
config.c +15 -14
index 39a7d7422c..c5f4b59ef3 100644
@@ -3140,37 +3140,36 @@ static int write_error(const char *filename)
 	return 4;
 }
 
-static struct strbuf store_create_section(const char *key,
-					  const struct config_store_data *store)
+static void store_create_section(const char *key,
+				const struct config_store_data *store,
+				struct strbuf *sb)
 {
 	const char *dot;
 	size_t i;
-	struct strbuf sb = STRBUF_INIT;
 
 	dot = memchr(key, '.', store->baselen);
 	if (dot) {
-		strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
+		strbuf_addf(sb, "[%.*s \"", (int)(dot - key), key);
 		for (i = dot - key + 1; i < store->baselen; i++) {
 			if (key[i] == '"' || key[i] == '\\')
-				strbuf_addch(&sb, '\\');
-			strbuf_addch(&sb, key[i]);
+				strbuf_addch(sb, '\\');
+			strbuf_addch(sb, key[i]);
 		}
-		strbuf_addstr(&sb, "\"]\n");
+		strbuf_addstr(sb, "\"]\n");
 	} else {
-		strbuf_addch(&sb, '[');
-		strbuf_add(&sb, key, store->baselen);
-		strbuf_addstr(&sb, "]\n");
+		strbuf_addch(sb, '[');
+		strbuf_add(sb, key, store->baselen);
+		strbuf_addstr(sb, "]\n");
 	}
-
-	return sb;
 }
 
 static ssize_t write_section(int fd, const char *key,
 			     const struct config_store_data *store)
 {
-	struct strbuf sb = store_create_section(key, store);
+	struct strbuf sb = STRBUF_INIT;
 	ssize_t ret;
 
+	store_create_section(key, store, &sb);
 	ret = write_in_full(fd, sb.buf, sb.len);
 	strbuf_release(&sb);
 
@@ -3833,7 +3832,9 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 						output[0] = '\t';
 					}
 				} else {
-					copystr = store_create_section(new_name, &store);
+					strbuf_reset(&copystr);
+					store_create_section(new_name, &store,
+							     &copystr);
 				}
 			}
 			remove = 0;
Signature · 1 line
2.40.1
#37 lore raw

PATCH v2 5/5 tests: mark as passing with SANITIZE=leak

In reply to Rubén Justo

The tests listed below, since previous commits, no longer trigger any leak.

+ t1507-rev-parse-upstream.sh
+ t1508-at-combinations.sh
+ t1514-rev-parse-push.sh
+ t2027-checkout-track.sh
+ t3200-branch.sh
+ t3204-branch-name-interpretation.sh
+ t5404-tracking-branches.sh
+ t5517-push-mirror.sh
+ t5525-fetch-tagopt.sh
+ t6040-tracking-info.sh
+ t7508-status.sh

Let's mark them with "TEST_PASSES_SANITIZE_LEAK=true" to notice and fix promptly any new leak that may be introduced and triggered by them in the future.

---
 t/t1507-rev-parse-upstream.sh         | 1 +
 t/t1508-at-combinations.sh            | 1 +
 t/t1514-rev-parse-push.sh             | 1 +
 t/t2027-checkout-track.sh             | 1 +
 t/t3200-branch.sh                     | 1 +
 t/t3204-branch-name-interpretation.sh | 1 +
 t/t5404-tracking-branches.sh          | 1 +
 t/t5517-push-mirror.sh                | 1 +
 t/t5525-fetch-tagopt.sh               | 1 +
 t/t6040-tracking-info.sh              | 1 +
 t/t7508-status.sh                     | 1 +
 11 files changed, 11 insertions(+)
t/t1507-rev-parse-upstream.sh +1 -0
index cb9ef7e329..b9af6b3ac0 100755
@@ -5,6 +5,7 @@ test_description='test <branch>@{upstream} syntax'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 
t/t1508-at-combinations.sh +1 -0
index 87a4286414..e841309d0e 100755
@@ -4,6 +4,7 @@ test_description='test various @{X} syntax combinations together'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 check() {
t/t1514-rev-parse-push.sh +1 -0
index d868a08110..a835a196aa 100755
@@ -4,6 +4,7 @@ test_description='test <branch>@{push} syntax'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 resolve () {
t/t2027-checkout-track.sh +1 -0
index dca35aa3e3..a8bbc60954 100755
@@ -5,6 +5,7 @@ test_description='tests for git branch --track'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
t/t3200-branch.sh +1 -0
index 98b6c8ac34..daf1666df7 100755
@@ -8,6 +8,7 @@ test_description='git branch assorted tests'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-rebase.sh
 
t/t3204-branch-name-interpretation.sh +1 -0
index 3399344f25..594e3e43e1 100755
@@ -9,6 +9,7 @@ This script aims to check the behavior of those corner cases.
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 expect_branch() {
t/t5404-tracking-branches.sh +1 -0
index cc07889667..51737eeafe 100755
@@ -5,6 +5,7 @@ test_description='tracking branch update checks for git push'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
t/t5517-push-mirror.sh +1 -0
index a448e169bd..6d4944a728 100755
@@ -5,6 +5,7 @@ test_description='pushing to a mirror repository'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 D=$(pwd)
t/t5525-fetch-tagopt.sh +1 -0
index 45815f7378..3a28f1ded5 100755
@@ -2,6 +2,7 @@
 
 test_description='tagopt variable affects "git fetch" and is overridden by commandline.'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 setup_clone () {
t/t6040-tracking-info.sh +1 -0
index a313849406..7ddbd96e58 100755
@@ -5,6 +5,7 @@ test_description='remote tracking stats'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 advance () {
t/t7508-status.sh +1 -0
index aed07c5b62..4c1f03e609 100755
@@ -5,6 +5,7 @@
 
 test_description='git status'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-terminal.sh
 
Signature · 1 line
2.40.1
#38 lore raw
In reply to Rubén Justo
Quoted 5 lines · Rubén Justo

This is the second version of this series. However, a subset of the patches from the first version have already been merged to 'next'. Therefore, those are not included here.

These are the rest of them, which address Peff's reviews.

Thanks!

#39 lore raw
In reply to Rubén Justo
Quoted 5 lines · Rubén Justo

This is the second version of this series. However, a subset of the patches from the first version have already been merged to 'next'. Therefore, those are not included here.

These are the rest of them, which address Peff's reviews.

These all look good to me. I'd probably have split the config.c refactor into its own commit, but I don't think it's worth going back for.

Thanks Rubén for fixing these!

-Peff

#40 lore raw
Quoted 2 lines · Jeff King

These all look good to me. I'd probably have split the config.c refactor into its own commit

That's a good point. I'll send the refactor when the dust settles, along with a revision for the UNLEAK().

Thank you!

#41 lore raw

v3 cover tests: mark as passing with SANITIZE=leak

In reply to Rubén Justo

The goal in this series is to pass t3200 with SANITIZE=leak.

As a result of the fixes, other tests also pass.

This is the list of tests that no longer trigger any leak after this series:

+ t1507-rev-parse-upstream.sh
+ t1508-at-combinations.sh
+ t1514-rev-parse-push.sh
+ t2027-checkout-track.sh
+ t3200-branch.sh
+ t3204-branch-name-interpretation.sh
+ t5404-tracking-branches.sh
+ t5517-push-mirror.sh
+ t5525-fetch-tagopt.sh
+ t6040-tracking-info.sh
+ t7508-status.sh

Each of the commits (except 5/5) fixes a leak. They have no dependencies on each other. As a result, they can be reordered.

To review one leak, the commit can be moved to the tip or reverted. E.g. to review: "branch: fix a leak in setup_tracking", this can be used:

  $ git revert --no-edit HEAD~3
  $ make SANITIZE=leak test T=t3200-branch.sh

Also, each commit have a minimal script in the message that can be used to reproduce the leak.

This is the third version of this series. The refactoring in config.c has been reverted, and will be sent outside of this series.

Sorry for the noise of a quick re-roll.

Thanks.

Rubén Justo (5):
rev-parse: fix a leak with --abbrev-ref
branch: fix a leak in setup_tracking
branch: fix a leak in cmd_branch
config: fix a leak in git_config_copy_or_rename_section_in_file
tests: mark as passing with SANITIZE=leak

 branch.c                              | 2 +-
 builtin/branch.c                      | 2 ++
 builtin/rev-parse.c                   | 5 ++++-
 config.c                              | 1 +
 t/t1507-rev-parse-upstream.sh         | 1 +
 t/t1508-at-combinations.sh            | 1 +
 t/t1514-rev-parse-push.sh             | 1 +
 t/t2027-checkout-track.sh             | 1 +
 t/t3200-branch.sh                     | 1 +
 t/t3204-branch-name-interpretation.sh | 1 +
 t/t5404-tracking-branches.sh          | 1 +
 t/t5517-push-mirror.sh                | 1 +
 t/t5525-fetch-tagopt.sh               | 1 +
 t/t6040-tracking-info.sh              | 1 +
 t/t7508-status.sh                     | 1 +
 15 files changed, 19 insertions(+), 2 deletions(-)
Signature · 1 line
2.40.1
#42 lore raw

PATCH v3 1/5 rev-parse: fix a leak with --abbrev-ref

To handle "--abbrev-ref" we use shorten_unambiguous_ref(). This function takes a refname and returns a shortened refname, which is a newly allocated string that needs to be freed.

Unfortunately, the refname variable is reused to receive the shortened one. Therefore, we lose the original refname, which needs to be freed as well, producing a leak.

This leak can be reviewed with:

   $ for a in {1..10}; do git branch foo_${a}; done
   $ git rev-parse --abbrev-ref refs/heads/foo_{1..10}

Direct leak of 171 byte(s) in 10 object(s) allocated from:
... in xstrdup wrapper.c
... in expand_ref refs.c
... in repo_dwim_ref refs.c
... in show_rev builtin/rev-parse.c
... in cmd_rev_parse builtin/rev-parse.c
... in run_builtin git.c

We have this leak since a45d34691e (rev-parse: --abbrev-ref option to shorten ref name, 2009-04-13) when "--abbrev-ref" was introduced.

Let's fix it.

---
 builtin/rev-parse.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
builtin/rev-parse.c +4 -1
index 852e49e340..d2eb239a08 100644
@@ -156,9 +156,12 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				 */
 				break;
 			case 1: /* happy */
-				if (abbrev_ref)
+				if (abbrev_ref) {
+					char *old = full;
 					full = shorten_unambiguous_ref(full,
 						abbrev_ref_strict);
+					free(old);
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */
Signature · 1 line
2.40.1
#43 lore raw

PATCH v3 2/5 branch: fix a leak in setup_tracking

In reply to Rubén Justo

In bdaf1dfae7 (branch: new autosetupmerge option "simple" for matching branches, 2022-04-29) a new exit for setup_tracking() missed the clean-up, producing a leak.

   $ git config branch.autoSetupMerge simple
   $ git remote add local .
   $ git update-ref refs/remotes/local/foo HEAD
   $ git branch bar local/foo

Direct leak of 384 byte(s) in 1 object(s) allocated from:
... in xrealloc wrapper.c
... in string_list_append_nodup string-list.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 builtinbranch.c
... in run_builtin git.c

Indirect 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 builtinbranch.c
... in run_builtin git.c

The return introduced in bdaf1dfae7 was to avoid setting up the tracking, but even in that case it is still necessary to do the clean-up. Let's do it.

---
 branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
branch.c +1 -1
index 427bde896f..d88f50a48a 100644
@@ -333,7 +333,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
 		if (!skip_prefix(tracking.srcs->items[0].string,
 				 "refs/heads/", &tracked_branch) ||
 		    strcmp(tracked_branch, new_ref))
-			return;
+			goto cleanup;
 	}
 
 	if (tracking.srcs->nr < 1)
Signature · 1 line
2.40.1
#44 lore raw

PATCH v3 3/5 branch: fix a leak in cmd_branch

In reply to Rubén Justo

In 98e7ab6d42 (for-each-ref: delay parsing of --sort=<atom> options, 2021-10-20) a new string_list was introduced to accumulate any "branch.sort" setting.

That string_list is cleared in ref_sorting_options(), which is only called when processing the "--list" sub-command. Therefore, with other sub-command, while having any sort option set, a leak is produced, e.g.:

   $ git config branch.sort invalid_sort_option
   $ git branch --edit-description

Direct leak of 384 byte(s) in 1 object(s) allocated from:
... in xrealloc wrapper.c
... in string_list_append_nodup string-list.c ... in string_list_append string-list.c
... in git_branch_config builtin/branch.c ... in configset_iter config.c
... in repo_config config.c
... in git_config config.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

Indirect leak of 20 byte(s) in 1 object(s) allocated from:
... in xstrdup wrapper.c
... in string_list_append string-list.c
... in git_branch_config builtin/branch.c ... in configset_iter config.c
... in repo_config config.c
... in git_config config.c
... in cmd_branch builtin/branch.c
... in run_builtin git.c

We don't have a common clean-up section in cmd_branch(). To avoid refactoring, keep the fix simple, and while we find a better solution which hopefuly will avoid entirely that string_list, when no sort options are needed; let's squelch the leak sanitizer using UNLEAK().

---
 builtin/branch.c | 2 ++
 1 file changed, 2 insertions(+)
builtin/branch.c +2 -0
index e6c2655af6..075e580d22 100644
@@ -832,6 +832,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (list)
 		setup_auto_pager("branch", 1);
 
+	UNLEAK(sorting_options);
+
 	if (delete) {
 		if (!argc)
 			die(_("branch name required"));
Signature · 1 line
2.40.1
#45 lore raw

PATCH v3 4/5 config: fix a leak in git_config_copy_or_rename_section_in_file

In reply to Rubén Justo

A branch can have its configuration spread over several configuration sections. This situation was already foreseen in 52d59cc645 (branch:
add a --copy (-c) option to go with --move (-m), 2017-06-18) when "branch -c" was introduced.

Unfortunately, a leak was also introduced:

   $ git branch foo
   $ cat >> .git/config <<EOF
   [branch "foo"]
   	some-key-a = a value
   [branch "foo"]
   	some-key-b = b value
   [branch "foo"]
   	some-key-c = c value
   EOF
   $ git branch -c foo bar

Direct leak of 130 byte(s) in 2 object(s) allocated from:
... in xrealloc wrapper.c
... in strbuf_grow strbuf.c
... in strbuf_vaddf strbuf.c
... in strbuf_addf strbuf.c
... in store_create_section config.c
... in git_config_copy_or_rename_section_in_file config.c ... in git_config_copy_section_in_file config.c ... in git_config_copy_section config.c
... in copy_or_rename_branch builtin/branch.c ... in cmd_branch builtin/branch.c
... in run_builtin git.c

Let's fix it.

---
 config.c | 1 +
 1 file changed, 1 insertion(+)
config.c +1 -0
index 39a7d7422c..207e4394a3 100644
@@ -3833,6 +3833,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 						output[0] = '\t';
 					}
 				} else {
+					strbuf_release(&copystr);
 					copystr = store_create_section(new_name, &store);
 				}
 			}
Signature · 1 line
2.40.1
#46 lore raw

PATCH v3 5/5 tests: mark as passing with SANITIZE=leak

In reply to Rubén Justo

The tests listed below, since previous commits, no longer trigger any leak.

+ t1507-rev-parse-upstream.sh
+ t1508-at-combinations.sh
+ t1514-rev-parse-push.sh
+ t2027-checkout-track.sh
+ t3200-branch.sh
+ t3204-branch-name-interpretation.sh
+ t5404-tracking-branches.sh
+ t5517-push-mirror.sh
+ t5525-fetch-tagopt.sh
+ t6040-tracking-info.sh
+ t7508-status.sh

Let's mark them with "TEST_PASSES_SANITIZE_LEAK=true" to notice and fix promptly any new leak that may be introduced and triggered by them in the future.

---
 t/t1507-rev-parse-upstream.sh         | 1 +
 t/t1508-at-combinations.sh            | 1 +
 t/t1514-rev-parse-push.sh             | 1 +
 t/t2027-checkout-track.sh             | 1 +
 t/t3200-branch.sh                     | 1 +
 t/t3204-branch-name-interpretation.sh | 1 +
 t/t5404-tracking-branches.sh          | 1 +
 t/t5517-push-mirror.sh                | 1 +
 t/t5525-fetch-tagopt.sh               | 1 +
 t/t6040-tracking-info.sh              | 1 +
 t/t7508-status.sh                     | 1 +
 11 files changed, 11 insertions(+)
t/t1507-rev-parse-upstream.sh +1 -0
index cb9ef7e329..b9af6b3ac0 100755
@@ -5,6 +5,7 @@ test_description='test <branch>@{upstream} syntax'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 
t/t1508-at-combinations.sh +1 -0
index 87a4286414..e841309d0e 100755
@@ -4,6 +4,7 @@ test_description='test various @{X} syntax combinations together'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 check() {
t/t1514-rev-parse-push.sh +1 -0
index d868a08110..a835a196aa 100755
@@ -4,6 +4,7 @@ test_description='test <branch>@{push} syntax'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 resolve () {
t/t2027-checkout-track.sh +1 -0
index dca35aa3e3..a8bbc60954 100755
@@ -5,6 +5,7 @@ test_description='tests for git branch --track'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
t/t3200-branch.sh +1 -0
index 98b6c8ac34..daf1666df7 100755
@@ -8,6 +8,7 @@ test_description='git branch assorted tests'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-rebase.sh
 
t/t3204-branch-name-interpretation.sh +1 -0
index 3399344f25..594e3e43e1 100755
@@ -9,6 +9,7 @@ This script aims to check the behavior of those corner cases.
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 expect_branch() {
t/t5404-tracking-branches.sh +1 -0
index cc07889667..51737eeafe 100755
@@ -5,6 +5,7 @@ test_description='tracking branch update checks for git push'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
t/t5517-push-mirror.sh +1 -0
index a448e169bd..6d4944a728 100755
@@ -5,6 +5,7 @@ test_description='pushing to a mirror repository'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 D=$(pwd)
t/t5525-fetch-tagopt.sh +1 -0
index 45815f7378..3a28f1ded5 100755
@@ -2,6 +2,7 @@
 
 test_description='tagopt variable affects "git fetch" and is overridden by commandline.'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 setup_clone () {
t/t6040-tracking-info.sh +1 -0
index a313849406..7ddbd96e58 100755
@@ -5,6 +5,7 @@ test_description='remote tracking stats'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 advance () {
t/t7508-status.sh +1 -0
index aed07c5b62..4c1f03e609 100755
@@ -5,6 +5,7 @@
 
 test_description='git status'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-terminal.sh
 
Signature · 1 line
2.40.1