checkout: do not write bogus reflog entry out
authorJunio C Hamano <gitster@pobox.com>
Wed, 6 Jul 2011 22:14:43 +0000 (15:14 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 6 Jul 2011 22:15:02 +0000 (15:15 -0700)
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>
builtin-checkout.c
index 59a0ef4ec9770af6d031abe959adc587c9538a89..141a9e187302ddf707c2dc6f3796694a55081c61 100644 (file)
@@ -342,10 +342,12 @@ static int switch_branches(struct checkout_opts *opts,
        unsigned char rev[20];
        int flag;
        memset(&old, 0, sizeof(old));
-       old.path = resolve_ref("HEAD", rev, 0, &flag);
+       old.path = xstrdup(resolve_ref("HEAD", rev, 0, &flag));
        old.commit = lookup_commit_reference_gently(rev, 1);
-       if (!(flag & REF_ISSYMREF))
+       if (!(flag & REF_ISSYMREF)) {
+               free((char *)old.path);
                old.path = NULL;
+       }
 
        if (old.path && !prefixcmp(old.path, "refs/heads/"))
                old.name = old.path + strlen("refs/heads/");
@@ -381,7 +383,7 @@ static int switch_branches(struct checkout_opts *opts,
                return ret;
 
        update_refs_for_switch(opts, &old, new);
-
+       free((char *)old.path);
        return post_checkout_hook(old.commit, new->commit, 1);
 }