From 2ae7f90f263760abd242501471f123632395d7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 21 Feb 2015 20:49:58 +0100 Subject: [PATCH 1/4] connect: use strcmp() for string comparison Get rid of magic string length constants and simply compare the strings using strcmp(). This makes the intent of the code a bit clearer. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- connect.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/connect.c b/connect.c index 5047402a1..5bff3ed3f 100644 --- a/connect.c +++ b/connect.c @@ -161,8 +161,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, server_capabilities = xstrdup(name + name_len + 1); } - if (extra_have && - name_len == 5 && !memcmp(".have", name, 5)) { + if (extra_have && !strcmp(name, ".have")) { sha1_array_append(extra_have, old_sha1); continue; } From 008d5d005d862d9b0309af94dfc266e3b06bdf6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 21 Feb 2015 20:51:28 +0100 Subject: [PATCH 2/4] for-each-ref: use skip_prefix() to avoid duplicate string comparison Use skip_prefix() to get the part after "color:" (if present) and only compare it with "reset" instead of comparing the whole string again. This gets rid of the duplicate "color:" part of the string constant. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin/for-each-ref.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 47bd62469..9463dca19 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -180,11 +180,10 @@ static const char *find_next(const char *cp) static int verify_format(const char *format) { const char *cp, *sp; - static const char color_reset[] = "color:reset"; need_color_reset_at_eol = 0; for (cp = format; *cp && (sp = find_next(cp)); ) { - const char *ep = strchr(sp, ')'); + const char *color, *ep = strchr(sp, ')'); int at; if (!ep) @@ -193,8 +192,8 @@ static int verify_format(const char *format) at = parse_atom(sp + 2, ep); cp = ep + 1; - if (starts_with(used_atom[at], "color:")) - need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset); + if (skip_prefix(used_atom[at], "color:", &color)) + need_color_reset_at_eol = !!strcmp(color, "reset"); } return 0; } From 68d6d6eb402d7e39c89ce43ca37e6c16b941890c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 21 Feb 2015 20:53:09 +0100 Subject: [PATCH 3/4] pretty: use starts_with() to check for a prefix Simplify the code and avoid duplication by using starts_with() instead of strlen() and strncmp() to check if a line starts with "encoding ". Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- pretty.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pretty.c b/pretty.c index 31fc76b2f..af0f57ac8 100644 --- a/pretty.c +++ b/pretty.c @@ -586,7 +586,7 @@ static char *replace_encoding_header(char *buf, const char *encoding) char *cp = buf; /* guess if there is an encoding header before a \n\n */ - while (strncmp(cp, "encoding ", strlen("encoding "))) { + while (!starts_with(cp, "encoding ")) { cp = strchr(cp, '\n'); if (!cp || *++cp == '\n') return buf; From 2ce63e9fac242a70cd6d9e1325063bbb2e5091f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 21 Feb 2015 20:55:22 +0100 Subject: [PATCH 4/4] sha1_name: use strlcpy() to copy strings Use strlcpy() instead of calling strncpy() and then setting the last byte of the target buffer to NUL explicitly. This shortens and simplifies the code a bit. Signed-of-by: Rene Scharfe Signed-off-by: Junio C Hamano --- sha1_name.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/sha1_name.c b/sha1_name.c index 63ee66fed..9ef426b32 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -1384,9 +1384,7 @@ static int get_sha1_with_context_1(const char *name, namelen = strlen(cp); } - strncpy(oc->path, cp, - sizeof(oc->path)); - oc->path[sizeof(oc->path)-1] = '\0'; + strlcpy(oc->path, cp, sizeof(oc->path)); if (!active_cache) read_cache(); @@ -1436,9 +1434,7 @@ static int get_sha1_with_context_1(const char *name, name, len); } hashcpy(oc->tree, tree_sha1); - strncpy(oc->path, filename, - sizeof(oc->path)); - oc->path[sizeof(oc->path)-1] = '\0'; + strlcpy(oc->path, filename, sizeof(oc->path)); free(new_filename); return ret;