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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
30a0ddb705
commit
fa3f60b783
6
remote.c
6
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user