Skip to content

Commit 45ccef8

Browse files
rscharfegitster
authored andcommitted
use COPY_ARRAY
Add a semantic patch for converting certain calls of memcpy(3) to COPY_ARRAY() and apply that transformation to the code base. The result is shorter and safer code. For now only consider calls where source and destination have the same type, or in other words: easy cases. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 60566cb commit 45ccef8

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static const char **internal_copy_pathspec(const char *prefix,
2626
int i;
2727
const char **result;
2828
ALLOC_ARRAY(result, count + 1);
29-
memcpy(result, pathspec, count * sizeof(const char *));
29+
COPY_ARRAY(result, pathspec, count);
3030
result[count] = NULL;
3131
for (i = 0; i < count; i++) {
3232
int length = strlen(result[i]);

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ static int remove_redundant(struct commit **array, int cnt)
931931
}
932932

933933
/* Now collect the result */
934-
memcpy(work, array, sizeof(*array) * cnt);
934+
COPY_ARRAY(work, array, cnt);
935935
for (i = filled = 0; i < cnt; i++)
936936
if (!redundant[i])
937937
array[filled++] = work[i];

contrib/coccinelle/array.cocci

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@@
2+
type T;
3+
T *dst;
4+
T *src;
5+
expression n;
6+
@@
7+
- memcpy(dst, src, n * sizeof(*dst));
8+
+ COPY_ARRAY(dst, src, n);
9+
10+
@@
11+
type T;
12+
T *dst;
13+
T *src;
14+
expression n;
15+
@@
16+
- memcpy(dst, src, n * sizeof(*src));
17+
+ COPY_ARRAY(dst, src, n);
18+
19+
@@
20+
type T;
21+
T *dst;
22+
T *src;
23+
expression n;
24+
@@
25+
- memcpy(dst, src, n * sizeof(T));
26+
+ COPY_ARRAY(dst, src, n);

pack-revindex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
107107
* we have to move it back from the temporary storage.
108108
*/
109109
if (from != entries)
110-
memcpy(entries, tmp, n * sizeof(*entries));
110+
COPY_ARRAY(entries, tmp, n);
111111
free(tmp);
112112
free(pos);
113113

pathspec.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
485485
{
486486
*dst = *src;
487487
ALLOC_ARRAY(dst->items, dst->nr);
488-
memcpy(dst->items, src->items,
489-
sizeof(struct pathspec_item) * dst->nr);
488+
COPY_ARRAY(dst->items, src->items, dst->nr);
490489
}
491490

492491
void clear_pathspec(struct pathspec *pathspec)

split-index.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ void move_cache_to_base_index(struct index_state *istate)
8383
si->base->timestamp = istate->timestamp;
8484
ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
8585
si->base->cache_nr = istate->cache_nr;
86-
memcpy(si->base->cache, istate->cache,
87-
sizeof(*istate->cache) * istate->cache_nr);
86+
COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
8887
mark_base_index_entries(si->base);
8988
for (i = 0; i < si->base->cache_nr; i++)
9089
si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
@@ -141,8 +140,7 @@ void merge_base_index(struct index_state *istate)
141140
istate->cache = NULL;
142141
istate->cache_alloc = 0;
143142
ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
144-
memcpy(istate->cache, si->base->cache,
145-
sizeof(*istate->cache) * istate->cache_nr);
143+
COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
146144

147145
si->nr_deletions = 0;
148146
si->nr_replacements = 0;

0 commit comments

Comments
 (0)