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

[PATCH] Fix mkpath abuse in dwim_ref/sha1_name.c

From
Alex Riesen <raa.lkml@gmail.com>
Date
Oct 14, 2008, 16:23 UTC
Message-ID
<81b0412b0810140923x5cf58bb9x5acd1517a19e9847@mail.gmail.com>

Otherwise the function sometimes fail to resolve obviously correct refnames, because the string data pointed to by "ref" argument were reused.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 sha1_name.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

Frankly, I'm having hard time trying to justify _any_ use of mkpath. It is a bug waiting to happen every time is any code between the call site and assignment but strdup.

From 052e137d4ad8687d78bb2570390ed4f6b19f7cba Mon Sep 17 00:00:00 2001
From: Alex Riesen <raa.lkml@gmail.com>
Date: Tue, 14 Oct 2008 18:14:20 +0200
Subject: [PATCH] Fix mkpath abuse in sha1_name.c

Otherwise the function sometimes fail to resolve obviously correct refnames, because the string data pointed to by "ref" argument were reused.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 sha1_name.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 41b6809..b5b53bf 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -242,6 +242,7 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
 {
 	const char **p, *r;
 	int refs_found = 0;
+	char fullref[PATH_MAX];
 
 	*ref = NULL;
 	for (p = ref_rev_parse_rules; *p; p++) {
@@ -249,7 +250,8 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
 		unsigned char *this_result;
 
 		this_result = refs_found ? sha1_from_ref : sha1;
-		r = resolve_ref(mkpath(*p, len, str), this_result, 1, NULL);
+		snprintf(fullref, sizeof(fullref), *p, len, str);
+		r = resolve_ref(fullref, this_result, 1, NULL);
 		if (r) {
 			if (!refs_found++)
 				*ref = xstrdup(r);
@@ -272,7 +274,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
 		char path[PATH_MAX];
 		const char *ref, *it;
 
-		strcpy(path, mkpath(*p, len, str));
+		snprintf(path, sizeof(path), *p, len, str);
 		ref = resolve_ref(path, hash, 1, NULL);
 		if (!ref)
 			continue;
-- 
1.6.0.2.494.gb25da
Next: Junio C Hamano
Message 1 of 5 in “Fix mkpath abuse in dwim_ref/sha1_name.c”
  1. Fix mkpath abuse in dwim_ref/sha1_name.cAlex Riesen, Oct 14, 2008
  2. Junio C HamanoOct 14, 2008
  3. Alex RiesenOct 15, 2008
  4. Junio C HamanoOct 15, 2008
  5. Alex RiesenOct 16, 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.