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

[PATCH] Don't use memcpy when source and dest. buffers may overlap

From
Jim Meyering <jim@meyering.net>
Date
Dec 11, 2006, 18:06 UTC
Message-ID
<87slfm489h.fsf@rho.meyering.net>

git-index-pack can call memcpy with overlapping source and destination buffers. The patch below makes it use memmove instead.

If you want to demonstrate a failure, add the following two lines
+               if (input_offset < input_len)
+                 abort ();

before the existing memcpy call (shown in the patch below), and then run this:

  (cd t; sh ./t5500-fetch-pack.sh)
Signed-off-by: Jim Meyering <jim@meyering.net>
---
 index-pack.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/index-pack.c b/index-pack.c
index 8331d99..6d6c92b 100644
--- a/index-pack.c
+++ b/index-pack.c
@@ -96,7 +96,7 @@ static void flush(void)
 		if (output_fd >= 0)
 			write_or_die(output_fd, input_buffer, input_offset);
 		SHA1_Update(&input_ctx, input_buffer, input_offset);
-		memcpy(input_buffer, input_buffer + input_offset, input_len);
+		memmove(input_buffer, input_buffer + input_offset, input_len);
 		input_offset = 0;
 	}
 }
--
Message 1 of 1 in “Don't use memcpy when source and dest. buffers may overlap”
  1. Don't use memcpy when source and dest. buffers may overlapJim Meyering, Dec 11, 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.