Commit 3f64699f authored by René Scharfe's avatar René Scharfe Committed by Junio C Hamano

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: 's avatarRene Scharfe <l.s.r@web.de>
Signed-off-by: 's avatarJunio C Hamano <gitster@pobox.com>
parent 3b9e3c2c
......@@ -24,3 +24,19 @@ expression n;
@@
- memcpy(dst, src, n * sizeof(T));
+ COPY_ARRAY(dst, src, n);
@@
type T;
T *ptr;
expression n;
@@
- ptr = xmalloc(n * sizeof(*ptr));
+ ALLOC_ARRAY(ptr, n);
@@
type T;
T *ptr;
expression n;
@@
- ptr = xmalloc(n * sizeof(T));
+ ALLOC_ARRAY(ptr, n);
......@@ -175,7 +175,7 @@ struct worktree **get_worktrees(unsigned flags)
struct dirent *d;
int counter = 0, alloc = 2;
list = xmalloc(alloc * sizeof(struct worktree *));
ALLOC_ARRAY(list, alloc);
list[counter++] = get_main_worktree();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment