http-walker: simplify process_alternates_response() using strbuf

Use strbuf to build the new base, which takes care of allocations and
the terminating NUL character automatically.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe
2014-08-30 17:55:45 +02:00
committed by Junio C Hamano
parent 96db324a73
commit 59b8263a6d

View File

@ -230,7 +230,6 @@ static void process_alternates_response(void *callback_data)
int okay = 0; int okay = 0;
int serverlen = 0; int serverlen = 0;
struct alt_base *newalt; struct alt_base *newalt;
char *target = NULL;
if (data[i] == '/') { if (data[i] == '/') {
/* /*
* This counts * This counts
@ -287,17 +286,15 @@ static void process_alternates_response(void *callback_data)
} }
/* skip "objects\n" at end */ /* skip "objects\n" at end */
if (okay) { if (okay) {
target = xmalloc(serverlen + posn - i - 6); struct strbuf target = STRBUF_INIT;
memcpy(target, base, serverlen); strbuf_add(&target, base, serverlen);
memcpy(target + serverlen, data + i, strbuf_add(&target, data + i, posn - i - 7);
posn - i - 7);
target[serverlen + posn - i - 7] = 0;
if (walker->get_verbosely) if (walker->get_verbosely)
fprintf(stderr, fprintf(stderr, "Also look at %s\n",
"Also look at %s\n", target); target.buf);
newalt = xmalloc(sizeof(*newalt)); newalt = xmalloc(sizeof(*newalt));
newalt->next = NULL; newalt->next = NULL;
newalt->base = target; newalt->base = strbuf_detach(&target, NULL);
newalt->got_indices = 0; newalt->got_indices = 0;
newalt->packs = NULL; newalt->packs = NULL;