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

[PATCH 2/4] remote: drop const return of tracking_for_push_dest()

From
Jeff King <peff@peff.net>
Date
Jan 19, 2026, 05:20 UTC
Message-ID
<20260119052026.GB1991523@coredump.intra.peff.net>
In-Reply-To
<20260119051858.GA1991308@coredump.intra.peff.net>

The string returned from tracking_for_push_dest() comes from apply_refspec(), and thus is always an allocated string (or NULL). We should return a non-const pointer so that the caller knows that ownership of the string is being transferred.

This goes back to the function's origin in e291c75a95 (remote.c: add branch_get_push, 2015-05-21). It never really mattered because our return is just forwarded through branch_get_push_1(), which returns a const string as part of an intentionally hacky memory management scheme (see that commit for details).

As the first step of untangling that hackery, let's drop the extra const from this helper function (and from the variables that store its result). There should be no functional change (yet).

Signed-off-by: Jeff King <peff@peff.net>
---
 remote.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/remote.c b/remote.c
index 3dc100be83..5de9619bc7 100644
--- a/remote.c
+++ b/remote.c
@@ -1869,9 +1869,9 @@ const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
 	return branch->merge[0]->dst;
 }
 
-static const char *tracking_for_push_dest(struct remote *remote,
-					  const char *refname,
-					  struct strbuf *err)
+static char *tracking_for_push_dest(struct remote *remote,
+				    const char *refname,
+				    struct strbuf *err)
 {
 	char *ret;
 
@@ -1899,7 +1899,7 @@ static const char *branch_get_push_1(struct repository *repo,
 
 	if (remote->push.nr) {
 		char *dst;
-		const char *ret;
+		char *ret;
 
 		dst = apply_refspecs(&remote->push, branch->refname);
 		if (!dst)
@@ -1929,7 +1929,8 @@ static const char *branch_get_push_1(struct repository *repo,
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
 		{
-			const char *up, *cur;
+			const char *up;
+			char *cur;
 
 			up = branch_get_upstream(branch, err);
 			if (!up)
-- 
2.53.0.rc0.338.g08aa8a9473
Previous: Junio C HamanoNext: Patrick Steinhardt
Message 7 of 15 in “memory leaks in remote.c”
  1. 0/4 memory leaks in remote.cJeff King, Jan 19, 2026
  2. 1/4 remote: return non-const pointer from error_buf()Jeff King, Jan 19, 2026
  3. Patrick SteinhardtJan 19, 2026
  4. Junio C HamanoJan 20, 2026
  5. Jeff KingJan 20, 2026
  6. Junio C HamanoJan 20, 2026
  7. 2/4 remote: drop const return of tracking_for_push_dest()Jeff King, Jan 19, 2026
  8. Patrick SteinhardtJan 19, 2026
  9. 3/4 remote: fix leak in branch_get_push_1() with invalid "simple" configJeff King, Jan 19, 2026
  10. Patrick SteinhardtJan 19, 2026
  11. 4/4 remote: always allocate branch.push_tracking_refJeff King, Jan 19, 2026
  12. Patrick SteinhardtJan 19, 2026
  13. Triangular workflowHarald Nordgren, Jan 19, 2026
  14. Jeff KingJan 20, 2026
  15. Junio C HamanoJan 20, 2026

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.