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

[PATCH 3/3] builtin-branch: use strbuf in rename_branch()

From
Miklos Vajna <vmiklos@frugalware.org>
Date
Nov 17, 2008, 20:48 UTC
Message-ID
<225d83c1ead50340eed97c64fbb8995017bf1ca8.1226954771.git.vmiklos@frugalware.org>
In-Reply-To
<5dee4e908190e7a4ed4a693fdf0fb3b3af88fc57.1226954771.git.vmiklos@frugalware.org>

In case the length of branch name is greather then PATH_MAX-11, we write to unallocated memory otherwise.

Signed-off-by: Miklos Vajna <vmiklos@frugalware.org>
---
 builtin-branch.c |   39 +++++++++++++++++++++------------------
 1 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/builtin-branch.c b/builtin-branch.c
index c8a8e2a..494cbac 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -424,42 +424,45 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 
 static void rename_branch(const char *oldname, const char *newname, int force)
 {
-	char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
+	struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
 	unsigned char sha1[20];
-	char oldsection[PATH_MAX], newsection[PATH_MAX];
+	struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
 
 	if (!oldname)
 		die("cannot rename the current branch while not on any.");
 
-	if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
-		die("Old branchname too long");
+	strbuf_addf(&oldref, "refs/heads/%s", oldname);
 
-	if (check_ref_format(oldref))
-		die("Invalid branch name: %s", oldref);
+	if (check_ref_format(oldref.buf))
+		die("Invalid branch name: %s", oldref.buf);
 
-	if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
-		die("New branchname too long");
+	strbuf_addf(&newref, "refs/heads/%s", newname);
 
-	if (check_ref_format(newref))
-		die("Invalid branch name: %s", newref);
+	if (check_ref_format(newref.buf))
+		die("Invalid branch name: %s", newref.buf);
 
-	if (resolve_ref(newref, sha1, 1, NULL) && !force)
+	if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
 		die("A branch named '%s' already exists.", newname);
 
-	snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
-		 oldref, newref);
+	strbuf_addf(&logmsg, "Branch: renamed %s to %s",
+		 oldref.buf, newref.buf);
 
-	if (rename_ref(oldref, newref, logmsg))
+	if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
 		die("Branch rename failed");
+	strbuf_release(&logmsg);
 
 	/* no need to pass logmsg here as HEAD didn't really move */
-	if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
+	if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
 		die("Branch renamed to %s, but HEAD is not updated!", newname);
 
-	snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11);
-	snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11);
-	if (git_config_rename_section(oldsection, newsection) < 0)
+	strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
+	strbuf_release(&oldref);
+	strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
+	strbuf_release(&newref);
+	if (git_config_rename_section(oldsection.buf, newsection.buf) < 0)
 		die("Branch is renamed, but update of config-file failed");
+	strbuf_release(&oldsection);
+	strbuf_release(&newsection);
 }
 
 static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
-- 
1.6.0.2
Previous: Miklos VajnaNext: Junio C Hamano
Message 4 of 7 in “builtin-branch: use strbuf”
  1. 0/3 builtin-branch: use strbufMiklos Vajna, Nov 17, 2008
  2. 1/3 builtin-branch: use strbuf in delete_branches()Miklos Vajna, Nov 17, 2008
  3. 2/3 builtin-branch: use strbuf in fill_tracking_info()Miklos Vajna, Nov 17, 2008
  4. 3/3 builtin-branch: use strbuf in rename_branch()Miklos Vajna, Nov 17, 2008
  5. Junio C HamanoNov 18, 2008
  6. Johannes SchindelinNov 19, 2008
  7. Lars HjemliNov 19, 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.