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

[PATCH] Fix unterminated string copy in set_work_tree

From
Alex Riesen <raa.lkml@gmail.com>
Date
Aug 2, 2007, 22:02 UTC
Message-ID
<20070802220255.GB2829@steel.home>
In-Reply-To
<Pine.LNX.4.64.0708022230070.14781@racer.site>
Use strlcpy which zero-terminates the output string
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
Johannes Schindelin, Thu, Aug 02, 2007 23:36:37 +0200:
Show 6 quoted lines
> On Thu, 2 Aug 2007, Junio C Hamano wrote:
> > 
> > Static is supposed to be zeroed and also is supposed to retain
> > the value from the previous call.  I am guessing from the change
> > to make "rel" to non-static that this function is called twice
> > perhaps?

Actually, I was very confused. When I wrote about cygwin problems, I actually debugged it for dir_buffer, real stack-based variable, which of course is not zero-initialized. For an unknown reason I confused the variable with buffer, which is static. "rel" should be left of this particular discussion (it just does not matter whether it is static or not in this context).

So the fix is a real fix for real problem which just happens to be invisible on our linux systems.

> Apparently (but I would feel safer with strlcpy() anyway).  git-read-tree 
> is the first and only offender which comes up in the test suite:
Yes, I feel so too, so here it is.
 setup.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/setup.c b/setup.c
index 3653092..27d585c 100644
--- a/setup.c
+++ b/setup.c
@@ -209,7 +209,7 @@ const char *set_work_tree(const char *dir)
 	len = strlen(dir);
 	if (len > postfix_len && !strcmp(dir + len - postfix_len,
 				"/" DEFAULT_GIT_DIR_ENVIRONMENT)) {
-			strncpy(dir_buffer, dir, len - postfix_len);
+		strlcpy(dir_buffer, dir, len - postfix_len + 1);
 
 		/* are we inside the default work tree? */
 		rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer);
-- 
1.5.3.rc3.139.ga57724
Previous: Johannes Schindelin
Message 9 of 9 in “Fix set_work_tree on cygwin”
  1. Fix set_work_tree on cygwinAlex Riesen, Aug 2, 2007
  2. Johannes SchindelinAug 2, 2007
  3. Alex RiesenAug 2, 2007
  4. Johannes SchindelinAug 2, 2007
  5. Junio C HamanoAug 2, 2007
  6. Johannes SchindelinAug 2, 2007
  7. Junio C HamanoAug 2, 2007
  8. Allow setup_work_tree() to be called several timesJohannes Schindelin, Aug 2, 2007
  9. Fix unterminated string copy in set_work_treeAlex Riesen, Aug 2, 2007

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.