remote.c: reject 0-length branch names

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed May 31, 2022 at 23:12 UTC f1dfbd9ee010e5cdf0d931d16b4b2892b33331e5
2 files changed +15 -1
remote.c
+4
@@ -352,8 +352,12 @@ static int handle_config(const char *key, const char *value, void *cb)
352 struct remote_state *remote_state = cb;
353
354 if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
355 + /* There is no subsection. */
356 if (!name)
357 return 0;
358 + /* There is a subsection, but it is empty. */
359 + if (!namelen)
360 + return -1;
361 branch = make_branch(remote_state, name, namelen);
362 if (!strcmp(subkey, "remote")) {
363 return git_config_string(&branch->remote_name, key, value);
t/t5516-fetch-push.sh
+11 -1
@@ -602,13 +602,23 @@ test_expect_success 'branch.*.pushremote config order is irrelevant' '
602 check_push_result two_repo $the_commit heads/main
603 '
604
605 -test_expect_success 'push ignores empty branch name entries' '
605 +test_expect_success 'push rejects empty branch name entries' '
606 mk_test one_repo heads/main &&
607 test_config remote.one.url one_repo &&
608 test_config branch..remote one &&
609 test_config branch..merge refs/heads/ &&
610 test_config branch.main.remote one &&
611 test_config branch.main.merge refs/heads/main &&
612 + test_must_fail git push 2>err &&
613 + grep "bad config variable .branch\.\." err
614 +'
615 +
616 +test_expect_success 'push ignores "branch." config without subsection' '
617 + mk_test one_repo heads/main &&
618 + test_config remote.one.url one_repo &&
619 + test_config branch.autoSetupMerge true &&
620 + test_config branch.main.remote one &&
621 + test_config branch.main.merge refs/heads/main &&
622 git push
623 '
624