1
0
mirror of https://github.com/git/git.git synced 2024-09-21 09:42:22 +02:00

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>
This commit is contained in:
René Scharfe 2017-02-25 11:30:03 +01:00 committed by Junio C Hamano
parent 3b9e3c2ced
commit 3f64699ffd
2 changed files with 17 additions and 1 deletions

View File

@ -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);

View File

@ -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();