Message ID | df6e4db6072e90afc92505a73792cf3c3221d5e4.1654038754.git.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | Accepted |
Commit | 91e2e8f63ebd92295ff0eb5f4095f9e1fba8bab0 |
Headers | show |
Series | remote.c: reject 0-length branch names | expand |
On Tue, May 31, 2022 at 11:12:33PM +0000, Glen Choo via GitGitGadget wrote: > Fix the bug by removing the convenience strlen functionality, so that > 0 means that the string is 0-length. We still insert a bogus branch name > into the hash map, but this will be fixed in a later commit. I think this is a good change, regardless of whether we take the second commit or not. These kind of "automagically run strlen() sometimes" interfaces are subtle, and I think have bitten us before. > diff --git a/remote.c b/remote.c > index 42a4e7106e1..cf7015ae8ab 100644 > --- a/remote.c > +++ b/remote.c > @@ -195,9 +195,6 @@ static struct branch *find_branch(struct remote_state *remote_state, > struct branches_hash_key lookup; > struct hashmap_entry lookup_entry, *e; > > - if (!len) > - len = strlen(name); > - > lookup.str = name; > lookup.len = len; > hashmap_entry_init(&lookup_entry, memhash(name, len)); This changes the behavior of find_branch() without changing its signature. So any topics in flight that use it might be subtly broken. I think that's probably OK in this instance, since it's a file-local static, and was added relatively recently (i.e., the blast radius is pretty small and unlikely). -Peff
diff --git a/remote.c b/remote.c index 42a4e7106e1..cf7015ae8ab 100644 --- a/remote.c +++ b/remote.c @@ -195,9 +195,6 @@ static struct branch *find_branch(struct remote_state *remote_state, struct branches_hash_key lookup; struct hashmap_entry lookup_entry, *e; - if (!len) - len = strlen(name); - lookup.str = name; lookup.len = len; hashmap_entry_init(&lookup_entry, memhash(name, len)); @@ -214,7 +211,8 @@ static void die_on_missing_branch(struct repository *repo, { /* branch == NULL is always valid because it represents detached HEAD. */ if (branch && - branch != find_branch(repo->remote_state, branch->name, 0)) + branch != find_branch(repo->remote_state, branch->name, + strlen(branch->name))) die("branch %s was not found in the repository", branch->name); } diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index 4dfb080433e..a05268952e9 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -598,6 +598,16 @@ 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' ' + 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 && + git push +' + test_expect_success 'push with dry-run' ' mk_test testrepo heads/main &&