{"thread":{"id":"51323","subject":"[PATCH 1/2] coccinelle: use COPY_ARRAY for copying arrays","startedAt":"2019-06-15T18:33:21Z","lastAt":"2019-06-17T01:14:57Z","messageCount":3,"participants":["René Scharfe","Derrick Stolee"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"377304","messageId":"a2633676-5350-e85d-2bea-c2c0f7a33bbb@web.de","threadId":"51323","inReplyTo":null,"subject":"[PATCH 1/2] coccinelle: use COPY_ARRAY for copying arrays","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2019-06-15T18:32:58Z","receivedAt":"2019-06-15T18:33:21Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"The current semantic patch for COPY_ARRAY transforms memcpy(3) calls on\npointers, but Coccinelle distinguishes them from arrays.  It already\ncontains three rules to handle the options for sizeof (i.e. source,\ndestination and type), and handling arrays as source and destination\nwould require four times as many rules if we enumerated all cases.\n\nWe also don't handle array subscripts, and supporting that would\nincrease the number of rules by another factor of four.  (An isomorphism\ntelling Coccinelle that \"sizeof x[...]\" is equivalent to \"sizeof *x\"\nwould be nice..)\n\nSupport arrays and array subscripts, but keep the number of rules down\nby adding normalization steps: First turn array subscripts into\nderefences, then determine the types of expressions used with sizeof and\nreplace them with these types, and then convert the different possible\ncombinations of arrays and pointers with memcpy(3) to COPY_ARRAY.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n contrib/coccinelle/array.cocci | 61 +++++++++++++++++++++++++---------\n 1 file changed, 46 insertions(+), 15 deletions(-)\n\ndiff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci\nindex 01586821dc..46b8d2ee11 100644\n--- a/contrib/coccinelle/array.cocci\n+++ b/contrib/coccinelle/array.cocci\n@@ -1,29 +1,60 @@\n @@\n-type T;\n-T *dst;\n-T *src;\n-expression n;\n+expression dst, src, n, E;\n @@\n-- memcpy(dst, src, (n) * sizeof(*dst));\n-+ COPY_ARRAY(dst, src, n);\n+  memcpy(dst, src, n * sizeof(\n+- E[...]\n++ *(E)\n+  ))\n\n @@\n type T;\n-T *dst;\n-T *src;\n-expression n;\n+T *ptr;\n+T[] arr;\n+expression E, n;\n @@\n-- memcpy(dst, src, (n) * sizeof(*src));\n-+ COPY_ARRAY(dst, src, n);\n+(\n+  memcpy(ptr, E,\n+- n * sizeof(*(ptr))\n++ n * sizeof(T)\n+  )\n+|\n+  memcpy(arr, E,\n+- n * sizeof(*(arr))\n++ n * sizeof(T)\n+  )\n+|\n+  memcpy(E, ptr,\n+- n * sizeof(*(ptr))\n++ n * sizeof(T)\n+  )\n+|\n+  memcpy(E, arr,\n+- n * sizeof(*(arr))\n++ n * sizeof(T)\n+  )\n+)\n\n @@\n type T;\n-T *dst;\n-T *src;\n+T *dst_ptr;\n+T *src_ptr;\n+T[] dst_arr;\n+T[] src_arr;\n expression n;\n @@\n-- memcpy(dst, src, (n) * sizeof(T));\n-+ COPY_ARRAY(dst, src, n);\n+(\n+- memcpy(dst_ptr, src_ptr, (n) * sizeof(T))\n++ COPY_ARRAY(dst_ptr, src_ptr, n)\n+|\n+- memcpy(dst_ptr, src_arr, (n) * sizeof(T))\n++ COPY_ARRAY(dst_ptr, src_arr, n)\n+|\n+- memcpy(dst_arr, src_ptr, (n) * sizeof(T))\n++ COPY_ARRAY(dst_arr, src_ptr, n)\n+|\n+- memcpy(dst_arr, src_arr, (n) * sizeof(T))\n++ COPY_ARRAY(dst_arr, src_arr, n)\n+)\n\n @@\n type T;\n--\n2.22.0\n"},{"id":"377305","messageId":"e09e49a6-8f6d-1f26-005a-bec5f99414dd@web.de","threadId":"51323","inReplyTo":"a2633676-5350-e85d-2bea-c2c0f7a33bbb@web.de","subject":"[PATCH 2/2] use COPY_ARRAY for copying arrays","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2019-06-15T18:36:35Z","receivedAt":"2019-06-15T18:36:43Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Convert calls of memcpy(3) to use COPY_ARRAY, which shortens and\nsimplifies the code a bit.\n\nPatch generated by Coccinelle and contrib/coccinelle/array.cocci.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n fast-import.c | 2 +-\n kwset.c       | 2 +-\n packfile.c    | 6 +++---\n pretty.c      | 4 ++--\n 4 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/fast-import.c b/fast-import.c\nindex 76a7bd3699..6dfdd6801c 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -644,7 +644,7 @@ static struct tree_content *grow_tree_content(\n \tstruct tree_content *r = new_tree_content(t->entry_count + amt);\n \tr->entry_count = t->entry_count;\n \tr->delta_depth = t->delta_depth;\n-\tmemcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));\n+\tCOPY_ARRAY(r->entries, t->entries, t->entry_count);\n \trelease_tree_content(t);\n \treturn r;\n }\ndiff --git a/kwset.c b/kwset.c\nindex 4fb6455aca..090ffcafa2 100644\n--- a/kwset.c\n+++ b/kwset.c\n@@ -475,7 +475,7 @@ kwsprep (kwset_t kws)\n \tfor (i = 0; i < NCHAR; ++i)\n \t  kwset->next[i] = next[U(trans[i])];\n       else\n-\tmemcpy(kwset->next, next, NCHAR * sizeof(struct trie *));\n+\tCOPY_ARRAY(kwset->next, next, NCHAR);\n     }\n\n   /* Fix things up for any translation table. */\ndiff --git a/packfile.c b/packfile.c\nindex d786ec7312..d55cb7f013 100644\n--- a/packfile.c\n+++ b/packfile.c\n@@ -1269,7 +1269,7 @@ static enum object_type packed_to_object_type(struct repository *r,\n \t\tif (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {\n \t\t\tpoi_stack_alloc = alloc_nr(poi_stack_nr);\n \t\t\tALLOC_ARRAY(poi_stack, poi_stack_alloc);\n-\t\t\tmemcpy(poi_stack, small_poi_stack, sizeof(off_t)*poi_stack_nr);\n+\t\t\tCOPY_ARRAY(poi_stack, small_poi_stack, poi_stack_nr);\n \t\t} else {\n \t\t\tALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);\n \t\t}\n@@ -1679,8 +1679,8 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,\n \t\t    && delta_stack == small_delta_stack) {\n \t\t\tdelta_stack_alloc = alloc_nr(delta_stack_nr);\n \t\t\tALLOC_ARRAY(delta_stack, delta_stack_alloc);\n-\t\t\tmemcpy(delta_stack, small_delta_stack,\n-\t\t\t       sizeof(*delta_stack)*delta_stack_nr);\n+\t\t\tCOPY_ARRAY(delta_stack, small_delta_stack,\n+\t\t\t\t   delta_stack_nr);\n \t\t} else {\n \t\t\tALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);\n \t\t}\ndiff --git a/pretty.c b/pretty.c\nindex ced0485257..e4ed14effe 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -106,8 +106,8 @@ static void setup_commit_formats(void)\n \tcommit_formats_len = ARRAY_SIZE(builtin_formats);\n \tbuiltin_formats_len = commit_formats_len;\n \tALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);\n-\tmemcpy(commit_formats, builtin_formats,\n-\t       sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));\n+\tCOPY_ARRAY(commit_formats, builtin_formats,\n+\t\t   ARRAY_SIZE(builtin_formats));\n\n \tgit_config(git_pretty_formats_config, NULL);\n }\n--\n2.22.0\n"},{"id":"377340","messageId":"d51041e1-c931-146f-51e7-8a51655dcaa0@gmail.com","threadId":"51323","inReplyTo":"e09e49a6-8f6d-1f26-005a-bec5f99414dd@web.de","subject":"Re: [PATCH 2/2] use COPY_ARRAY for copying arrays","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2019-06-17T01:14:06Z","receivedAt":"2019-06-17T01:14:57Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 6/15/2019 2:36 PM, René Scharfe wrote:\n> Convert calls of memcpy(3) to use COPY_ARRAY, which shortens and\n> simplifies the code a bit.\n\nThese changes do look simpler. Thanks!\n\n> Patch generated by Coccinelle and contrib/coccinelle/array.cocci.\n\nAnd this auto-generation is particularly useful!\n\n> \n> Signed-off-by: Rene Scharfe <l.s.r@web.de>\n> ---\n>  fast-import.c | 2 +-\n>  kwset.c       | 2 +-\n>  packfile.c    | 6 +++---\n>  pretty.c      | 4 ++--\n>  4 files changed, 7 insertions(+), 7 deletions(-)\n> \n> diff --git a/kwset.c b/kwset.c\n> index 4fb6455aca..090ffcafa2 100644\n> --- a/kwset.c\n> +++ b/kwset.c\n> @@ -475,7 +475,7 @@ kwsprep (kwset_t kws)\n>  \tfor (i = 0; i < NCHAR; ++i)\n>  \t  kwset->next[i] = next[U(trans[i])];\n>        else\n> -\tmemcpy(kwset->next, next, NCHAR * sizeof(struct trie *));\n> +\tCOPY_ARRAY(kwset->next, next, NCHAR);\n\nI was unfamiliar with kwset.c, so I took a look and noticed that\nit was adapted from GNU grep, so this change takes us even farther\nfrom the original source (and closer to Git source conventions).\n\nI think this is the right thing to do, but thought I'd point it\nout in case anyone thought differently.\n\nBoth patches LGTM.\n\nThanks,\n-Stolee\n"}]}