Skip to content

Commit 0b8d22f

Browse files
committed
Merge branch 'pw/sequencer-reflog-use-after-free'
Use-after-free fix in the sequencer. * pw/sequencer-reflog-use-after-free: sequencer: rework reflog message handling sequencer: move reflog message functions
2 parents cb96e16 + 5dbaec6 commit 0b8d22f

File tree

2 files changed

+67
-60
lines changed

2 files changed

+67
-60
lines changed

sequencer.c

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,6 @@ struct replay_ctx {
224224
* current chain.
225225
*/
226226
struct strbuf current_fixups;
227-
/*
228-
* Stores the reflog message that will be used when creating a
229-
* commit. Points to a static buffer and should not be free()'d.
230-
*/
231-
const char *reflog_message;
232227
/*
233228
* The number of completed fixup and squash commands in the
234229
* current chain.
@@ -1124,10 +1119,10 @@ static int run_command_silent_on_success(struct child_process *cmd)
11241119
* author metadata.
11251120
*/
11261121
static int run_git_commit(const char *defmsg,
1122+
const char *reflog_action,
11271123
struct replay_opts *opts,
11281124
unsigned int flags)
11291125
{
1130-
struct replay_ctx *ctx = opts->ctx;
11311126
struct child_process cmd = CHILD_PROCESS_INIT;
11321127

11331128
if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
@@ -1145,7 +1140,7 @@ static int run_git_commit(const char *defmsg,
11451140
gpg_opt, gpg_opt);
11461141
}
11471142

1148-
strvec_pushf(&cmd.env, GIT_REFLOG_ACTION "=%s", ctx->reflog_message);
1143+
strvec_pushf(&cmd.env, GIT_REFLOG_ACTION "=%s", reflog_action);
11491144

11501145
if (opts->committer_date_is_author_date)
11511146
strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s",
@@ -1529,10 +1524,10 @@ static int parse_head(struct repository *r, struct commit **head)
15291524
*/
15301525
static int try_to_commit(struct repository *r,
15311526
struct strbuf *msg, const char *author,
1527+
const char *reflog_action,
15321528
struct replay_opts *opts, unsigned int flags,
15331529
struct object_id *oid)
15341530
{
1535-
struct replay_ctx *ctx = opts->ctx;
15361531
struct object_id tree;
15371532
struct commit *current_head = NULL;
15381533
struct commit_list *parents = NULL;
@@ -1694,7 +1689,7 @@ static int try_to_commit(struct repository *r,
16941689
goto out;
16951690
}
16961691

1697-
if (update_head_with_reflog(current_head, oid, ctx->reflog_message,
1692+
if (update_head_with_reflog(current_head, oid, reflog_action,
16981693
msg, &err)) {
16991694
res = error("%s", err.buf);
17001695
goto out;
@@ -1725,6 +1720,7 @@ static int write_rebase_head(struct object_id *oid)
17251720

17261721
static int do_commit(struct repository *r,
17271722
const char *msg_file, const char *author,
1723+
const char *reflog_action,
17281724
struct replay_opts *opts, unsigned int flags,
17291725
struct object_id *oid)
17301726
{
@@ -1740,7 +1736,7 @@ static int do_commit(struct repository *r,
17401736
msg_file);
17411737

17421738
res = try_to_commit(r, msg_file ? &sb : NULL,
1743-
author, opts, flags, &oid);
1739+
author, reflog_action, opts, flags, &oid);
17441740
strbuf_release(&sb);
17451741
if (!res) {
17461742
refs_delete_ref(get_main_ref_store(r), "",
@@ -1756,7 +1752,7 @@ static int do_commit(struct repository *r,
17561752
if (is_rebase_i(opts) && oid)
17571753
if (write_rebase_head(oid))
17581754
return -1;
1759-
return run_git_commit(msg_file, opts, flags);
1755+
return run_git_commit(msg_file, reflog_action, opts, flags);
17601756
}
17611757

17621758
return res;
@@ -2226,6 +2222,39 @@ static void refer_to_commit(struct replay_opts *opts,
22262222
}
22272223
}
22282224

2225+
static const char *sequencer_reflog_action(struct replay_opts *opts)
2226+
{
2227+
if (!opts->reflog_action) {
2228+
opts->reflog_action = getenv(GIT_REFLOG_ACTION);
2229+
opts->reflog_action =
2230+
xstrdup(opts->reflog_action ? opts->reflog_action
2231+
: action_name(opts));
2232+
}
2233+
2234+
return opts->reflog_action;
2235+
}
2236+
2237+
__attribute__((format (printf, 3, 4)))
2238+
static const char *reflog_message(struct replay_opts *opts,
2239+
const char *sub_action, const char *fmt, ...)
2240+
{
2241+
va_list ap;
2242+
static struct strbuf buf = STRBUF_INIT;
2243+
2244+
va_start(ap, fmt);
2245+
strbuf_reset(&buf);
2246+
strbuf_addstr(&buf, sequencer_reflog_action(opts));
2247+
if (sub_action)
2248+
strbuf_addf(&buf, " (%s)", sub_action);
2249+
if (fmt) {
2250+
strbuf_addstr(&buf, ": ");
2251+
strbuf_vaddf(&buf, fmt, ap);
2252+
}
2253+
va_end(ap);
2254+
2255+
return buf.buf;
2256+
}
2257+
22292258
static int do_pick_commit(struct repository *r,
22302259
struct todo_item *item,
22312260
struct replay_opts *opts,
@@ -2236,13 +2265,19 @@ static int do_pick_commit(struct repository *r,
22362265
const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r);
22372266
struct object_id head;
22382267
struct commit *base, *next, *parent;
2239-
const char *base_label, *next_label;
2268+
const char *base_label, *next_label, *reflog_action;
22402269
char *author = NULL;
22412270
struct commit_message msg = { NULL, NULL, NULL, NULL };
22422271
int res, unborn = 0, reword = 0, allow, drop_commit;
22432272
enum todo_command command = item->command;
22442273
struct commit *commit = item->commit;
22452274

2275+
if (is_rebase_i(opts))
2276+
reflog_action = reflog_message(
2277+
opts, command_to_string(item->command), NULL);
2278+
else
2279+
reflog_action = sequencer_reflog_action(opts);
2280+
22462281
if (opts->no_commit) {
22472282
/*
22482283
* We do not intend to commit immediately. We just want to
@@ -2494,7 +2529,8 @@ static int do_pick_commit(struct repository *r,
24942529
} /* else allow == 0 and there's nothing special to do */
24952530
if (!opts->no_commit && !drop_commit) {
24962531
if (author || command == TODO_REVERT || (flags & AMEND_MSG))
2497-
res = do_commit(r, msg_file, author, opts, flags,
2532+
res = do_commit(r, msg_file, author, reflog_action,
2533+
opts, flags,
24982534
commit? &commit->object.oid : NULL);
24992535
else
25002536
res = error(_("unable to parse commit author"));
@@ -2509,7 +2545,7 @@ static int do_pick_commit(struct repository *r,
25092545
* got here.
25102546
*/
25112547
flags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;
2512-
res = run_git_commit(NULL, opts, flags);
2548+
res = run_git_commit(NULL, reflog_action, opts, flags);
25132549
*check_todo = 1;
25142550
}
25152551
}
@@ -3919,39 +3955,6 @@ static int do_label(struct repository *r, const char *name, int len)
39193955
return ret;
39203956
}
39213957

3922-
static const char *sequencer_reflog_action(struct replay_opts *opts)
3923-
{
3924-
if (!opts->reflog_action) {
3925-
opts->reflog_action = getenv(GIT_REFLOG_ACTION);
3926-
opts->reflog_action =
3927-
xstrdup(opts->reflog_action ? opts->reflog_action
3928-
: action_name(opts));
3929-
}
3930-
3931-
return opts->reflog_action;
3932-
}
3933-
3934-
__attribute__((format (printf, 3, 4)))
3935-
static const char *reflog_message(struct replay_opts *opts,
3936-
const char *sub_action, const char *fmt, ...)
3937-
{
3938-
va_list ap;
3939-
static struct strbuf buf = STRBUF_INIT;
3940-
3941-
va_start(ap, fmt);
3942-
strbuf_reset(&buf);
3943-
strbuf_addstr(&buf, sequencer_reflog_action(opts));
3944-
if (sub_action)
3945-
strbuf_addf(&buf, " (%s)", sub_action);
3946-
if (fmt) {
3947-
strbuf_addstr(&buf, ": ");
3948-
strbuf_vaddf(&buf, fmt, ap);
3949-
}
3950-
va_end(ap);
3951-
3952-
return buf.buf;
3953-
}
3954-
39553958
static struct commit *lookup_label(struct repository *r, const char *label,
39563959
int len, struct strbuf *buf)
39573960
{
@@ -4089,6 +4092,7 @@ static int do_merge(struct repository *r,
40894092
int merge_arg_len, oneline_offset, can_fast_forward, ret, k;
40904093
static struct lock_file lock;
40914094
const char *p;
4095+
const char *reflog_action = reflog_message(opts, "merge", NULL);
40924096

40934097
if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
40944098
ret = -1;
@@ -4360,14 +4364,15 @@ static int do_merge(struct repository *r,
43604364
* value (a negative one would indicate that the `merge`
43614365
* command needs to be rescheduled).
43624366
*/
4363-
ret = !!run_git_commit(git_path_merge_msg(r), opts,
4364-
run_commit_flags);
4367+
ret = !!run_git_commit(git_path_merge_msg(r), reflog_action,
4368+
opts, run_commit_flags);
43654369

43664370
if (!ret && flags & TODO_EDIT_MERGE_MSG) {
43674371
fast_forward_edit:
43684372
*check_todo = 1;
43694373
run_commit_flags |= AMEND_MSG | EDIT_MSG | VERIFY_MSG;
4370-
ret = !!run_git_commit(NULL, opts, run_commit_flags);
4374+
ret = !!run_git_commit(NULL, reflog_action, opts,
4375+
run_commit_flags);
43714376
}
43724377

43734378

@@ -4882,13 +4887,9 @@ static int pick_one_commit(struct repository *r,
48824887
struct replay_opts *opts,
48834888
int *check_todo, int* reschedule)
48844889
{
4885-
struct replay_ctx *ctx = opts->ctx;
48864890
int res;
48874891
struct todo_item *item = todo_list->items + todo_list->current;
48884892
const char *arg = todo_item_get_arg(todo_list, item);
4889-
if (is_rebase_i(opts))
4890-
ctx->reflog_message = reflog_message(
4891-
opts, command_to_string(item->command), NULL);
48924893

48934894
res = do_pick_commit(r, item, opts, is_final_fixup(todo_list),
48944895
check_todo);
@@ -4947,7 +4948,6 @@ static int pick_commits(struct repository *r,
49474948
struct replay_ctx *ctx = opts->ctx;
49484949
int res = 0, reschedule = 0;
49494950

4950-
ctx->reflog_message = sequencer_reflog_action(opts);
49514951
if (opts->allow_ff)
49524952
ASSERT(!(opts->signoff || opts->no_commit ||
49534953
opts->record_origin || should_edit(opts) ||
@@ -5208,6 +5208,7 @@ static int commit_staged_changes(struct repository *r,
52085208
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
52095209
unsigned int final_fixup = 0, is_clean;
52105210
struct strbuf rev = STRBUF_INIT;
5211+
const char *reflog_action = reflog_message(opts, "continue", NULL);
52115212
int ret;
52125213

52135214
if (has_unstaged_changes(r, 1)) {
@@ -5370,7 +5371,7 @@ static int commit_staged_changes(struct repository *r,
53705371
}
53715372

53725373
if (run_git_commit(final_fixup ? NULL : rebase_path_message(),
5373-
opts, flags)) {
5374+
reflog_action, opts, flags)) {
53745375
ret = error(_("could not commit staged changes."));
53755376
goto out;
53765377
}
@@ -5402,7 +5403,6 @@ static int commit_staged_changes(struct repository *r,
54025403

54035404
int sequencer_continue(struct repository *r, struct replay_opts *opts)
54045405
{
5405-
struct replay_ctx *ctx = opts->ctx;
54065406
struct todo_list todo_list = TODO_LIST_INIT;
54075407
int res;
54085408

@@ -5423,7 +5423,6 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
54235423
unlink(rebase_path_dropped());
54245424
}
54255425

5426-
ctx->reflog_message = reflog_message(opts, "continue", NULL);
54275426
if (commit_staged_changes(r, opts, &todo_list)) {
54285427
res = -1;
54295428
goto release_todo_list;
@@ -5475,7 +5474,6 @@ static int single_pick(struct repository *r,
54755474
TODO_PICK : TODO_REVERT;
54765475
item.commit = cmit;
54775476

5478-
opts->ctx->reflog_message = sequencer_reflog_action(opts);
54795477
return do_pick_commit(r, &item, opts, 0, &check_todo);
54805478
}
54815479

t/t3430-rebase-merges.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test_expect_success 'create completely different structure' '
8686
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
8787
test_tick &&
8888
git rebase -i -r A main &&
89-
test_cmp_graph <<-\EOF
89+
test_cmp_graph <<-\EOF &&
9090
* Merge the topic branch '\''onebranch'\''
9191
|\
9292
| * D
@@ -99,6 +99,15 @@ test_expect_success 'create completely different structure' '
9999
|/
100100
* A
101101
EOF
102+
103+
head="$(git show-ref --verify -s --abbrev HEAD)" &&
104+
cat >expect <<-EOF &&
105+
$head HEAD@{0}: rebase (finish): returning to refs/heads/main
106+
$head HEAD@{1}: rebase (merge): Merge the topic branch ${SQ}onebranch${SQ}
107+
EOF
108+
109+
git reflog -n2 HEAD >actual &&
110+
test_cmp expect actual
102111
'
103112

104113
test_expect_success 'generate correct todo list' '

0 commit comments

Comments
 (0)