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

[PATCH 2/2] remote.c: reject 0-length branch names

From
Glen Choo via GitGitGadget <gitgitgadget@gmail.com>
Date
May 31, 2022, 23:12 UTC
Message-ID
<f947cf221c0b5320d0b7438b88a0d94a5bd3a70b.1654038754.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1273.git.git.1654038754.gitgitgadget@gmail.com>
From: Glen Choo <chooglen@google.com>

Branch names can't be empty, so config keys with an empty branch name, e.g. "branch..remote", are silently ignored.

Since these config keys will never be useful, make it a fatal error when remote.c finds a key that starts with "branch." and has an empty subsection.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 remote.c              |  4 ++++
 t/t5516-fetch-push.sh | 12 +++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/remote.c b/remote.c
index cf7015ae8ab..a3888dd789c 100644
--- a/remote.c
+++ b/remote.c
@@ -352,8 +352,12 @@ static int handle_config(const char *key, const char *value, void *cb)
 	struct remote_state *remote_state = cb;
 
 	if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
+		/* There is no subsection. */
 		if (!name)
 			return 0;
+		/* There is a subsection, but it is empty. */
+		if (!namelen)
+			return -1;
 		branch = make_branch(remote_state, name, namelen);
 		if (!strcmp(subkey, "remote")) {
 			return git_config_string(&branch->remote_name, key, value);
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index a05268952e9..e99c31f8c35 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -598,13 +598,23 @@ test_expect_success 'branch.*.pushremote config order is irrelevant' '
 	check_push_result two_repo $the_commit heads/main
 '
 
-test_expect_success 'push ignores empty branch name entries' '
+test_expect_success 'push rejects empty branch name entries' '
 	mk_test one_repo heads/main &&
 	test_config remote.one.url one_repo &&
 	test_config branch..remote one &&
 	test_config branch..merge refs/heads/ &&
 	test_config branch.main.remote one &&
 	test_config branch.main.merge refs/heads/main &&
+	test_must_fail git push 2>err &&
+	grep "bad config variable .branch\.\." err
+'
+
+test_expect_success 'push ignores "branch." config without subsection' '
+	mk_test one_repo heads/main &&
+	test_config remote.one.url one_repo &&
+	test_config branch.autoSetupMerge true &&
+	test_config branch.main.remote one &&
+	test_config branch.main.merge refs/heads/main &&
 	git push
 '
 
-- 
gitgitgadget
Previous: Jeff KingNext: Ævar Arnfjörð Bjarmason
Message 4 of 8 in “remote.c: reject 0-length branch names”
  1. 0/2 remote.c: reject 0-length branch namesGlen Choo via GitGitGadget, May 31, 2022
  2. 1/2 remote.c: don't BUG() on 0-length branch namesGlen Choo via GitGitGadget, May 31, 2022
  3. Jeff KingJun 8, 2022
  4. 2/2 remote.c: reject 0-length branch namesGlen Choo via GitGitGadget, May 31, 2022
  5. Ævar Arnfjörð BjarmasonJun 1, 2022
  6. Glen ChooJun 1, 2022
  7. Junio C HamanoJun 1, 2022
  8. Jeff KingJun 8, 2022

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.