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

[PATCH 6/9] index-pack: use strip_suffix to avoid magic numbers

From
Jeff King <peff@peff.net>
Date
Jun 30, 2014, 16:59 UTC
Message-ID
<20140630165910.GF16637@sigill.intra.peff.net>
In-Reply-To
<20140630165526.GA15690@sigill.intra.peff.net>

We also switch to using strbufs, which lets us avoid the potentially dangerous combination of a manual malloc followed by a strcpy.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/index-pack.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 46376b6..d4b77fd 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1505,7 +1505,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	const char *curr_index;
 	const char *index_name = NULL, *pack_name = NULL;
 	const char *keep_name = NULL, *keep_msg = NULL;
-	char *index_name_buf = NULL, *keep_name_buf = NULL;
+	struct strbuf index_name_buf = STRBUF_INIT,
+		      keep_name_buf = STRBUF_INIT;
 	struct pack_idx_entry **idx_objects;
 	struct pack_idx_option opts;
 	unsigned char pack_sha1[20];
@@ -1602,24 +1603,22 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	if (fix_thin_pack && !from_stdin)
 		die(_("--fix-thin cannot be used without --stdin"));
 	if (!index_name && pack_name) {
-		int len = strlen(pack_name);
-		if (!ends_with(pack_name, ".pack"))
+		size_t len;
+		if (!strip_suffix(pack_name, ".pack", &len))
 			die(_("packfile name '%s' does not end with '.pack'"),
 			    pack_name);
-		index_name_buf = xmalloc(len);
-		memcpy(index_name_buf, pack_name, len - 5);
-		strcpy(index_name_buf + len - 5, ".idx");
-		index_name = index_name_buf;
+		strbuf_add(&index_name_buf, pack_name, len);
+		strbuf_addstr(&index_name_buf, ".idx");
+		index_name = index_name_buf.buf;
 	}
 	if (keep_msg && !keep_name && pack_name) {
-		int len = strlen(pack_name);
-		if (!ends_with(pack_name, ".pack"))
+		size_t len;
+		if (!strip_suffix(pack_name, ".pack", &len))
 			die(_("packfile name '%s' does not end with '.pack'"),
 			    pack_name);
-		keep_name_buf = xmalloc(len);
-		memcpy(keep_name_buf, pack_name, len - 5);
-		strcpy(keep_name_buf + len - 5, ".keep");
-		keep_name = keep_name_buf;
+		strbuf_add(&keep_name_buf, pack_name, len);
+		strbuf_addstr(&keep_name_buf, ".idx");
+		keep_name = keep_name_buf.buf;
 	}
 	if (verify) {
 		if (!index_name)
@@ -1667,8 +1666,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	else
 		close(input_fd);
 	free(objects);
-	free(index_name_buf);
-	free(keep_name_buf);
+	strbuf_release(&index_name_buf);
+	strbuf_release(&keep_name_buf);
 	if (pack_name == NULL)
 		free((void *) curr_pack);
 	if (index_name == NULL)
-- 
2.0.0.566.gfe3e6b2
Previous: Jeff KingNext: Jeff King
Message 22 of 27 in “sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one()”
  1. 1/2 sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one()René Scharfe, Jun 28, 2014
  2. 2/4 sha1_file: use strncmp for string comparisonRené Scharfe, Jun 28, 2014
  3. Duy NguyenJun 29, 2014
  4. 1/2 sha1_file: replace PATH_MAX buffer wirh strbuf in, prepare_packed_git_one()René Scharfe, Jun 29, 2014
  5. 2/2 sha1_file: use strncmp for string comparisonRené Scharfe, Jun 29, 2014
  6. Jeff KingJun 30, 2014
  7. Duy NguyenJun 30, 2014
  8. Jeff KingJun 30, 2014
  9. René ScharfeJun 30, 2014
  10. Jeff KingJun 30, 2014
  11. René ScharfeJun 30, 2014
  12. Jeff KingJun 30, 2014
  13. 0/9 add strip_suffix as an alternative to ends_withJeff King, Jun 30, 2014
  14. 1/9 sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one()Jeff King, Jun 30, 2014
  15. 2/9 add strip_suffix functionJeff King, Jun 30, 2014
  16. Junio C HamanoJul 2, 2014
  17. Jeff KingJul 2, 2014
  18. Junio C HamanoJul 2, 2014
  19. 3/9 implement ends_with via strip_suffixJeff King, Jun 30, 2014
  20. 4/9 replace has_extension with ends_withJeff King, Jun 30, 2014
  21. 5/9 use strip_suffix instead of ends_with in simple casesJeff King, Jun 30, 2014
  22. 6/9 index-pack: use strip_suffix to avoid magic numbersJeff King, Jun 30, 2014
  23. 7/9 strbuf: implement strbuf_strip_suffixJeff King, Jun 30, 2014
  24. 8/9 verify-pack: use strbuf_strip_suffixJeff King, Jun 30, 2014
  25. 9/9 prepare_packed_git_one: refactor duplicate-pack checkJeff King, Jun 30, 2014
  26. Junio C HamanoJul 1, 2014
  27. Jeff KingJun 30, 2014

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.