use MOVE_ARRAY
authorRené Scharfe <l.s.r@web.de>
Sat, 15 Jul 2017 20:00:45 +0000 (22:00 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 Jul 2017 21:54:56 +0000 (14:54 -0700)
Simplify the code for moving members inside of an array and make it more
robust by using the helper macro MOVE_ARRAY. It calculates the size
based on the specified number of elements for us and supports NULL
pointers when that number is zero. Raw memmove(3) calls with NULL can
cause the compiler to (over-eagerly) optimize out later NULL checks.

This patch was generated with contrib/coccinelle/array.cocci and spatch
(Coccinelle).

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/ls-files.c
builtin/merge.c
builtin/pack-objects.c
cache-tree.c
commit.c
notes-merge.c
read-cache.c
reflog-walk.c
string-list.c
index b8514a0029a1437687a5cf6b6f8bad9792340bc3..dc4a6aa3d951b46220dcb084d3f86ca7a4cc1a29 100644 (file)
@@ -378,8 +378,7 @@ static void prune_index(struct index_state *istate,
                }
                last = next;
        }
                }
                last = next;
        }
-       memmove(istate->cache, istate->cache + pos,
-               (last - pos) * sizeof(struct cache_entry *));
+       MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
        istate->cache_nr = last - pos;
 }
 
        istate->cache_nr = last - pos;
 }
 
index 900bafdb45d0b28ab5497cfd251535266fc92be5..d5797b8fe77c99c89323348f3a0bd1d68ae9b453 100644 (file)
@@ -537,7 +537,7 @@ static void parse_branch_merge_options(char *bmo)
                die(_("Bad branch.%s.mergeoptions string: %s"), branch,
                    split_cmdline_strerror(argc));
        REALLOC_ARRAY(argv, argc + 2);
                die(_("Bad branch.%s.mergeoptions string: %s"), branch,
                    split_cmdline_strerror(argc));
        REALLOC_ARRAY(argv, argc + 2);
-       memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
+       MOVE_ARRAY(argv + 1, argv, argc + 1);
        argc++;
        argv[0] = "branch.*.mergeoptions";
        parse_options(argc, argv, NULL, builtin_merge_options,
        argc++;
        argv[0] = "branch.*.mergeoptions";
        parse_options(argc, argv, NULL, builtin_merge_options,
index f4a8441fe913850e7a1cadf3376837237519e761..e730b415bfdbe5f00e08cb28c8cced817067cfee 100644 (file)
@@ -1298,9 +1298,8 @@ static int check_pbase_path(unsigned hash)
                   done_pbase_paths_alloc);
        done_pbase_paths_num++;
        if (pos < done_pbase_paths_num)
                   done_pbase_paths_alloc);
        done_pbase_paths_num++;
        if (pos < done_pbase_paths_num)
-               memmove(done_pbase_paths + pos + 1,
-                       done_pbase_paths + pos,
-                       (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
+               MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
+                          done_pbase_paths_num - pos - 1);
        done_pbase_paths[pos] = hash;
        return 0;
 }
        done_pbase_paths[pos] = hash;
        return 0;
 }
index ec23d8c03d10bd3815f59c2d70ae5eb45170aaad..2440d1dc89175efaf74286da4b2c8f97fc52fbd9 100644 (file)
@@ -131,9 +131,8 @@ static int do_invalidate_path(struct cache_tree *it, const char *path)
                         * move 4 and 5 up one place (2 entries)
                         * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
                         */
                         * move 4 and 5 up one place (2 entries)
                         * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
                         */
-                       memmove(it->down+pos, it->down+pos+1,
-                               sizeof(struct cache_tree_sub *) *
-                               (it->subtree_nr - pos - 1));
+                       MOVE_ARRAY(it->down + pos, it->down + pos + 1,
+                                  it->subtree_nr - pos - 1);
                        it->subtree_nr--;
                }
                return 1;
                        it->subtree_nr--;
                }
                return 1;
index cbfd6899392e8715b206c96b3b58cbdaf29c77cd..d3150d627071316accafb7d9c69b5988050f4a8e 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -223,9 +223,8 @@ int unregister_shallow(const struct object_id *oid)
        if (pos < 0)
                return -1;
        if (pos + 1 < commit_graft_nr)
        if (pos < 0)
                return -1;
        if (pos + 1 < commit_graft_nr)
