Skip to content

Commit c5bcf1f

Browse files
committed
checkout: do not write bogus reflog entry out
As resolve_ref() returns a static buffer that is local to the function, the caller needs to be sure that it will not have any other calls to the function before it uses the returned value, or store it away with a strdup(). The code used old.path to record which branch it used to be on, so that it can say between which branches the switch took place in the reflog, but sometimes it failed to do so. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 782c2d6 commit c5bcf1f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

builtin-checkout.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ static int switch_branches(struct checkout_opts *opts,
342342
unsigned char rev[20];
343343
int flag;
344344
memset(&old, 0, sizeof(old));
345-
old.path = resolve_ref("HEAD", rev, 0, &flag);
345+
old.path = xstrdup(resolve_ref("HEAD", rev, 0, &flag));
346346
old.commit = lookup_commit_reference_gently(rev, 1);
347-
if (!(flag & REF_ISSYMREF))
347+
if (!(flag & REF_ISSYMREF)) {
348+
free((char *)old.path);
348349
old.path = NULL;
350+
}
349351

350352
if (old.path && !prefixcmp(old.path, "refs/heads/"))
351353
old.name = old.path + strlen("refs/heads/");
@@ -381,7 +383,7 @@ static int switch_branches(struct checkout_opts *opts,
381383
return ret;
382384

383385
update_refs_for_switch(opts, &old, new);
384-
386+
free((char *)old.path);
385387
return post_checkout_hook(old.commit, new->commit, 1);
386388
}
387389

0 commit comments

Comments
 (0)