All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git List <git@vger.kernel.org>
Subject: Re: [PATCH 3/5] wt-status: avoid building bogus branch name with detached HEAD
Date: Sun, 01 Nov 2015 09:50:45 -0800	[thread overview]
Message-ID: <xmqqbnbdip6y.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <5634FC23.2090703@web.de> ("René Scharfe"'s message of "Sat, 31 Oct 2015 18:36:35 +0100")

René Scharfe <l.s.r@web.de> writes:

> If we're on a detached HEAD then wt_shortstatus_print_tracking() takes
> the string "HEAD (no branch)", translates it, skips the first eleven
> characters and passes the result to branch_get(), which returns a bogus
> result and accesses memory out of bounds in order to produce it.

The fix is correct, but the above explanation looks "not quite" to
me.

That "HEAD (no branch)" thing is in a separate branch_name variable
that is not involved in the actual computation (i.e. call to
branch_get()).

The function gets "HEAD" in s->branch, uses that and skips the first
eleven characters (i.e. beyond the end of that string), lets
branch_get() to return a garbage and likely missing branch, finds
that nobody tracks that, and does the right thing anyway.  If the
garbage past the end of the "HEAD" happens to have a name of an
existing branch, we would get an incorrect result.

Thanks.

> Somehow stat_tracking_info(), which is passed that result, does the
> right thing anyway, i.e. it finds that there is no base.
>
> Avoid the bogus results and memory accesses by checking for HEAD first
> and exiting early in that case.  This fixes t7060 with --valgrind.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  t/t7060-wtstatus.sh |  2 +-
>  wt-status.c         | 15 +++++++++------
>  2 files changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/t/t7060-wtstatus.sh b/t/t7060-wtstatus.sh
> index e6af772..44bf1d8 100755
> --- a/t/t7060-wtstatus.sh
> +++ b/t/t7060-wtstatus.sh
> @@ -213,7 +213,7 @@ EOF
>  	git checkout master
>  '
>  
> -test_expect_failure 'status --branch with detached HEAD' '
> +test_expect_success 'status --branch with detached HEAD' '
>  	git reset --hard &&
>  	git checkout master^0 &&
>  	git status --branch --porcelain >actual &&
> diff --git a/wt-status.c b/wt-status.c
> index 083328f..e206cc9 100644
> --- a/wt-status.c
> +++ b/wt-status.c
> @@ -1644,16 +1644,19 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
>  		return;
>  	branch_name = s->branch;
>  
> +	if (s->is_initial)
> +		color_fprintf(s->fp, header_color, _("Initial commit on "));
> +
> +	if (!strcmp(s->branch, "HEAD")) {
> +		color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
> +			      _("HEAD (no branch)"));
> +		goto conclude;
> +	}
> +
>  	if (starts_with(branch_name, "refs/heads/"))
>  		branch_name += 11;
> -	else if (!strcmp(branch_name, "HEAD")) {
> -		branch_name = _("HEAD (no branch)");
> -		branch_color_local = color(WT_STATUS_NOBRANCH, s);
> -	}
>  
>  	branch = branch_get(s->branch + 11);
> -	if (s->is_initial)
> -		color_fprintf(s->fp, header_color, _("Initial commit on "));
>  
>  	color_fprintf(s->fp, branch_color_local, "%s", branch_name);

  reply	other threads:[~2015-11-01 17:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-31 17:33 [PATCH 0/5] wt-status: fix an invalid memory read, clean up René Scharfe
2015-10-31 17:35 ` [PATCH 1/5] t7060: add test for status --branch on a detached HEAD René Scharfe
2015-10-31 17:36 ` [PATCH 2/5] wt-status: exit early using goto in wt_shortstatus_print_tracking() René Scharfe
2015-10-31 17:36 ` [PATCH 3/5] wt-status: avoid building bogus branch name with detached HEAD René Scharfe
2015-11-01 17:50   ` Junio C Hamano [this message]
2015-11-01 18:11     ` René Scharfe
2015-10-31 17:37 ` [PATCH 4/5] wt-status: don't skip a magical number of characters blindly René Scharfe
2015-11-01 17:51   ` Junio C Hamano
2015-11-01 17:55     ` Junio C Hamano
2015-10-31 17:37 ` [PATCH 5/5] wt-status: use skip_prefix() to get rid of magic string length constants René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqbnbdip6y.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.