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

[PATCH] Fix git-show-branch --current when not on a branch

From
Stephan Beyer <s-beyer@gmx.net>
Date
May 23, 2008, 23:00 UTC
Message-ID
<alpine.DEB.1.10.0805240049400.3980@euler.pkqs.net>

builtin-show-branch.c relied on resolve_ref() to return "refs/heads/foo" if on branch foo. But if not on a branch, it returns "HEAD". Hence, `head + pfxlen' (i.e. head+11) is a memory address beyond the "HEAD" string, so that further operation leads to access of uninitialized memory.

This commit fixes the bug by just not adding the "refs/heads/"-length offset. So append_one_rev() operates on "refs/heads/foo" instead of "foo", which still works. But now it also operates correctly on "HEAD".

Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
---
  builtin-show-branch.c |    3 +--
  1 files changed, 1 insertions(+), 2 deletions(-)
diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 019abd3..412eba0 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -782,8 +782,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
  				has_head++;
  		}
  		if (!has_head) {
-			int pfxlen = strlen("refs/heads/");
-			append_one_rev(head + pfxlen);
+			append_one_rev(head);
  		}
  	}
-- 
1.5.5.1
Next: Stephan Beyer
Message 1 of 4 in “Fix git-show-branch --current when not on a branch”
  1. Fix git-show-branch --current when not on a branchStephan Beyer, May 23, 2008
  2. Stephan BeyerMay 26, 2008
  3. Junio C HamanoMay 26, 2008
  4. Stephan BeyerMay 27, 2008

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.