cocci: use ALLOC_ARRAY

Add a semantic patch for using ALLOC_ARRAY to allocate arrays and apply the transformation on the current source tree. The macro checks for multiplication overflow and infers the element size automatically; the result is shorter and safer code. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 25, 2017 at 11:30 UTC 3f64699ffde6cd2152db2106505a2310601a207f
2 files changed +17 -1
contrib/coccinelle/array.cocci
+16
@@ -24,3 +24,19 @@ expression n;
24 @@
25 - memcpy(dst, src, n * sizeof(T));
26 + COPY_ARRAY(dst, src, n);
27 +
28 +@@
29 +type T;
30 +T *ptr;
31 +expression n;
32 +@@
33 +- ptr = xmalloc(n * sizeof(*ptr));
34 ++ ALLOC_ARRAY(ptr, n);
35 +
36 +@@
37 +type T;
38 +T *ptr;
39 +expression n;
40 +@@
41 +- ptr = xmalloc(n * sizeof(T));
42 ++ ALLOC_ARRAY(ptr, n);
worktree.c
+1 -1
@@ -175,7 +175,7 @@ struct worktree **get_worktrees(unsigned flags)
175 struct dirent *d;
176 int counter = 0, alloc = 2;
177
178 - list = xmalloc(alloc * sizeof(struct worktree *));
178 + ALLOC_ARRAY(list, alloc);
179
180 list[counter++] = get_main_worktree();
181