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

[PATCH] Fix mkpath abuse in dwim_ref and dwim_log of sha1_name.c

From
Alex Riesen <raa.lkml@gmail.com>
Date
Oct 26, 2008, 22:07 UTC
Message-ID
<20081026220724.GB18594@blimp.localdomain>
In-Reply-To
<20081026215913.GA18594@blimp.localdomain>

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

The change in dwim_log does not fix anything, just optimizes away strcpy code as the path can be created directly in the available buffer.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---

Was noticed in cygwin port, which somehow (supposedly by excessive calling of git_config from lstat stub setup) managed to reuse the just returned buffer.

 sha1_name.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 41b6809..159c2ab 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -245,11 +245,13 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
 
 	*ref = NULL;
 	for (p = ref_rev_parse_rules; *p; p++) {
+		char fullref[PATH_MAX];
 		unsigned char sha1_from_ref[20];
 		unsigned char *this_result;
 
 		this_result = refs_found ? sha1_from_ref : sha1;
-		r = resolve_ref(mkpath(*p, len, str), this_result, 1, NULL);
+		mksnpath(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));
+		mksnpath(path, sizeof(path), *p, len, str);
 		ref = resolve_ref(path, hash, 1, NULL);
 		if (!ref)
 			continue;
-- 
1.6.0.3.540.g3f8b
Previous: Alex RiesenNext: Alex Riesen
Message 2 of 10 in “Add mksnpath and git_snpath which allow to specify the output buffer”
  1. Add mksnpath and git_snpath which allow to specify the output bufferAlex Riesen, Oct 26, 2008
  2. Fix mkpath abuse in dwim_ref and dwim_log of sha1_name.cAlex Riesen, Oct 26, 2008
  3. Fix potentially dangerous uses of mkpath and git_pathAlex Riesen, Oct 26, 2008
  4. Johannes SixtOct 27, 2008
  5. Alex RiesenOct 27, 2008
  6. Junio C HamanoOct 27, 2008
  7. Alex RiesenOct 27, 2008
  8. Junio C HamanoOct 28, 2008
  9. Alex RiesenOct 28, 2008
  10. Alex RiesenOct 28, 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.