From: Pierre Habouzit Date: Tue, 18 Sep 2007 21:48:22 GMT Subject: [PATCH 5/5] Avoid duplicating memory, and use xmemdupz instead of xstrdup. Message-ID: <20070918224123.3251C344AB3@madism.org> In-Reply-To: <20070918223947.GB4535@artemis.corp> Signed-off-by: Pierre Habouzit --- fetch.c | 23 +++++++++-------------- 1 files changed, 9 insertions(+), 14 deletions(-) diff --git a/fetch.c b/fetch.c index b1c1f07..479ec02 100644 --- a/fetch.c +++ b/fetch.c @@ -218,24 +218,19 @@ int pull_targets_stdin(char ***target, const char ***write_ref) struct strbuf buf; *target = NULL; *write_ref = NULL; strbuf_init(&buf, 0); - while (1) { - char *rf_one = NULL; - char *tg_one; - - if (strbuf_getline(&buf, stdin, '\n') == EOF) - break; - tg_one = buf.buf; - rf_one = strchr(tg_one, '\t'); - if (rf_one) - *rf_one++ = 0; + while (strbuf_getline(&buf, stdin, '\n') != EOF) { + char *rf_one = memchr(buf.buf, '\t', buf.len); if (targets >= targets_alloc) { - targets_alloc = targets_alloc ? targets_alloc * 2 : 64; - *target = xrealloc(*target, targets_alloc * sizeof(**target)); + ALLOC_GROW(target, targets, targets_alloc); *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref)); } - (*target)[targets] = xstrdup(tg_one); - (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL; + if (rf_one) { + (*write_ref)[targets] = xmemdupz(rf_one, buf.len - (rf_one - buf.buf)); + } else { + (*write_ref)[targets] = NULL; + } + (*target)[targets] = strbuf_detach(&buf); targets++; } strbuf_release(&buf); -- 1.5.3.1