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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 31, 2015 at 18:36 UTC baf0a3e47d807b63e9fc5628caa455d1da91dd6c
2 files changed +10 -7
t/t7060-wtstatus.sh
+1 -1
@@ -213,7 +213,7 @@ EOF
213 git checkout master
214 '
215
216 -test_expect_failure 'status --branch with detached HEAD' '
216 +test_expect_success 'status --branch with detached HEAD' '
217 git reset --hard &&
218 git checkout master^0 &&
219 git status --branch --porcelain >actual &&
wt-status.c
+9 -6
@@ -1521,16 +1521,19 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
1521 return;
1522 branch_name = s->branch;
1523
1524 + if (s->is_initial)
1525 + color_fprintf(s->fp, header_color, _("Initial commit on "));
1526 +
1527 + if (!strcmp(s->branch, "HEAD")) {
1528 + color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
1529 + _("HEAD (no branch)"));
1530 + goto conclude;
1531 + }
1532 +
1533 if (starts_with(branch_name, "refs/heads/"))
1534 branch_name += 11;
1526 - else if (!strcmp(branch_name, "HEAD")) {
1527 - branch_name = _("HEAD (no branch)");
1528 - branch_color_local = color(WT_STATUS_NOBRANCH, s);
1529 - }
1535
1536 branch = branch_get(s->branch + 11);
1532 - if (s->is_initial)
1533 - color_fprintf(s->fp, header_color, _("Initial commit on "));
1537
1538 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1539