Skip to content

Commit

Permalink
transport_anonymize_url: use xstrfmt
Browse files Browse the repository at this point in the history
This function uses xcalloc and two memcpy calls to
concatenate two strings. We can do this as an xstrfmt
one-liner, and then it is more clear that we are allocating
the correct amount of memory.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Feb 22, 2016
1 parent 7eb45b5 commit 21f9d0f
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ int transport_disconnect(struct transport *transport)
*/
char *transport_anonymize_url(const char *url)
{
char *anon_url, *scheme_prefix, *anon_part;
char *scheme_prefix, *anon_part;
size_t anon_len, prefix_len = 0;

anon_part = strchr(url, '@');
Expand Down Expand Up @@ -1384,10 +1384,8 @@ char *transport_anonymize_url(const char *url)
goto literal_copy;
prefix_len = scheme_prefix - url + 3;
}
anon_url = xcalloc(1, 1 + prefix_len + anon_len);
memcpy(anon_url, url, prefix_len);
memcpy(anon_url + prefix_len, anon_part, anon_len);
return anon_url;
return xstrfmt("%.*s%.*s", (int)prefix_len, url,
(int)anon_len, anon_part);
literal_copy:
return xstrdup(url);
}
Expand Down

0 comments on commit 21f9d0f

Please sign in to comment.