cocci: use ALLOC_ARRAY
authorRené Scharfe <l.s.r@web.de>
Sat, 25 Feb 2017 10:30:03 +0000 (11:30 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Feb 2017 19:02:05 +0000 (11:02 -0800)
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>
contrib/coccinelle/array.cocci
worktree.c
index 2d7f25d99f0754e82e2d0615ebd42b02d8fe5a66..4ba98b7eaff268c6fc9f2f984449fd4d9e1bfd4b 100644 (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);
index eb6121263b0d56b6b7ea7c5024e9ceb68256c1c5..b43160c0031a95426249ab0730096112d0bcb1df 100644 (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();