Skip to content

Commit 000c4ce

Browse files
newrengitster
authored andcommitted
merge-ort: fix calling merge_finalize() with no intermediate merge
If some code sets up the data structures for a merge, but then never actually performs one before calling merge_finalize(), then merge_finalize() wouldn't notice that result->priv was NULL and return early, resulting in following that NULL pointer and getting a segfault. There is currently no code in the git codebase that does this, but this issue was found during testing of some proposed patches that had the following structure: struct merge_options merge_opt; struct merge_result result; init_merge_options(&merge_opt, the_repository); memset(&result, 0, sizeof(result)); <do N merges, for some value of N> merge_finalize(&merge_opt, &result); where some flags could cause the code to have N=0, i.e. doing no merges. Add a check for result->priv being NULL and return early to avoid a segfault in these kinds of cases. While at it, ensure the FREE_AND_NULL() in the function does something useful with the nulling aspect, namely sets result->priv to NULL rather than a mere temporary. Reported-by: Derrick Stolee <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7556e5d commit 000c4ce

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

merge-ort.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4683,14 +4683,14 @@ void merge_switch_to_result(struct merge_options *opt,
46834683
void merge_finalize(struct merge_options *opt,
46844684
struct merge_result *result)
46854685
{
4686-
struct merge_options_internal *opti = result->priv;
4687-
46884686
if (opt->renormalize)
46894687
git_attr_set_direction(GIT_ATTR_CHECKIN);
46904688
assert(opt->priv == NULL);
46914689

4692-
clear_or_reinit_internal_opts(opti, 0);
4693-
FREE_AND_NULL(opti);
4690+
if (result->priv) {
4691+
clear_or_reinit_internal_opts(result->priv, 0);
4692+
FREE_AND_NULL(result->priv);
4693+
}
46944694
}
46954695

46964696
/*** Function Grouping: helper functions for merge_incore_*() ***/

0 commit comments

Comments
 (0)