Skip to content

Commit a813b19

Browse files
committed
Merge branch 'rs/copy-array' into maint
Code cleanup. * rs/copy-array: use COPY_ARRAY add COPY_ARRAY
2 parents f7e2e59 + 45ccef8 commit a813b19

File tree

7 files changed

+40
-9
lines changed

7 files changed

+40
-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);

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,14 @@ extern FILE *fopen_for_writing(const char *path);
798798
#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
799799
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
800800

801+
#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
802+
BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
803+
static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
804+
{
805+
if (n)
806+
memcpy(dst, src, st_mult(size, n));
807+
}
808+
801809
/*
802810
* These functions help you allocate structs with flex arrays, and copy
803811
* the data directly into the array. For example, if you had:

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)