Skip to content

Commit 0b91d56

Browse files
committed
Merge branch 'gc/zero-length-branch-config-fix'
A misconfigured 'branch..remote' led to a bug in configuration parsing. * gc/zero-length-branch-config-fix: remote.c: reject 0-length branch names remote.c: don't BUG() on 0-length branch names
2 parents c21fa3b + f1dfbd9 commit 0b91d56

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

remote.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ static struct branch *find_branch(struct remote_state *remote_state,
196196
struct branches_hash_key lookup;
197197
struct hashmap_entry lookup_entry, *e;
198198

199-
if (!len)
200-
len = strlen(name);
201-
202199
lookup.str = name;
203200
lookup.len = len;
204201
hashmap_entry_init(&lookup_entry, memhash(name, len));
@@ -215,7 +212,8 @@ static void die_on_missing_branch(struct repository *repo,
215212
{
216213
/* branch == NULL is always valid because it represents detached HEAD. */
217214
if (branch &&
218-
branch != find_branch(repo->remote_state, branch->name, 0))
215+
branch != find_branch(repo->remote_state, branch->name,
216+
strlen(branch->name)))
219217
die("branch %s was not found in the repository", branch->name);
220218
}
221219

@@ -355,8 +353,12 @@ static int handle_config(const char *key, const char *value, void *cb)
355353
struct remote_state *remote_state = cb;
356354

357355
if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
356+
/* There is no subsection. */
358357
if (!name)
359358
return 0;
359+
/* There is a subsection, but it is empty. */
360+
if (!namelen)
361+
return -1;
360362
branch = make_branch(remote_state, name, namelen);
361363
if (!strcmp(subkey, "remote")) {
362364
return git_config_string(&branch->remote_name, key, value);

t/t5516-fetch-push.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,26 @@ test_expect_success 'branch.*.pushremote config order is irrelevant' '
598598
check_push_result two_repo $the_commit heads/main
599599
'
600600

601+
test_expect_success 'push rejects empty branch name entries' '
602+
mk_test one_repo heads/main &&
603+
test_config remote.one.url one_repo &&
604+
test_config branch..remote one &&
605+
test_config branch..merge refs/heads/ &&
606+
test_config branch.main.remote one &&
607+
test_config branch.main.merge refs/heads/main &&
608+
test_must_fail git push 2>err &&
609+
grep "bad config variable .branch\.\." err
610+
'
611+
612+
test_expect_success 'push ignores "branch." config without subsection' '
613+
mk_test one_repo heads/main &&
614+
test_config remote.one.url one_repo &&
615+
test_config branch.autoSetupMerge true &&
616+
test_config branch.main.remote one &&
617+
test_config branch.main.merge refs/heads/main &&
618+
git push
619+
'
620+
601621
test_expect_success 'push with dry-run' '
602622
603623
mk_test testrepo heads/main &&

0 commit comments

Comments
 (0)