git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/5] wt-status: avoid building bogus branch name with detached HEAD

From
René Scharfe <l.s.r@web.de>
Date
Oct 31, 2015, 17:36 UTC
Message-ID
<5634FC23.2090703@web.de>
In-Reply-To
<5634FB59.1000506@web.de>

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>
---
 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);
 
-- 
2.6.2
Previous: René ScharfeNext: Junio C Hamano
Message 4 of 10 in “wt-status: fix an invalid memory read, clean up”
  1. 0/5 wt-status: fix an invalid memory read, clean upRené Scharfe, Oct 31, 2015
  2. 1/5 t7060: add test for status --branch on a detached HEADRené Scharfe, Oct 31, 2015
  3. 2/5 wt-status: exit early using goto in wt_shortstatus_print_tracking()René Scharfe, Oct 31, 2015
  4. 3/5 wt-status: avoid building bogus branch name with detached HEADRené Scharfe, Oct 31, 2015
  5. Junio C HamanoNov 1, 2015
  6. René ScharfeNov 1, 2015
  7. 4/5 wt-status: don't skip a magical number of characters blindlyRené Scharfe, Oct 31, 2015
  8. Junio C HamanoNov 1, 2015
  9. Junio C HamanoNov 1, 2015
  10. 5/5 wt-status: use skip_prefix() to get rid of magic string length constantsRené Scharfe, Oct 31, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.