Re: [PATCH] Fix git-show-branch --current when not on a branch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Stephan Beyer <s-beyer@xxxxxxx> writes:

> builtin-show-branch.c relied on resolve_ref() to return
> "refs/heads/foo" if on branch foo.  But if not on a branch,
> it returns "HEAD".  Hence, `head + pfxlen' (i.e. head+11)
> is a memory address beyond the "HEAD" string, so that
> further operation leads to access of uninitialized memory.
>
> This commit fixes the bug by just not adding the
> "refs/heads/"-length offset.  So append_one_rev() operates
> on "refs/heads/foo" instead of "foo", which still works.
> But now it also operates correctly on "HEAD".
>
> Signed-off-by: Stephan Beyer <s-beyer@xxxxxxx>

Thanks for a patch and (more importantly) prodding..

>  builtin-show-branch.c |    3 +--
>  1 files changed, 1 insertions(+), 2 deletions(-)
>
> diff --git a/builtin-show-branch.c b/builtin-show-branch.c
> index 019abd3..412eba0 100644
> --- a/builtin-show-branch.c
> +++ b/builtin-show-branch.c
> @@ -782,8 +782,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
>  				has_head++;
>  		}
>  		if (!has_head) {
> -			int pfxlen = strlen("refs/heads/");
> -			append_one_rev(head + pfxlen);
> +			append_one_rev(head);

This changes the output for normal case.  How about doing it like this instead?

 builtin-show-branch.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 019abd3..a383323 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -782,8 +782,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 				has_head++;
 		}
 		if (!has_head) {
-			int pfxlen = strlen("refs/heads/");
-			append_one_rev(head + pfxlen);
+			int offset = !prefixcmp(head, "refs/heads/") ? 11 : 0;
+			append_one_rev(head + offset);
 		}
 	}
 
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux