git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/6] remote: check branch->merge before access in branch_release

From
Jacob Keller <jacob.e.keller@intel.com>
Date
Jun 11, 2025, 00:52 UTC
Message-ID
<20250610-jk-submodule-helper-use-url-v1-3-6d14c1504e91@gmail.com>
In-Reply-To
<20250610-jk-submodule-helper-use-url-v1-0-6d14c1504e91@gmail.com>
From: Jacob Keller <jacob.keller@gmail.com>

The branch_release function doesn't check the merge field before accessing it, based on an assumption that merge will be set if branch->merge_nr is non-zero.

This is not always the case. It turns out that branch->merge is not initialized until set_merge() is called, but branch->merge_nr can be non-zero from handle_config() calling add_merge().

set_merge() is not called until branch_get(). This function does set merge_nr to zero if merge is not initialized. However, branch_release is called on every branch when tearing down a repository.

An upcoming change to submodule--helper will initialize the remote state by calling read_config(). In some cases, this results in branches in the remote_state which have a non-zero merge_nr but no merge array. This results in a crash when tearing the repository down.

To fix this, lets simply check if merge is valid before attempting to release its contents.

This makes it safe to initialize the remote_state for a submodule repository without crashing on teardown.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
---
 remote.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/remote.c b/remote.c
index 7092b12209d93e20ce1ed3b7d9e4cbac058c57ff..1035f0cd32d034cce05bd2a3d829ec90795ff4e2 100644
--- a/remote.c
+++ b/remote.c
@@ -253,9 +253,11 @@ static void branch_release(struct branch *branch)
 	free((char *)branch->refname);
 	free(branch->remote_name);
 	free(branch->pushremote_name);
-	for (int i = 0; i < branch->merge_nr; i++)
-		refspec_item_clear(branch->merge[i]);
-	free(branch->merge);
+	if (branch->merge) {
+		for (int i = 0; i < branch->merge_nr; i++)
+			refspec_item_clear(branch->merge[i]);
+		free(branch->merge);
+	}
 }
 
 static struct rewrite *make_rewrite(struct rewrites *r,
-- 
2.48.1.397.gec9d649cc640
Previous: Jacob KellerNext: Jacob Keller
Message 4 of 17 in “submodule: improve remote lookup logic”
  1. 0/6 submodule: improve remote lookup logicJacob Keller, Jun 11, 2025
  2. 1/6 dir: move starts_with_dot(_dot)_slash to dir.hJacob Keller, Jun 11, 2025
  3. 2/6 remote: remove the_repository from some functionsJacob Keller, Jun 11, 2025
  4. 3/6 remote: check branch->merge before access in branch_releaseJacob Keller, Jun 11, 2025
  5. 4/6 submodule--helper: improve logic for fallback remote nameJacob Keller, Jun 11, 2025
  6. Lidong YanJun 17, 2025
  7. Jacob KellerJun 17, 2025
  8. Jacob KellerJun 17, 2025
  9. Junio C HamanoJun 17, 2025
  10. 5/6 submodule: move get_default_remote_submodule()Jacob Keller, Jun 11, 2025
  11. 6/6 submodule: look up remotes by URL firstJacob Keller, Jun 11, 2025
  12. Jacob KellerJun 16, 2025
  13. Junio C HamanoJun 16, 2025
  14. Jacob KellerJun 16, 2025
  15. Junio C HamanoJun 17, 2025
  16. Junio C HamanoJun 17, 2025
  17. Jacob KellerJun 17, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.