Skip to content

Commit baf0a3e

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bcf8cc2 commit baf0a3e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

t/t7060-wtstatus.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ EOF
213213
git checkout master
214214
'
215215

216-
test_expect_failure 'status --branch with detached HEAD' '
216+
test_expect_success 'status --branch with detached HEAD' '
217217
git reset --hard &&
218218
git checkout master^0 &&
219219
git status --branch --porcelain >actual &&

wt-status.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,16 +1521,19 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
15211521
return;
15221522
branch_name = s->branch;
15231523

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+
15241533
if (starts_with(branch_name, "refs/heads/"))
15251534
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-
}
15301535

15311536
branch = branch_get(s->branch + 11);
1532-
if (s->is_initial)
1533-
color_fprintf(s->fp, header_color, _("Initial commit on "));
15341537

15351538
color_fprintf(s->fp, branch_color_local, "%s", branch_name);
15361539

0 commit comments

Comments
 (0)