From 7ca8c18950c3f843cedba897b44f9c79b5ab44eb Mon Sep 17 00:00:00 2001 From: René Scharfe Date: Sat, 31 Oct 2015 18:35:32 +0100 Subject: t7060: add test for status --branch on a detached HEAD This test fails when run under Valgrind because branch_get() gets passed a bogus branch name pointer: ==62831== Invalid read of size 1 ==62831== at 0x4F76AE: branch_get (remote.c:1650) ==62831== by 0x53499E: wt_shortstatus_print_tracking (wt-status.c:1654) ==62831== by 0x53499E: wt_shortstatus_print (wt-status.c:1706) ==62831== by 0x428D29: cmd_status (commit.c:1384) ==62831== by 0x405D6D: run_builtin (git.c:350) ==62831== by 0x405D6D: handle_builtin (git.c:536) ==62831== by 0x404F10: run_argv (git.c:582) ==62831== by 0x404F10: main (git.c:690) ==62831== Address 0x5e89b0b is 6 bytes after a block of size 5 alloc'd ==62831== at 0x4C28C4F: malloc (vg_replace_malloc.c:299) ==62831== by 0x59579E9: strdup (strdup.c:42) ==62831== by 0x52E108: xstrdup (wrapper.c:43) ==62831== by 0x5322A6: wt_status_prepare (wt-status.c:130) ==62831== by 0x4276E0: status_init_config (commit.c:184) ==62831== by 0x428BB8: cmd_status (commit.c:1350) ==62831== by 0x405D6D: run_builtin (git.c:350) ==62831== by 0x405D6D: handle_builtin (git.c:536) ==62831== by 0x404F10: run_argv (git.c:582) ==62831== by 0x404F10: main (git.c:690) Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- t/t7060-wtstatus.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 't/t7060-wtstatus.sh') diff --git a/t/t7060-wtstatus.sh b/t/t7060-wtstatus.sh index 741ec08576..879d0c169d 100755 --- a/t/t7060-wtstatus.sh +++ b/t/t7060-wtstatus.sh @@ -213,5 +213,19 @@ EOF git checkout master ' +test_expect_failure 'status --branch with detached HEAD' ' + git reset --hard && + git checkout master^0 && + git status --branch --porcelain >actual && + cat >expected <<-EOF && + ## HEAD (no branch) + ?? .gitconfig + ?? actual + ?? expect + ?? expected + ?? mdconflict/ + EOF + test_i18ncmp expected actual +' test_done -- cgit v1.2.3 From baf0a3e47d807b63e9fc5628caa455d1da91dd6c Mon Sep 17 00:00:00 2001 From: René Scharfe Date: Sat, 31 Oct 2015 18:36:35 +0100 Subject: wt-status: avoid building bogus branch name with detached HEAD 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. 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 Signed-off-by: Junio C Hamano --- t/t7060-wtstatus.sh | 2 +- wt-status.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) (limited to 't/t7060-wtstatus.sh') diff --git a/t/t7060-wtstatus.sh b/t/t7060-wtstatus.sh index 879d0c169d..58df3f3bb1 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 ac05b9b73d..0e4a04e695 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1521,16 +1521,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); -- cgit v1.2.3