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

Re: [PATCH 2/2] builtin git-mv: support moving directories

From
Junio C Hamano <junkio@cox.net>
Date
Aug 8, 2006, 19:52 UTC
Message-ID
<7virl3vwck.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<17624.55600.423000.61479@lapjr.intranet.kiel.bmiag.de>
Juergen Ruehle <j.ruehle@bmiag.de> writes:
Show 19 quoted lines
> This might have been fixed already (my tree is a couple of days old),
> but the strcat fails for me, because the alloced memory is not
> cleared.
>
> Johannes Schindelin writes:
>  > +static const char *add_slash(const char *path)
>  > +{
>  > +	int len = strlen(path);
>  > +	if (path[len - 1] != '/') {
>  > +		char *with_slash = xmalloc(len + 2);
>  > +		memcpy(with_slash, path, len);
>  > +		strcat(with_slash + len, "/");
>  > +		return with_slash;
>  > +	}
>  > +	return path;
>  > +}
>
> perhaps morph the strcat into a memcopy or append the slash and the
> NUL manually?

Thanks, well spotted. A textually minimum change would be to do:

- 		memcpy(with_slash, path, len);
+ 		memcpy(with_slash, path, len + 1);

but that would touch the end of the string twice, so manually terminating the string with '/' NUL would be appropriate.

I will apply a patch I've been privately using from time to time to catch something like this to "master".

-- >8 -- [PATCH] debugging: XMALLOC_POISON

Compile with -DXMALLOC_POISON=1 to catch errors from using uninitialized memory returned by xmalloc.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 git-compat-util.h |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 93f5580..3bcf5b1 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -91,6 +91,9 @@ static inline void *xmalloc(size_t size)
 		ret = malloc(1);
 	if (!ret)
 		die("Out of memory, malloc failed");
+#ifdef XMALLOC_POISON
+	memset(ret, 0xA5, size);
+#endif
 	return ret;
 }
 
-- 
1.4.2.rc3.g45c5
Previous: Juergen RuehleNext: Johannes Schindelin
Message 4 of 5 in “builtin git-mv: support moving directories”
  1. 2/2 builtin git-mv: support moving directoriesJohannes Schindelin, Jul 26, 2006
  2. Junio C HamanoJul 26, 2006
  3. Juergen RuehleAug 8, 2006
  4. Junio C HamanoAug 8, 2006
  5. mv: strcat -> strcpy to account for uninitialized memoryJohannes Schindelin, Aug 8, 2006

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.