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

[PATCH v2] branch_get_push: do not segfault when HEAD is detached

From
Kyle Meyer <kyle@kyleam.com>
Date
Jan 7, 2017, 01:12 UTC
Message-ID
<20170107011215.29691-1-kyle@kyleam.com>
In-Reply-To
<20170106045623.21118-1-kyle@kyleam.com>

Move the detached HEAD check from branch_get_push_1() to branch_get_push() to avoid setting branch->push_tracking_ref when branch is NULL.

Signed-off-by: Kyle Meyer <kyle@kyleam.com>
---
 remote.c                  | 6 +++---
 t/t1514-rev-parse-push.sh | 6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/remote.c b/remote.c
index ad6c5424e..d5eaec737 100644
--- a/remote.c
+++ b/remote.c
@@ -1716,9 +1716,6 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
 {
 	struct remote *remote;
 
-	if (!branch)
-		return error_buf(err, _("HEAD does not point to a branch"));
-
 	remote = remote_get(pushremote_for_branch(branch, NULL));
 	if (!remote)
 		return error_buf(err,
@@ -1778,6 +1775,9 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
 
 const char *branch_get_push(struct branch *branch, struct strbuf *err)
 {
+	if (!branch)
+		return error_buf(err, _("HEAD does not point to a branch"));
+
 	if (!branch->push_tracking_ref)
 		branch->push_tracking_ref = branch_get_push_1(branch, err);
 	return branch->push_tracking_ref;
diff --git a/t/t1514-rev-parse-push.sh b/t/t1514-rev-parse-push.sh
index 7214f5b33..623a32aa6 100755
--- a/t/t1514-rev-parse-push.sh
+++ b/t/t1514-rev-parse-push.sh
@@ -60,4 +60,10 @@ test_expect_success '@{push} with push refspecs' '
 	resolve topic@{push} refs/remotes/origin/magic/topic
 '
 
+test_expect_success 'resolving @{push} fails with a detached HEAD' '
+	git checkout HEAD^0 &&
+	test_when_finished "git checkout -" &&
+	test_must_fail git rev-parse @{push}
+'
+
 test_done
-- 
2.11.0
Previous: Junio C Hamano
Message 9 of 9 in “branch_get_push: do not segfault when HEAD is detached”
  1. branch_get_push: do not segfault when HEAD is detachedKyle Meyer, Jan 6, 2017
  2. Jeff KingJan 6, 2017
  3. Johannes SchindelinJan 6, 2017
  4. Kyle MeyerJan 7, 2017
  5. Jeff KingJan 7, 2017
  6. Kyle MeyerJan 7, 2017
  7. Jeff KingJan 7, 2017
  8. Junio C HamanoJan 9, 2017
  9. branch_get_push: do not segfault when HEAD is detachedKyle Meyer, Jan 7, 2017

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.