{"thread":{"id":"45224","subject":"[PATCH] cocci: use ALLOC_ARRAY","startedAt":"2017-02-25T10:30:45Z","lastAt":"2017-02-25T10:30:45Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"312631","messageId":"6652d950-7de3-3b6d-0a2d-414e0e4247f4@web.de","threadId":"45224","inReplyTo":null,"subject":"[PATCH] cocci: use ALLOC_ARRAY","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-02-25T10:30:03Z","receivedAt":"2017-02-25T10:30:45Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add a semantic patch for using ALLOC_ARRAY to allocate arrays and apply\nthe transformation on the current source tree.  The macro checks for\nmultiplication overflow and infers the element size automatically; the\nresult is shorter and safer code.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n contrib/coccinelle/array.cocci | 16 ++++++++++++++++\n worktree.c                     |  2 +-\n 2 files changed, 17 insertions(+), 1 deletion(-)\n\ndiff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci\nindex 2d7f25d99f..4ba98b7eaf 100644\n--- a/contrib/coccinelle/array.cocci\n+++ b/contrib/coccinelle/array.cocci\n@@ -24,3 +24,19 @@ expression n;\n @@\n - memcpy(dst, src, n * sizeof(T));\n + COPY_ARRAY(dst, src, n);\n+\n+@@\n+type T;\n+T *ptr;\n+expression n;\n+@@\n+- ptr = xmalloc(n * sizeof(*ptr));\n++ ALLOC_ARRAY(ptr, n);\n+\n+@@\n+type T;\n+T *ptr;\n+expression n;\n+@@\n+- ptr = xmalloc(n * sizeof(T));\n++ ALLOC_ARRAY(ptr, n);\ndiff --git a/worktree.c b/worktree.c\nindex d633761575..d7b911aac7 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -175,7 +175,7 @@ struct worktree **get_worktrees(unsigned flags)\n \tstruct dirent *d;\n \tint counter = 0, alloc = 2;\n \n-\tlist = xmalloc(alloc * sizeof(struct worktree *));\n+\tALLOC_ARRAY(list, alloc);\n \n \tlist[counter++] = get_main_worktree();\n \n-- \n2.12.0\n\n"}]}