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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
60566cbb58
commit
45ccef87b3
@ -26,7 +26,7 @@ static const char **internal_copy_pathspec(const char *prefix,
|
|||||||
int i;
|
int i;
|
||||||
const char **result;
|
const char **result;
|
||||||
ALLOC_ARRAY(result, count + 1);
|
ALLOC_ARRAY(result, count + 1);
|
||||||
memcpy(result, pathspec, count * sizeof(const char *));
|
COPY_ARRAY(result, pathspec, count);
|
||||||
result[count] = NULL;
|
result[count] = NULL;
|
||||||
for (i = 0; i < count; i++) {
|
for (i = 0; i < count; i++) {
|
||||||
int length = strlen(result[i]);
|
int length = strlen(result[i]);
|
||||||
|
2
commit.c
2
commit.c
@ -931,7 +931,7 @@ static int remove_redundant(struct commit **array, int cnt)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Now collect the result */
|
/* Now collect the result */
|
||||||
memcpy(work, array, sizeof(*array) * cnt);
|
COPY_ARRAY(work, array, cnt);
|
||||||
for (i = filled = 0; i < cnt; i++)
|
for (i = filled = 0; i < cnt; i++)
|
||||||
if (!redundant[i])
|
if (!redundant[i])
|
||||||
array[filled++] = work[i];
|
array[filled++] = work[i];
|
||||||
|
26
contrib/coccinelle/array.cocci
Normal file
26
contrib/coccinelle/array.cocci
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
@@
|
||||||
|
type T;
|
||||||
|
T *dst;
|
||||||
|
T *src;
|
||||||
|
expression n;
|
||||||
|
@@
|
||||||
|
- memcpy(dst, src, n * sizeof(*dst));
|
||||||
|
+ COPY_ARRAY(dst, src, n);
|
||||||
|
|
||||||
|
@@
|
||||||
|
type T;
|
||||||
|
T *dst;
|
||||||
|
T *src;
|
||||||
|
expression n;
|
||||||
|
@@
|
||||||
|
- memcpy(dst, src, n * sizeof(*src));
|
||||||
|
+ COPY_ARRAY(dst, src, n);
|
||||||
|
|
||||||
|
@@
|
||||||
|
type T;
|
||||||
|
T *dst;
|
||||||
|
T *src;
|
||||||
|
expression n;
|
||||||
|
@@
|
||||||
|
- memcpy(dst, src, n * sizeof(T));
|
||||||
|
+ COPY_ARRAY(dst, src, n);
|
@ -107,7 +107,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
|
|||||||
* we have to move it back from the temporary storage.
|
* we have to move it back from the temporary storage.
|
||||||
*/
|
*/
|
||||||
if (from != entries)
|
if (from != entries)
|
||||||
memcpy(entries, tmp, n * sizeof(*entries));
|
COPY_ARRAY(entries, tmp, n);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
free(pos);
|
free(pos);
|
||||||
|
|
||||||
|
@ -485,8 +485,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
|
|||||||
{
|
{
|
||||||
*dst = *src;
|
*dst = *src;
|
||||||
ALLOC_ARRAY(dst->items, dst->nr);
|
ALLOC_ARRAY(dst->items, dst->nr);
|
||||||
memcpy(dst->items, src->items,
|
COPY_ARRAY(dst->items, src->items, dst->nr);
|
||||||
sizeof(struct pathspec_item) * dst->nr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear_pathspec(struct pathspec *pathspec)
|
void clear_pathspec(struct pathspec *pathspec)
|
||||||
|
@ -83,8 +83,7 @@ void move_cache_to_base_index(struct index_state *istate)
|
|||||||
si->base->timestamp = istate->timestamp;
|
si->base->timestamp = istate->timestamp;
|
||||||
ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
|
ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
|
||||||
si->base->cache_nr = istate->cache_nr;
|
si->base->cache_nr = istate->cache_nr;
|
||||||
memcpy(si->base->cache, istate->cache,
|
COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
|
||||||
sizeof(*istate->cache) * istate->cache_nr);
|
|
||||||
mark_base_index_entries(si->base);
|
mark_base_index_entries(si->base);
|
||||||
for (i = 0; i < si->base->cache_nr; i++)
|
for (i = 0; i < si->base->cache_nr; i++)
|
||||||
si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
|
si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
|
||||||
@ -141,8 +140,7 @@ void merge_base_index(struct index_state *istate)
|
|||||||
istate->cache = NULL;
|
istate->cache = NULL;
|
||||||
istate->cache_alloc = 0;
|
istate->cache_alloc = 0;
|
||||||
ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
|
ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
|
||||||
memcpy(istate->cache, si->base->cache,
|
COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
|
||||||
sizeof(*istate->cache) * istate->cache_nr);
|
|
||||||
|
|
||||||
si->nr_deletions = 0;
|
si->nr_deletions = 0;
|
||||||
si->nr_replacements = 0;
|
si->nr_replacements = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user