merge-recursive: add sanity checks for relevant merge_options

There are lots of options that callers can set, yet most have a limited range of valid values, some options are meant for output (e.g. opt->obuf, which is expected to start empty), and callers are expected to not set opt->priv. Add several sanity checks to ensure callers provide sane values. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Aug 17, 2019 at 11:41 UTC 45ef16f77ae5a14aa751d03c9c9fc2fa91804425
1 file changed +24
merge-recursive.c
+24
@@ -3615,6 +3615,30 @@ static int merge_start(struct merge_options *opt, struct tree *head)
3615 {
3616 struct strbuf sb = STRBUF_INIT;
3617
3618 + /* Sanity checks on opt */
3619 + assert(opt->repo);
3620 +
3621 + assert(opt->branch1 && opt->branch2);
3622 +
3623 + assert(opt->detect_renames >= -1 &&
3624 + opt->detect_renames <= DIFF_DETECT_COPY);
3625 + assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
3626 + opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
3627 + assert(opt->rename_limit >= -1);
3628 + assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
3629 + assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
3630 +
3631 + assert(opt->xdl_opts >= 0);
3632 + assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
3633 + opt->recursive_variant <= MERGE_VARIANT_THEIRS);
3634 +
3635 + assert(opt->verbosity >= 0 && opt->verbosity <= 5);
3636 + assert(opt->buffer_output <= 2);
3637 + assert(opt->obuf.len == 0);
3638 +
3639 + assert(opt->priv == NULL);
3640 +
3641 + /* Sanity check on repo state; index must match head */
3642 if (repo_index_has_changes(opt->repo, head, &sb)) {
3643 err(opt, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3644 sb.buf);