Skip to content

Commit 449f2db

Browse files
committed
Merge branch 'jk/xmalloc'
The code is updated to check the result of memory allocation before it is used in more places, by using xmalloc and/or xcalloc calls. * jk/xmalloc: progress: use xmalloc/xcalloc xdiff: use xmalloc/xrealloc xdiff: use git-compat-util test-prio-queue: use xmalloc
2 parents c8e8b5c + 999b951 commit 449f2db

File tree

4 files changed

+9
-23
lines changed

4 files changed

+9
-23
lines changed

progress.c

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

169169
if (!tp) {
170-
progress->throughput = tp = calloc(1, sizeof(*tp));
171-
if (tp) {
172-
tp->prev_total = tp->curr_total = total;
173-
tp->prev_ns = now_ns;
174-
strbuf_init(&tp->display, 0);
175-
}
170+
progress->throughput = tp = xcalloc(1, sizeof(*tp));
171+
tp->prev_total = tp->curr_total = total;
172+
tp->prev_ns = now_ns;
173+
strbuf_init(&tp->display, 0);
176174
return;
177175
}
178176
tp->curr_total = total;
@@ -225,13 +223,7 @@ void display_progress(struct progress *progress, uint64_t n)
225223
static struct progress *start_progress_delay(const char *title, uint64_t total,
226224
unsigned delay, unsigned sparse)
227225
{
228-
struct progress *progress = malloc(sizeof(*progress));
229-
if (!progress) {
230-
/* unlikely, but here's a good fallback */
231-
fprintf(stderr, "%s...\n", title);
232-
fflush(stderr);
233-
return NULL;
234-
}
226+
struct progress *progress = xmalloc(sizeof(*progress));
235227
progress->title = title;
236228
progress->total = total;
237229
progress->last_value = -1;

t/helper/test-prio-queue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int cmd__prio_queue(int argc, const char **argv)
4040
} else if (!strcmp(*argv, "stack")) {
4141
pq.compare = NULL;
4242
} else {
43-
int *v = malloc(sizeof(*v));
43+
int *v = xmalloc(sizeof(*v));
4444
*v = atoi(*argv);
4545
prio_queue_put(&pq, v);
4646
}

xdiff/xdiff.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ typedef struct s_bdiffparam {
113113
} bdiffparam_t;
114114

115115

116-
#define xdl_malloc(x) malloc(x)
116+
#define xdl_malloc(x) xmalloc(x)
117117
#define xdl_free(ptr) free(ptr)
118-
#define xdl_realloc(ptr,x) realloc(ptr,x)
118+
#define xdl_realloc(ptr,x) xrealloc(ptr,x)
119119

120120
void *xdl_mmfile_first(mmfile_t *mmf, long *size);
121121
long xdl_mmfile_size(mmfile_t *mmf);

xdiff/xinclude.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@
2323
#if !defined(XINCLUDE_H)
2424
#define XINCLUDE_H
2525

26-
#include <ctype.h>
27-
#include <stdio.h>
28-
#include <stdlib.h>
29-
#include <unistd.h>
30-
#include <string.h>
31-
#include <limits.h>
32-
26+
#include "git-compat-util.h"
3327
#include "xmacros.h"
3428
#include "xdiff.h"
3529
#include "xtypes.h"

0 commit comments

Comments
 (0)