-               memmove(commit_graft + pos, commit_graft + pos + 1,
-                               sizeof(struct commit_graft *)
-                               * (commit_graft_nr - pos - 1));
+               MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
+                          commit_graft_nr - pos - 1);
        commit_graft_nr--;
        return 0;
 }
        commit_graft_nr--;
        return 0;
 }
index 70e3fbeefbe24233a545e75c0322a8a310778e43..c12b354f1003a29c6faf58e7ec970e4ac761812e 100644 (file)
@@ -99,8 +99,7 @@ static struct notes_merge_pair *find_notes_merge_pair_pos(
        else {
                *occupied = 0;
                if (insert_new && i < len) {
        else {
                *occupied = 0;
                if (insert_new && i < len) {
-                       memmove(list + i + 1, list + i,
-                               (len - i) * sizeof(struct notes_merge_pair));
+                       MOVE_ARRAY(list + i + 1, list + i, len - i);
                        memset(list + i, 0, sizeof(struct notes_merge_pair));
                }
        }
                        memset(list + i, 0, sizeof(struct notes_merge_pair));
                }
        }
index 2121b6e7bba5f310825e0b1f2eccd524cc8658ce..acfb028f480b5545aa82d5c0533fe8bdfc581dfd 100644 (file)
@@ -515,9 +515,8 @@ int remove_index_entry_at(struct index_state *istate, int pos)
        istate->cache_nr--;
        if (pos >= istate->cache_nr)
                return 0;
        istate->cache_nr--;
        if (pos >= istate->cache_nr)
                return 0;
-       memmove(istate->cache + pos,
-               istate->cache + pos + 1,
-               (istate->cache_nr - pos) * sizeof(struct cache_entry *));
+       MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
+                  istate->cache_nr - pos);
        return 1;
 }
 
        return 1;
 }
 
index 081f89b70d643514f148aaff9121263361fdb956..81bca2ed23387b4be64c581f35f238a6ffd12a69 100644 (file)
@@ -111,10 +111,9 @@ static struct commit_info *get_commit_info(struct commit *commit,
                        struct commit_info *result = &lifo->items[i];
                        if (pop) {
                                if (i + 1 < lifo->nr)
                        struct commit_info *result = &lifo->items[i];
                        if (pop) {
                                if (i + 1 < lifo->nr)
-                                       memmove(lifo->items + i,
-                                               lifo->items + i + 1,
-                                               (lifo->nr - i) *
-                                               sizeof(struct commit_info));
+                                       MOVE_ARRAY(lifo->items + i,
+                                                  lifo->items + i + 1,
+                                                  lifo->nr - i);
                                lifo->nr--;
                        }
                        return result;
                                lifo->nr--;
                        }
                        return result;
index c650500c6e51d983dc45a4be3b8dad98f1dece92..806b4c87232fb2ed307f6ce64817e14feefc216f 100644 (file)
@@ -43,9 +43,8 @@ static int add_entry(int insert_at, struct string_list *list, const char *string
 
        ALLOC_GROW(list->items, list->nr+1, list->alloc);
        if (index < list->nr)
 
        ALLOC_GROW(list->items, list->nr+1, list->alloc);
        if (index < list->nr)
-               memmove(list->items + index + 1, list->items + index,
-                               (list->nr - index)
-                               * sizeof(struct string_list_item));
+               MOVE_ARRAY(list->items + index + 1, list->items + index,
+                          list->nr - index);
        list->items[index].string = list->strdup_strings ?
                xstrdup(string) : (char *)string;
        list->items[index].util = NULL;
        list->items[index].string = list->strdup_strings ?
                xstrdup(string) : (char *)string;
        list->items[index].util = NULL;
@@ -77,8 +76,7 @@ void string_list_remove(struct string_list *list, const char *string,
                        free(list->items[i].util);
 
                list->nr--;
                        free(list->items[i].util);
 
                list->nr--;
-               memmove(list->items + i, list->items + i + 1,
-                       (list->nr - i) * sizeof(struct string_list_item));
+               MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
        }
 }
 
        }
 }