Merge branch 'rs/copy-array' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 11 Oct 2016 21:18:32 +0000 (14:18 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 11 Oct 2016 21:18:32 +0000 (14:18 -0700)
Code cleanup.

* rs/copy-array:
use COPY_ARRAY
add COPY_ARRAY

builtin/mv.c
commit.c
contrib/coccinelle/array.cocci [new file with mode: 0644]
git-compat-util.h
pack-revindex.c
pathspec.c
split-index.c
index 446a316738e0e739b6a89992879c6e4838f23c8f..2f43877bc9a17c5bef2906a383cc8cdd6f4f5b82 100644 (file)
@@ -26,7 +26,7 @@ static const char **internal_copy_pathspec(const char *prefix,
        int i;
        const char **result;
        ALLOC_ARRAY(result, count + 1);
-       memcpy(result, pathspec, count * sizeof(const char *));
+       COPY_ARRAY(result, pathspec, count);
        result[count] = NULL;
        for (i = 0; i < count; i++) {
                int length = strlen(result[i]);
index ba6dee37aa36cc7141649deda6407012cd0f2724..aada266f9a85e379ab2d2603b82071be4bb19ded 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -931,7 +931,7 @@ static int remove_redundant(struct commit **array, int cnt)
        }
 
        /* Now collect the result */
-       memcpy(work, array, sizeof(*array) * cnt);
+       COPY_ARRAY(work, array, cnt);
        for (i = filled = 0; i < cnt; i++)
                if (!redundant[i])
                        array[filled++] = work[i];
diff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci
new file mode 100644 (file)
index 0000000..2d7f25d
--- /dev/null
@@ -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);
index 2c949983dc8313d78b6b45e635f7efb5d61e9dec..aec8cc98909237a17afde7809372389000a35a7e 100644 (file)
@@ -798,6 +798,14 @@ extern FILE *fopen_for_writing(const char *path);
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
 
+#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
+       BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
+static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
+{
+       if (n)
+               memcpy(dst, src, st_mult(size, n));
+}
+
 /*
  * These functions help you allocate structs with flex arrays, and copy
  * the data directly into the array. For example, if you had:
index 96d51c3467b9ef864f62962aa3567247338d2c3a..6bc7c940335cdf2d31cb38c2db6f8f6c985d3c3b 100644 (file)
@@ -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.
         */
        if (from != entries)
-               memcpy(entries, tmp, n * sizeof(*entries));
+               COPY_ARRAY(entries, tmp, n);
        free(tmp);
        free(pos);
 
index 24e0dd52322baf4a507765ae0948c98f0ec30602..49a53607bb004e8ccc34e6a18eadc8dfc2e7879d 100644 (file)
@@ -485,8 +485,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
 {
        *dst = *src;
        ALLOC_ARRAY(dst->items, dst->nr);
-       memcpy(dst->items, src->items,
-              sizeof(struct pathspec_item) * dst->nr);
+       COPY_ARRAY(dst->items, src->items, dst->nr);
 }
 
 void clear_pathspec(struct pathspec *pathspec)
index 3c75d4b9ce717fa92520881ef754485d068a3a60..35da553655bdf7b70519ecf529544796ca8185b5 100644 (file)
@@ -83,8 +83,7 @@ void move_cache_to_base_index(struct index_state *istate)
        si->base->timestamp = istate->timestamp;
        ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
        si->base->cache_nr = istate->cache_nr;
-       memcpy(si->base->cache, istate->cache,
-              sizeof(*istate->cache) * istate->cache_nr);
+       COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
        mark_base_index_entries(si->base);
        for (i = 0; i < si->base->cache_nr; i++)
                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_alloc = 0;
        ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
-       memcpy(istate->cache, si->base->cache,
-              sizeof(*istate->cache) * istate->cache_nr);
+       COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
 
        si->nr_deletions = 0;
        si->nr_replacements = 0;