* [PATCH] cocci: use ALLOC_ARRAY
@ 2017-02-25 10:30 René Scharfe
0 siblings, 0 replies; only message in thread
From: René Scharfe @ 2017-02-25 10:30 UTC (permalink / raw)
To: Git List; +Cc: Junio C Hamano
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>
---
contrib/coccinelle/array.cocci | 16 ++++++++++++++++
worktree.c | 2 +-
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci
index 2d7f25d99f..4ba98b7eaf 100644
--- a/contrib/coccinelle/array.cocci
+++ b/contrib/coccinelle/array.cocci
@@ -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);
diff --git a/worktree.c b/worktree.c
index d633761575..d7b911aac7 100644
--- a/worktree.c
+++ b/worktree.c
@@ -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();
--
2.12.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2017-02-25 10:30 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-25 10:30 [PATCH] cocci: use ALLOC_ARRAY René Scharfe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox