All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Use starts_with() for C strings instead of memcmp()
@ 2014-06-06 17:24 René Scharfe
  0 siblings, 0 replies; only message in thread
From: René Scharfe @ 2014-06-06 17:24 UTC (permalink / raw)
  To: Git Mailing List
  Cc: Nguyễn Thái Ngọc Duy, Junio C Hamano, Jeff King,
	Ramkumar Ramachandra

Convert three cases of checking for a constant prefix using memcmp() to
starts_with().  This way there is no need for magic string length
constants and we avoid running over the end of the string should it be
shorter than the prefix.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
These are the easy cases I found; there are several more comparisons
of strings to constants using memcmp().  Some could benefit from
skip_prefix(), others may need a bit more thought, and perhaps I
missed a few.

 builtin/for-each-ref.c | 2 +-
 fetch-pack.c           | 2 +-
 remote.c               | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 3e1d5c3..4135980 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -193,7 +193,7 @@ static int verify_format(const char *format)
 		at = parse_atom(sp + 2, ep);
 		cp = ep + 1;
 
-		if (!memcmp(used_atom[at], "color:", 6))
+		if (starts_with(used_atom[at], "color:"))
 			need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset);
 	}
 	return 0;
diff --git a/fetch-pack.c b/fetch-pack.c
index eeee2bb..b12bd4c 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -507,7 +507,7 @@ static void filter_refs(struct fetch_pack_args *args,
 		int keep = 0;
 		next = ref->next;
 
-		if (!memcmp(ref->name, "refs/", 5) &&
+		if (starts_with(ref->name, "refs/") &&
 		    check_refname_format(ref->name, 0))
 			; /* trash */
 		else {
diff --git a/remote.c b/remote.c
index eea2c8d..0f6ef36 100644
--- a/remote.c
+++ b/remote.c
@@ -1194,7 +1194,7 @@ static int match_explicit(struct ref *src, struct ref *dst,
 	case 1:
 		break;
 	case 0:
-		if (!memcmp(dst_value, "refs/", 5))
+		if (starts_with(dst_value, "refs/"))
 			matched_dst = make_linked_ref(dst_value, dst_tail);
 		else if (is_null_sha1(matched_src->new_sha1))
 			error("unable to delete '%s': remote ref does not exist",
-- 
2.0.0

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2014-06-06 17:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-06 17:24 [PATCH] Use starts_with() for C strings instead of memcmp() René Scharfe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.