From fa3f60b783b42e0d07c667a8f582c3df12791cec Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 18 Jun 2014 16:02:13 -0400 Subject: use xstrfmt in favor of manual size calculations In many parts of the code, we do an ugly and error-prone malloc like: const char *fmt = "something %s"; buf = xmalloc(strlen(foo) + 10 + 1); sprintf(buf, fmt, foo); This makes the code brittle, and if we ever get the allocation wrong, is a potential heap overflow. Let's instead favor xstrfmt, which handles the allocation automatically, and makes the code shorter and more readable. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- remote.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'remote.c') diff --git a/remote.c b/remote.c index 0e9459cc06..bf27e44762 100644 --- a/remote.c +++ b/remote.c @@ -170,7 +170,6 @@ static struct branch *make_branch(const char *name, int len) { struct branch *ret; int i; - char *refname; for (i = 0; i < branches_nr; i++) { if (len ? (!strncmp(name, branches[i]->name, len) && @@ -186,10 +185,7 @@ static struct branch *make_branch(const char *name, int len) ret->name = xstrndup(name, len); else ret->name = xstrdup(name); - refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1); - strcpy(refname, "refs/heads/"); - strcpy(refname + strlen("refs/heads/"), ret->name); - ret->refname = refname; + ret->refname = xstrfmt("refs/heads/%s", ret->name); return ret; } -- cgit v1.2.1