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

[PATCH] builtin/mv.c: use correct type to compute size of an array element

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 7, 2022, 02:02 UTC
Message-ID
<xmqq8rp54r4l.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 *".

Noticed while running "make contrib/coccinelle/array.cocci.patch".
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * There is this rule in the array.cocci file:
        @@
        type T;
        T *dst;
        T *src;
        expression n;
        @@
        (
        - memmove(dst, src, (n) * sizeof(*dst));
        + MOVE_ARRAY(dst, src, n);
        |
        - memmove(dst, src, (n) * sizeof(*src));
        + MOVE_ARRAY(dst, src, n);
        |
        - memmove(dst, src, (n) * sizeof(T));
        + MOVE_ARRAY(dst, src, n);
        )
   but it triggered only for modes[] array among the four parallel
   arrays that are being moved here.
   There are a few tangents.
   * Should we in general use sizeof(TYPE) in these cases, instead
     of the size of the zeroth element, e.g.
 		memmove(source + i, source + i + 1,
			n * sizeof(source[i]));
    
     It would have been caught by the above Coccinelle rule (we are
     taking the size of *dst).
   * Shouldn't we have an array of struct with four members, instead
     of four parallel arrays, e.g.
		struct {
			const char *source;
			const char *destination;
			enum update_mode mode;
			const char *submodule_gitfile;
		} *mv_file;
   The latter question is important to answer before we accept
   Coccinelle-suggested rewrite to use four MOVE_ARRAY() on these
   four parallel arrays on top of this fix.
 builtin/mv.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git c/builtin/mv.c w/builtin/mv.c
index 2a38e2af46..d419e83f0f 100644
--- c/builtin/mv.c
+++ w/builtin/mv.c
@@ -377,13 +377,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		if (--argc > 0) {
 			int n = argc - i;
 			memmove(source + i, source + i + 1,
-				n * sizeof(char *));
+				n * sizeof(const char *));
 			memmove(destination + i, destination + i + 1,
-				n * sizeof(char *));
+				n * sizeof(const char *));
 			memmove(modes + i, modes + i + 1,
 				n * sizeof(enum update_mode));
 			memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
-				n * sizeof(char *));
+				n * sizeof(const char *));
 			i--;
 		}
 	}
Next: Junio C Hamano
Message 1 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.