Skip to content

Commit 0c52cf8

Browse files
avargitster
authored andcommitted
sequencer: add a "goto cleanup" to do_reset()
Restructure code that's mostly added in 9055e40 (sequencer: introduce new commands to reset the revision, 2018-04-25) to avoid code duplication, and to make freeing other resources easier in a subsequent commit. It's safe to initialize "tree_desc" to be zero'd out in order to unconditionally free desc.buffer, it won't be initialized on the first couple of "goto"'s. There are three earlier "return"'s in this function which should probably be made to use this new "cleanup" too, per [1] it looks like they're leaving behind stale locks. But let's not try to fix every potential bug here now, I'm just trying to narrowly plug a memory leak. 1. https://lore.kernel.org/git/CABPp-BH=3DP-dXRCphY53-3eZd1TU8h5GY_M12nnbEGm-UYB9Q@mail.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e5a917f commit 0c52cf8

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

sequencer.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,7 +3644,7 @@ static int do_reset(struct repository *r,
36443644
struct strbuf ref_name = STRBUF_INIT;
36453645
struct object_id oid;
36463646
struct lock_file lock = LOCK_INIT;
3647-
struct tree_desc desc;
3647+
struct tree_desc desc = { 0 };
36483648
struct tree *tree;
36493649
struct unpack_trees_options unpack_tree_opts;
36503650
int ret = 0;
@@ -3678,10 +3678,8 @@ static int do_reset(struct repository *r,
36783678
strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
36793679
if (get_oid(ref_name.buf, &oid) &&
36803680
get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) {
3681-
error(_("could not read '%s'"), ref_name.buf);
3682-
rollback_lock_file(&lock);
3683-
strbuf_release(&ref_name);
3684-
return -1;
3681+
ret = error(_("could not read '%s'"), ref_name.buf);
3682+
goto cleanup;
36853683
}
36863684
}
36873685

@@ -3696,38 +3694,34 @@ static int do_reset(struct repository *r,
36963694
init_checkout_metadata(&unpack_tree_opts.meta, name, &oid, NULL);
36973695

36983696
if (repo_read_index_unmerged(r)) {
3699-
rollback_lock_file(&lock);
3700-
strbuf_release(&ref_name);
3701-
return error_resolve_conflict(_(action_name(opts)));
3697+
ret = error_resolve_conflict(_(action_name(opts)));
3698+
goto cleanup;
37023699
}
37033700

37043701
if (!fill_tree_descriptor(r, &desc, &oid)) {
3705-
error(_("failed to find tree of %s"), oid_to_hex(&oid));
3706-
rollback_lock_file(&lock);
3707-
free((void *)desc.buffer);
3708-
strbuf_release(&ref_name);
3709-
return -1;
3702+
ret = error(_("failed to find tree of %s"), oid_to_hex(&oid));
3703+
goto cleanup;
37103704
}
37113705

37123706
if (unpack_trees(1, &desc, &unpack_tree_opts)) {
3713-
rollback_lock_file(&lock);
3714-
free((void *)desc.buffer);
3715-
strbuf_release(&ref_name);
3716-
return -1;
3707+
ret = -1;
3708+
goto cleanup;
37173709
}
37183710

37193711
tree = parse_tree_indirect(&oid);
37203712
prime_cache_tree(r, r->index, tree);
37213713

37223714
if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0)
37233715
ret = error(_("could not write index"));
3724-
free((void *)desc.buffer);
37253716

37263717
if (!ret)
37273718
ret = update_ref(reflog_message(opts, "reset", "'%.*s'",
37283719
len, name), "HEAD", &oid,
37293720
NULL, 0, UPDATE_REFS_MSG_ON_ERR);
3730-
3721+
cleanup:
3722+
free((void *)desc.buffer);
3723+
if (ret < 0)
3724+
rollback_lock_file(&lock);
37313725
strbuf_release(&ref_name);
37323726
return ret;
37333727
}

0 commit comments

Comments
 (0)