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

[PATCH v3] builtin/mv.c: use the MOVE_ARRAY() macro instead of memmove()

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 10, 2022, 01:33 UTC
Message-ID
<xmqq4jzpu4xp.fsf_-_@gitster.g>
In-Reply-To
<xmqqy1x531vp.fsf@gitster.g>

The variables 'source', 'destination', and 'submodule_gitfile' are all of type "const char **", and an element of such an array is of "type const char *", but these memmove() calls were written as if these variables are of type "char **".

Once these memmove() calls are fixed to use the correct type to compute the number of bytes to be moved, e.g.

-      memmove(source + i, source + i + 1, n * sizeof(char *));
+      memmove(source + i, source + i + 1, n * sizeof(const char *));

existing contrib/coccinelle/array.cocci rules can recognize them as candidates for turning into MOVE_ARRAY().

While at it, use CALLOC_ARRAY() instead of xcalloc() to allocate the modes[] array that is involved in the change.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * The first hunk is new; with this additional rewrite from
   xcalloc() to CALLOC_ARRAY(), the static-analysis job on 'seen'
   starts passing (cf. https://github.com/git/git/runs/7267146111)
   I think this is now good enough to merge down to 'next' and to
   'master'.
 builtin/mv.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/builtin/mv.c b/builtin/mv.c
index 83a465ba83..859fa5023f 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -148,7 +148,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		die(_("index file corrupt"));
 
 	source = internal_prefix_pathspec(prefix, argv, argc, 0);
-	modes = xcalloc(argc, sizeof(enum update_mode));
+	CALLOC_ARRAY(modes, argc);
+
 	/*
 	 * Keep trailing slash, needed to let
 	 * "git mv file no-such-dir/" error out, except in the case
@@ -282,14 +283,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 remove_entry:
 		if (--argc > 0) {
 			int n = argc - i;
-			memmove(source + i, source + i + 1,
-				n * sizeof(char *));
-			memmove(destination + i, destination + i + 1,
-				n * sizeof(char *));
-			memmove(modes + i, modes + i + 1,
-				n * sizeof(enum update_mode));
-			memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
-				n * sizeof(char *));
+			MOVE_ARRAY(source + i, source + i + 1, n);
+			MOVE_ARRAY(destination + i, destination + i + 1, n);
+			MOVE_ARRAY(modes + i, modes + i + 1, n);
+			MOVE_ARRAY(submodule_gitfile + i,
+				   submodule_gitfile + i + 1, n);
 			i--;
 		}
 	}
-- 
2.37.0-238-g7905cdbf0e
Previous: Junio C HamanoNext: Derrick Stolee
Message 3 of 18 in “builtin/mv.c: use correct type to compute size of an array element”
  1. builtin/mv.c: use correct type to compute size of an array elementJunio C Hamano, Jul 7, 2022
  2. builtin/mv.c: use the MOVE_ARRAY() macro instead of memmove()Junio C Hamano, Jul 7, 2022
  3. builtin/mv.c: use the MOVE_ARRAY() macro instead of memmove()Junio C Hamano, Jul 10, 2022
  4. Derrick StoleeJul 18, 2022
  5. Ævar Arnfjörð BjarmasonJul 7, 2022
  6. Junio C HamanoJul 7, 2022
  7. René ScharfeJul 7, 2022
  8. René ScharfeJul 9, 2022
  9. Junio C HamanoJul 10, 2022
  10. cocci: avoid normalization rules for memcpyRené Scharfe, Jul 10, 2022
  11. Ævar Arnfjörð BjarmasonJul 10, 2022
  12. Ævar Arnfjörð BjarmasonJul 10, 2022
  13. Junio C HamanoJul 10, 2022
  14. René ScharfeJul 11, 2022
  15. Ævar Arnfjörð BjarmasonJul 11, 2022
  16. René ScharfeJul 7, 2022
  17. Jeff KingJul 7, 2022
  18. Junio C HamanoJul 7, 2022

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.