{"thread":{"id":"36859","subject":"[PATCH] Use starts_with() for C strings instead of memcmp()","startedAt":"2014-06-06T17:24:48Z","lastAt":"2014-06-06T17:24:48Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"243505","messageId":"5391F960.8070702@web.de","threadId":"36859","inReplyTo":null,"subject":"[PATCH] Use starts_with() for C strings instead of memcmp()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2014-06-06T17:24:48Z","receivedAt":"2014-06-06T17:24:48Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Convert three cases of checking for a constant prefix using memcmp() to\nstarts_with().  This way there is no need for magic string length\nconstants and we avoid running over the end of the string should it be\nshorter than the prefix.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\nThese are the easy cases I found; there are several more comparisons\nof strings to constants using memcmp().  Some could benefit from\nskip_prefix(), others may need a bit more thought, and perhaps I\nmissed a few.\n\n builtin/for-each-ref.c | 2 +-\n fetch-pack.c           | 2 +-\n remote.c               | 2 +-\n 3 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 3e1d5c3..4135980 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -193,7 +193,7 @@ static int verify_format(const char *format)\n \t\tat = parse_atom(sp + 2, ep);\n \t\tcp = ep + 1;\n \n-\t\tif (!memcmp(used_atom[at], \"color:\", 6))\n+\t\tif (starts_with(used_atom[at], \"color:\"))\n \t\t\tneed_color_reset_at_eol = !!strcmp(used_atom[at], color_reset);\n \t}\n \treturn 0;\ndiff --git a/fetch-pack.c b/fetch-pack.c\nindex eeee2bb..b12bd4c 100644\n--- a/fetch-pack.c\n+++ b/fetch-pack.c\n@@ -507,7 +507,7 @@ static void filter_refs(struct fetch_pack_args *args,\n \t\tint keep = 0;\n \t\tnext = ref->next;\n \n-\t\tif (!memcmp(ref->name, \"refs/\", 5) &&\n+\t\tif (starts_with(ref->name, \"refs/\") &&\n \t\t    check_refname_format(ref->name, 0))\n \t\t\t; /* trash */\n \t\telse {\ndiff --git a/remote.c b/remote.c\nindex eea2c8d..0f6ef36 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1194,7 +1194,7 @@ static int match_explicit(struct ref *src, struct ref *dst,\n \tcase 1:\n \t\tbreak;\n \tcase 0:\n-\t\tif (!memcmp(dst_value, \"refs/\", 5))\n+\t\tif (starts_with(dst_value, \"refs/\"))\n \t\t\tmatched_dst = make_linked_ref(dst_value, dst_tail);\n \t\telse if (is_null_sha1(matched_src->new_sha1))\n \t\t\terror(\"unable to delete '%s': remote ref does not exist\",\n-- \n2.0.0\n"}]}