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 <gitster@pobox.com>

Junio C Hamano committed Jul 6, 2011 at 15:14 UTC c5bcf1f9f6d3429ab9a09e07e28362e7d189005b
1 file changed +5 -3
builtin-checkout.c
+5 -3
@@ -342,10 +342,12 @@ static int switch_branches(struct checkout_opts *opts,
342 unsigned char rev[20];
343 int flag;
344 memset(&old, 0, sizeof(old));
345 - old.path = resolve_ref("HEAD", rev, 0, &flag);
345 + old.path = xstrdup(resolve_ref("HEAD", rev, 0, &flag));
346 old.commit = lookup_commit_reference_gently(rev, 1);
347 - if (!(flag & REF_ISSYMREF))
347 + if (!(flag & REF_ISSYMREF)) {
348 + free((char *)old.path);
349 old.path = NULL;
350 + }
351
352 if (old.path && !prefixcmp(old.path, "refs/heads/"))
353 old.name = old.path + strlen("refs/heads/");
@@ -381,7 +383,7 @@ static int switch_branches(struct checkout_opts *opts,
383 return ret;
384
385 update_refs_for_switch(opts, &old, new);
384 -
386 + free((char *)old.path);
387 return post_checkout_hook(old.commit, new->commit, 1);
388 }
389