use MOVE_ARRAY
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>
René Scharfe committed
Jul 15, 2017 at 22:00 UTC
f331ab9d4cb21942dcde6d879aaca6a1784e8cb6
9 files changed
+17
-26
builtin/ls-files.c
+1
-2
@@ -378,8 +378,7 @@ static void prune_index(struct index_state *istate,
378
}
379
last = next;
380
}
381
- memmove(istate->cache, istate->cache + pos,
382
- (last - pos) * sizeof(struct cache_entry *));
381
+ MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
382
istate->cache_nr = last - pos;
383
}
384
builtin/merge.c
+1
-1
@@ -537,7 +537,7 @@ static void parse_branch_merge_options(char *bmo)
537
die(_("Bad branch.%s.mergeoptions string: %s"), branch,
538
split_cmdline_strerror(argc));
539
REALLOC_ARRAY(argv, argc + 2);
540
- memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
540
+ MOVE_ARRAY(argv + 1, argv, argc + 1);
541
argc++;
542
argv[0] = "branch.*.mergeoptions";
543
parse_options(argc, argv, NULL, builtin_merge_options,
builtin/pack-objects.c
+2
-3
@@ -1298,9 +1298,8 @@ static int check_pbase_path(unsigned hash)
1298
done_pbase_paths_alloc);
1299
done_pbase_paths_num++;
1300
if (pos < done_pbase_paths_num)
1301
- memmove(done_pbase_paths + pos + 1,
1302
- done_pbase_paths + pos,
1303
- (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1301
+ MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1302
+ done_pbase_paths_num - pos - 1);
1303
done_pbase_paths[pos] = hash;
1304
return 0;
1305
}
cache-tree.c
+2
-3
@@ -131,9 +131,8 @@ static int do_invalidate_path(struct cache_tree *it, const char *path)
131
* move 4 and 5 up one place (2 entries)
132
* 2 = 6 - 3 - 1 = subtree_nr - pos - 1
133
*/
134
- memmove(it->down+pos, it->down+pos+1,
135
- sizeof(struct cache_tree_sub *) *
136
- (it->subtree_nr - pos - 1));
134
+ MOVE_ARRAY(it->down + pos, it->down + pos + 1,
135
+ it->subtree_nr - pos - 1);
136
it->subtree_nr--;
137
}
138
return 1;
commit.c
+2
-3
@@ -223,9 +223,8 @@ int unregister_shallow(const struct object_id *oid)
223
if (pos < 0)
224
return -1;
225
if (pos + 1 < commit_graft_nr)
226
- memmove(commit_graft + pos, commit_graft + pos + 1,
227
- sizeof(struct commit_graft *)
228
- * (commit_graft_nr - pos - 1));
226
+ MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
227
+ commit_graft_nr - pos - 1);
228
commit_graft_nr--;
229
return 0;
230
}
notes-merge.c
+1
-2
@@ -99,8 +99,7 @@ static struct notes_merge_pair *find_notes_merge_pair_pos(
99
else {
100
*occupied = 0;
101
if (insert_new && i < len) {
102
- memmove(list + i + 1, list + i,
103
- (len - i) * sizeof(struct notes_merge_pair));
102
+ MOVE_ARRAY(list + i + 1, list + i, len - i);
103
memset(list + i, 0, sizeof(struct notes_merge_pair));
104
}
105
}
read-cache.c
+2
-3
@@ -515,9 +515,8 @@ int remove_index_entry_at(struct index_state *istate, int pos)
515
istate->cache_nr--;
516
if (pos >= istate->cache_nr)
517
return 0;
518
- memmove(istate->cache + pos,
519
- istate->cache + pos + 1,
520
- (istate->cache_nr - pos) * sizeof(struct cache_entry *));
518
+ MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
519
+ istate->cache_nr - pos);
520
return 1;
521
}
522
reflog-walk.c
+3
-4
@@ -111,10 +111,9 @@ static struct commit_info *get_commit_info(struct commit *commit,
111
struct commit_info *result = &lifo->items[i];
112
if (pop) {
113
if (i + 1 < lifo->nr)
114
- memmove(lifo->items + i,
115
- lifo->items + i + 1,
116
- (lifo->nr - i) *
117
- sizeof(struct commit_info));
114
+ MOVE_ARRAY(lifo->items + i,
115
+ lifo->items + i + 1,
116
+ lifo->nr - i);
117
lifo->nr--;
118
}
119
return result;
string-list.c
+3
-5
@@ -43,9 +43,8 @@ static int add_entry(int insert_at, struct string_list *list, const char *string
43
44
ALLOC_GROW(list->items, list->nr+1, list->alloc);
45
if (index < list->nr)
46
- memmove(list->items + index + 1, list->items + index,
47
- (list->nr - index)
48
- * sizeof(struct string_list_item));
46
+ MOVE_ARRAY(list->items + index + 1, list->items + index,
47
+ list->nr - index);
48
list->items[index].string = list->strdup_strings ?
49
xstrdup(string) : (char *)string;
50
list->items[index].util = NULL;
@@ -77,8 +76,7 @@ void string_list_remove(struct string_list *list, const char *string,
76
free(list->items[i].util);
77
78
list->nr--;
80
- memmove(list->items + i, list->items + i + 1,
81
- (list->nr - i) * sizeof(struct string_list_item));
79
+ MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
80
}
81
}
82