Skip to content

Commit

Permalink
Prevent potential segfault in scalar reconfigure --all (#647)
Browse files Browse the repository at this point in the history
See gitgitgadget#1724 for the upstream fix. This includes an extra
detail around the context of that change that will require some reaction
in microsoft/git. Please see the `fixup!` and merge commit messages for
details.

This merges in the upstream commit that is going into `master` soon.
  • Loading branch information
dscho authored May 14, 2024
2 parents b68812e + 633892e commit a7790b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
14 changes: 8 additions & 6 deletions scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,6 @@ static int cmd_reconfigure(int argc, const char **argv)
};
struct string_list scalar_repos = STRING_LIST_INIT_DUP;
int i, res = 0;
struct repository r = { NULL };
struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;

argc = parse_options(argc, argv, NULL, options,
Expand All @@ -1048,6 +1047,7 @@ static int cmd_reconfigure(int argc, const char **argv)

for (i = 0; i < scalar_repos.nr; i++) {
int succeeded = 0;
struct repository *old_repo, r = { NULL };
const char *dir = scalar_repos.items[i].string;

strbuf_reset(&commondir);
Expand Down Expand Up @@ -1095,15 +1095,17 @@ static int cmd_reconfigure(int argc, const char **argv)

git_config_clear();

if (repo_init(&r, gitdir.buf, commondir.buf))
goto loop_end;

old_repo = the_repository;
the_repository = &r;
r.commondir = commondir.buf;
r.gitdir = gitdir.buf;

if (set_recommended_config(1) >= 0)
if (set_recommended_config(1) >= 0 &&
toggle_maintenance(1) >= 0)
succeeded = 1;

if (toggle_maintenance(1) >= 0)
succeeded = 1;
the_repository = old_repo;

loop_end:
if (!succeeded) {
Expand Down
38 changes: 38 additions & 0 deletions t/t9210-scalar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,44 @@ test_expect_success 'scalar reconfigure' '
test_subcommand git maintenance start <reconfigure
'

test_expect_success 'scalar reconfigure --all with includeIf.onbranch' '
repos="two three four" &&
for num in $repos
do
git init $num/src &&
scalar register $num/src &&
git -C $num/src config includeif."onbranch:foo".path something &&
git -C $num/src config core.preloadIndex false || return 1
done &&
scalar reconfigure --all &&
for num in $repos
do
test true = "$(git -C $num/src config core.preloadIndex)" || return 1
done
'

test_expect_success 'scalar reconfigure --all with detached HEADs' '
repos="two three four" &&
for num in $repos
do
rm -rf $num/src &&
git init $num/src &&
scalar register $num/src &&
git -C $num/src config core.preloadIndex false &&
test_commit -C $num/src initial &&
git -C $num/src switch --detach HEAD || return 1
done &&
scalar reconfigure --all &&
for num in $repos
do
test true = "$(git -C $num/src config core.preloadIndex)" || return 1
done
'

test_expect_success '`reconfigure -a` removes stale config entries' '
git init stale/src &&
scalar register stale &&
Expand Down

0 comments on commit a7790b9

Please sign in to comment.