{"thread":{"id":"36020","subject":"[PATCH v2] Replace memcpy with hashcpy when dealing hash copy globally","startedAt":"2014-03-02T09:37:24Z","lastAt":"2014-03-03T07:49:45Z","messageCount":2,"participants":["Sun He","Eric Sunshine"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"235796","messageId":"1393753044-6232-1-git-send-email-sunheehnus@gmail.com","threadId":"36020","inReplyTo":null,"subject":"[PATCH v2] Replace memcpy with hashcpy when dealing hash copy globally","fromName":"Sun He","fromEmail":"sunheehnus@gmail.com","sentAt":"2014-03-02T09:37:24Z","receivedAt":"2014-03-02T09:37:24Z","isPatch":true,"sender":{"key":"sunheehnus@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2889804?v=4"},"body":" Replacing memcpy with hashcpy is more directly and elegant.\n\n Leave ppc/sha1.c alone, as it is an isolated component.\n Pull cache.h(actually ../cache.h) in just for one memcpy\n there is not proper.\n\nHelped-by: Michael Haggerty <mhagger@alum.mit.edu>\nHelped-by: Duy Nguyen <pclouds@gmail.com>\nSigned-off-by: Sun He <sunheehnus@gmail.com>\n---\n\n PATCH v2 leave ppc/sha1.c alone.\n\n The general rule is if cache.h or git-compat-util.h is included,\n it is the first #include, and system includes will be always in\n git-compat-tuil.h.\n\t\t\t\t\tvia Duy Nguyen\n\n The change in PATCH v1 is not proper because I placed cache.h\n in the end.\n And adding it to the head is not a good way to achieve the goal,\n as is said above \"---\".\n\n Thanks to Duy Nguyen.\n\n Find the potential places with memcpy by the bash command:\n  $ find . | xargs grep \"memcpy.*\\(.*20.*\\)\"\n\n\n ppc/sha1.c doesn't include cache.h and it cannot use \n So just leave memcpy(in ppc/sha1.c) alone\n\n bundle.c            | 2 +-\n grep.c              | 2 +-\n pack-bitmap-write.c | 2 +-\n reflog-walk.c       | 4 ++--\n refs.c              | 2 +-\n 5 files changed, 6 insertions(+), 6 deletions(-)\n\ndiff --git a/bundle.c b/bundle.c\nindex e99065c..7809fbb 100644\n--- a/bundle.c\n+++ b/bundle.c\n@@ -19,7 +19,7 @@ static void add_to_ref_list(const unsigned char *sha1, const char *name,\n \t\tlist->list = xrealloc(list->list,\n \t\t\t\tlist->alloc * sizeof(list->list[0]));\n \t}\n-\tmemcpy(list->list[list->nr].sha1, sha1, 20);\n+\thashcpy(list->list[list->nr].sha1, sha1);\n \tlist->list[list->nr].name = xstrdup(name);\n \tlist->nr++;\n }\ndiff --git a/grep.c b/grep.c\nindex c668034..f5101f7 100644\n--- a/grep.c\n+++ b/grep.c\n@@ -1650,7 +1650,7 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,\n \t\tbreak;\n \tcase GREP_SOURCE_SHA1:\n \t\tgs->identifier = xmalloc(20);\n-\t\tmemcpy(gs->identifier, identifier, 20);\n+\t\thashcpy(gs->identifier, identifier);\n \t\tbreak;\n \tcase GREP_SOURCE_BUF:\n \t\tgs->identifier = NULL;\ndiff --git a/pack-bitmap-write.c b/pack-bitmap-write.c\nindex 1218bef..5f1791a 100644\n--- a/pack-bitmap-write.c\n+++ b/pack-bitmap-write.c\n@@ -530,7 +530,7 @@ void bitmap_writer_finish(struct pack_idx_entry **index,\n \theader.version = htons(default_version);\n \theader.options = htons(flags | options);\n \theader.entry_count = htonl(writer.selected_nr);\n-\tmemcpy(header.checksum, writer.pack_checksum, 20);\n+\thashcpy(header.checksum, writer.pack_checksum);\n \n \tsha1write(f, &header, sizeof(header));\n \tdump_bitmap(f, writer.commits);\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex b2fbdb2..d490f7d 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -32,8 +32,8 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,\n \t\t\tsizeof(struct reflog_info));\n \t}\n \titem = array->items + array->nr;\n-\tmemcpy(item->osha1, osha1, 20);\n-\tmemcpy(item->nsha1, nsha1, 20);\n+\thashcpy(item->osha1, osha1);\n+\thashcpy(item->nsha1, nsha1);\n \titem->email = xstrdup(email);\n \titem->timestamp = timestamp;\n \titem->tz = tz;\ndiff --git a/refs.c b/refs.c\nindex 89228e2..f90b7ea 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1222,7 +1222,7 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,\n \tif (ref == NULL)\n \t\treturn -1;\n \n-\tmemcpy(sha1, ref->u.value.sha1, 20);\n+\thashcpy(sha1, ref->u.value.sha1);\n \treturn 0;\n }\n \n-- \n1.9.0.138.g2de3478.dirty\n"},{"id":"235845","messageId":"CAPig+cTiC9-wmeXANFMZO-1MtLQ+u40aQi1jr0JPfK4KS+DS-w@mail.gmail.com","threadId":"36020","inReplyTo":"1393753044-6232-1-git-send-email-sunheehnus@gmail.com","subject":"Re: [PATCH v2] Replace memcpy with hashcpy when dealing hash copy globally","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-03-03T07:49:45Z","receivedAt":"2014-03-03T07:49:45Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Mar 2, 2014 at 4:37 AM, Sun He <sunheehnus@gmail.com> wrote:\n>  Replacing memcpy with hashcpy is more directly and elegant.\n\nA better explanation is that the change takes advantage of the\nabstraction provided by hashcpy() rather than hardcoding knowledge\nabout a particular hash representation.\n\n>  Leave ppc/sha1.c alone, as it is an isolated component.\n>  Pull cache.h(actually ../cache.h) in just for one memcpy\n>  there is not proper.\n\nStrange one-space indentation on all lines of commit message.\n\nOther than that, the patch looks reasonable.\n\n> Helped-by: Michael Haggerty <mhagger@alum.mit.edu>\n> Helped-by: Duy Nguyen <pclouds@gmail.com>\n> Signed-off-by: Sun He <sunheehnus@gmail.com>\n> ---\n>\n>  PATCH v2 leave ppc/sha1.c alone.\n>\n>  The general rule is if cache.h or git-compat-util.h is included,\n>  it is the first #include, and system includes will be always in\n>  git-compat-tuil.h.\n>                                         via Duy Nguyen\n>\n>  The change in PATCH v1 is not proper because I placed cache.h\n>  in the end.\n>  And adding it to the head is not a good way to achieve the goal,\n>  as is said above \"---\".\n>\n>  Thanks to Duy Nguyen.\n>\n>  Find the potential places with memcpy by the bash command:\n>   $ find . | xargs grep \"memcpy.*\\(.*20.*\\)\"\n>\n>\n>  ppc/sha1.c doesn't include cache.h and it cannot use\n>  So just leave memcpy(in ppc/sha1.c) alone\n>\n>  bundle.c            | 2 +-\n>  grep.c              | 2 +-\n>  pack-bitmap-write.c | 2 +-\n>  reflog-walk.c       | 4 ++--\n>  refs.c              | 2 +-\n>  5 files changed, 6 insertions(+), 6 deletions(-)\n>\n> diff --git a/bundle.c b/bundle.c\n> index e99065c..7809fbb 100644\n> --- a/bundle.c\n> +++ b/bundle.c\n> @@ -19,7 +19,7 @@ static void add_to_ref_list(const unsigned char *sha1, const char *name,\n>                 list->list = xrealloc(list->list,\n>                                 list->alloc * sizeof(list->list[0]));\n>         }\n> -       memcpy(list->list[list->nr].sha1, sha1, 20);\n> +       hashcpy(list->list[list->nr].sha1, sha1);\n>         list->list[list->nr].name = xstrdup(name);\n>         list->nr++;\n>  }\n> diff --git a/grep.c b/grep.c\n> index c668034..f5101f7 100644\n> --- a/grep.c\n> +++ b/grep.c\n> @@ -1650,7 +1650,7 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,\n>                 break;\n>         case GREP_SOURCE_SHA1:\n>                 gs->identifier = xmalloc(20);\n> -               memcpy(gs->identifier, identifier, 20);\n> +               hashcpy(gs->identifier, identifier);\n>                 break;\n>         case GREP_SOURCE_BUF:\n>                 gs->identifier = NULL;\n> diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c\n> index 1218bef..5f1791a 100644\n> --- a/pack-bitmap-write.c\n> +++ b/pack-bitmap-write.c\n> @@ -530,7 +530,7 @@ void bitmap_writer_finish(struct pack_idx_entry **index,\n>         header.version = htons(default_version);\n>         header.options = htons(flags | options);\n>         header.entry_count = htonl(writer.selected_nr);\n> -       memcpy(header.checksum, writer.pack_checksum, 20);\n> +       hashcpy(header.checksum, writer.pack_checksum);\n>\n>         sha1write(f, &header, sizeof(header));\n>         dump_bitmap(f, writer.commits);\n> diff --git a/reflog-walk.c b/reflog-walk.c\n> index b2fbdb2..d490f7d 100644\n> --- a/reflog-walk.c\n> +++ b/reflog-walk.c\n> @@ -32,8 +32,8 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,\n>                         sizeof(struct reflog_info));\n>         }\n>         item = array->items + array->nr;\n> -       memcpy(item->osha1, osha1, 20);\n> -       memcpy(item->nsha1, nsha1, 20);\n> +       hashcpy(item->osha1, osha1);\n> +       hashcpy(item->nsha1, nsha1);\n>         item->email = xstrdup(email);\n>         item->timestamp = timestamp;\n>         item->tz = tz;\n> diff --git a/refs.c b/refs.c\n> index 89228e2..f90b7ea 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1222,7 +1222,7 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,\n>         if (ref == NULL)\n>                 return -1;\n>\n> -       memcpy(sha1, ref->u.value.sha1, 20);\n> +       hashcpy(sha1, ref->u.value.sha1);\n>         return 0;\n>  }\n>\n> --\n> 1.9.0.138.g2de3478.dirty\n>\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"}]}