Skip to content

Commit 999b951

Browse files
peffgitster
authored andcommitted
progress: use xmalloc/xcalloc
Since the early days of Git, the progress code allocates its struct with a bare malloc(), not xmalloc(). If the allocation fails, we just avoid showing progress at all. While perhaps a noble goal not to fail the whole operation because of optional progress, in practice: 1. Any failure to allocate a few dozen bytes here means critical path allocations are likely to fail, too. 2. These days we use a strbuf for throughput progress (and there's a patch under discussion to do the same for non-throughput cases, too). And that uses xmalloc() under the hood, which means we'd still die on some allocation failures. Let's switch to xmalloc(). That makes us consistent with the rest of Git and makes it easier to audit for other (less careful) bare mallocs. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 36c8319 commit 999b951

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

progress.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ void display_throughput(struct progress *progress, uint64_t total)
139139
now_ns = getnanotime();
140140

141141
if (!tp) {
142-
progress->throughput = tp = calloc(1, sizeof(*tp));
143-
if (tp) {
144-
tp->prev_total = tp->curr_total = total;
145-
tp->prev_ns = now_ns;
146-
strbuf_init(&tp->display, 0);
147-
}
142+
progress->throughput = tp = xcalloc(1, sizeof(*tp));
143+
tp->prev_total = tp->curr_total = total;
144+
tp->prev_ns = now_ns;
145+
strbuf_init(&tp->display, 0);
148146
return;
149147
}
150148
tp->curr_total = total;
@@ -196,13 +194,7 @@ int display_progress(struct progress *progress, uint64_t n)
196194
static struct progress *start_progress_delay(const char *title, uint64_t total,
197195
unsigned delay)
198196
{
199-
struct progress *progress = malloc(sizeof(*progress));
200-
if (!progress) {
201-
/* unlikely, but here's a good fallback */
202-
fprintf(stderr, "%s...\n", title);
203-
fflush(stderr);
204-
return NULL;
205-
}
197+
struct progress *progress = xmalloc(sizeof(*progress));
206198
progress->title = title;
207199
progress->total = total;
208200
progress->last_value = -1;

0 commit comments

Comments
 (0)