Commit 322260e1783c1fb6a443258366c79d7546e4fc25

Martin Pieuchot 2018-01-26T13:58:40

Get rid of xmalloc.{c,h}. malloc(3) & pread(3) errors will now be reported as GOT_ERR_NO_MEM. This does not improve the numbers of leaks in diffreg.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
diff --git a/lib/diffreg.c b/lib/diffreg.c
index c39cd8c..c58a75c 100644
--- a/lib/diffreg.c
+++ b/lib/diffreg.c
@@ -89,7 +89,6 @@
 #include "got_diff.h"
 
 #include "diff.h"
-#include "xmalloc.h" /* XXX should return GOT_ERR_NO_MEM instead of exiting */
 
 #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
 #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
@@ -185,24 +184,24 @@ struct context_vec {
 };
 
 static void	 diff_output(FILE *, const char *, ...);
-static void	 output(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int);
+static int	 output(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int);
 static void	 check(struct got_diff_state *, FILE *, FILE *, int);
 static void	 range(FILE *, int, int, char *);
 static void	 uni_range(FILE *, int, int);
 static void	 dump_context_vec(FILE *, struct got_diff_state *, struct got_diff_args *, FILE *, FILE *, int);
 static void	 dump_unified_vec(FILE *, struct got_diff_state *, struct got_diff_args *, FILE *, FILE *, int);
-static void	 prepare(struct got_diff_state *, int, FILE *, off_t, int);
+static int	 prepare(struct got_diff_state *, int, FILE *, off_t, int);
 static void	 prune(struct got_diff_state *);
 static void	 equiv(struct line *, int, struct line *, int, int *);
 static void	 unravel(struct got_diff_state *, int);
-static void	 unsort(struct line *, int, int *);
-static void	 change(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int, int, int, int, int *);
+static int	 unsort(struct line *, int, int *);
+static int	 change(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int, int, int, int, int *);
 static void	 sort(struct line *, int);
 static void	 print_header(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, const char *);
 static int	 ignoreline(char *);
 static int	 asciifile(FILE *);
 static int	 fetch(FILE *, struct got_diff_state *, struct got_diff_args *, long *, int, int, FILE *, int, int, int);
-static int	 newcand(struct got_diff_state *, int, int, int);
+static int	 newcand(struct got_diff_state *, int, int, int, int *);
 static int	 search(struct got_diff_state *, int *, int, int);
 static int	 skipline(FILE *);
 static int	 isqrt(int);
@@ -331,8 +330,14 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 		args->status |= 1;
 		goto closem;
 	}
-	prepare(ds, 0, f1, ds->stb1.st_size, flags);
-	prepare(ds, 1, f2, ds->stb2.st_size, flags);
+	if (prepare(ds, 0, f1, ds->stb1.st_size, flags)) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto closem;
+	}
+	if (prepare(ds, 1, f2, ds->stb2.st_size, flags)) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto closem;
+	}
 
 	prune(ds);
 	sort(ds->sfile[0], ds->slen[0]);
@@ -347,7 +352,10 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 	}
 
 	ds->class = (int *)ds->file[0];
-	unsort(ds->sfile[0], ds->slen[0], ds->class);
+	if (unsort(ds->sfile[0], ds->slen[0], ds->class)) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto closem;
+	}
 	ds->class = reallocarray(ds->class, ds->slen[0] + 2, sizeof(*ds->class));
 	if (ds->class == NULL) {
 		err = got_error(GOT_ERR_NO_MEM);
@@ -369,6 +377,10 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 	i = stone(ds, ds->class, ds->slen[0], ds->member, ds->klist, flags);
 	free(ds->member);
 	free(ds->class);
+	if (i < 0) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto closem;
+	}
 
 	ds->J = reallocarray(ds->J, ds->len[0] + 2, sizeof(*ds->J));
 	if (ds->J == NULL) {
@@ -392,7 +404,9 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 		goto closem;
 	}
 	check(ds, f1, f2, flags);
-	output(outfile, ds, args, args->label[0], f1, args->label[1], f2, flags);
+	if (output(outfile, ds, args, args->label[0], f1, args->label[1], f2,
+	    flags))
+		err = got_error(GOT_ERR_NO_MEM);
 closem:
 	if (ds->anychange) {
 		args->status |= 1;
@@ -435,24 +449,7 @@ files_differ(struct got_diff_state *ds, FILE *f1, FILE *f2, int flags)
 	}
 }
 
-char *
-splice(char *dir, char *file)
-{
-	char *tail, *buf;
-	size_t dirlen;
-
-	dirlen = strlen(dir);
-	while (dirlen != 0 && dir[dirlen - 1] == '/')
-	    dirlen--;
-	if ((tail = strrchr(file, '/')) == NULL)
-		tail = file;
-	else
-		tail++;
-	xasprintf(&buf, "%.*s/%s", (int)dirlen, dir, tail);
-	return (buf);
-}
-
-static void
+static int
 prepare(struct got_diff_state *ds, int i, FILE *fd, off_t filesize, int flags)
 {
 	struct line *p;
@@ -465,16 +462,22 @@ prepare(struct got_diff_state *ds, int i, FILE *fd, off_t filesize, int flags)
 	if (sz < 100)
 		sz = 100;
 
-	p = xcalloc(sz + 3, sizeof(*p));
+	p = calloc(sz + 3, sizeof(*p));
+	if (p == NULL)
+		return (-1);
 	for (j = 0; (h = readhash(ds, fd, flags));) {
 		if (j == sz) {
 			sz = sz * 3 / 2;
-			p = xreallocarray(p, sz + 3, sizeof(*p));
+			p = reallocarray(p, sz + 3, sizeof(*p));
+			if (p == NULL)
+				return (-1);
 		}
 		p[++j].value = h;
 	}
 	ds->len[i] = j;
 	ds->file[i] = p;
+
+	return (0);
 }
 
 static void
@@ -551,6 +554,7 @@ stone(struct got_diff_state *ds, int *a, int n, int *b, int *c, int flags)
 	int i, k, y, j, l;
 	int oldc, tc, oldl, sq;
 	u_int numtries, bound;
+	int error;
 
 	if (flags & D_MINIMAL)
 		bound = UINT_MAX;
@@ -560,7 +564,9 @@ stone(struct got_diff_state *ds, int *a, int n, int *b, int *c, int flags)
 	}
 
 	k = 0;
-	c[0] = newcand(ds, 0, 0, 0);
+	c[0] = newcand(ds, 0, 0, 0, &error);
+	if (error)
+		return -1;
 	for (i = 1; i <= n; i++) {
 		j = a[i];
 		if (j == 0)
@@ -579,12 +585,16 @@ stone(struct got_diff_state *ds, int *a, int n, int *b, int *c, int flags)
 				if (ds->clist[c[l]].y <= y)
 					continue;
 				tc = c[l];
-				c[l] = newcand(ds, i, y, oldc);
+				c[l] = newcand(ds, i, y, oldc, &error);
+				if (error)
+					return -1;
 				oldc = tc;
 				oldl = l;
 				numtries++;
 			} else {
-				c[l] = newcand(ds, i, y, oldc);
+				c[l] = newcand(ds, i, y, oldc, &error);
+				if (error)
+					return -1;
 				k++;
 				break;
 			}
@@ -594,18 +604,24 @@ stone(struct got_diff_state *ds, int *a, int n, int *b, int *c, int flags)
 }
 
 static int
-newcand(struct got_diff_state *ds, int x, int y, int pred)
+newcand(struct got_diff_state *ds, int x, int y, int pred, int *errorp)
 {
 	struct cand *q;
 
 	if (ds->clen == ds->clistlen) {
 		ds->clistlen = ds->clistlen * 11 / 10;
-		ds->clist = xreallocarray(ds->clist, ds->clistlen, sizeof(*ds->clist));
+		ds->clist = reallocarray(ds->clist, ds->clistlen,
+		    sizeof(*ds->clist));
+		if (ds->clist == NULL) {
+			*errorp = -1;
+			return 0;
+		}
 	}
 	q = ds->clist + ds->clen;
 	q->x = x;
 	q->y = y;
 	q->pred = pred;
+	*errorp = 0;
 	return (ds->clen++);
 }
 
@@ -788,17 +804,21 @@ sort(struct line *a, int n)
 	}
 }
 
-static void
+static int
 unsort(struct line *f, int l, int *b)
 {
 	int *a, i;
 
-	a = xcalloc(l + 1, sizeof(*a));
+	a = calloc(l + 1, sizeof(*a));
+	if (a == NULL)
+		return (-1);
 	for (i = 1; i <= l; i++)
 		a[f[i].serial] = f[i].value;
 	for (i = 1; i <= l; i++)
 		b[i] = a[i];
 	free(a);
+
+	return (0);
 }
 
 static int
@@ -811,11 +831,12 @@ skipline(FILE *f)
 	return (i);
 }
 
-static void
+static int
 output(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
     const char *file1, FILE *f1, const char *file2, FILE *f2, int flags)
 {
 	int m, i0, i1, j0, j1;
+	int error = 0;
 
 	rewind(f1);
 	rewind(f2);
@@ -832,7 +853,10 @@ output(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
 				i1++;
 			j1 = ds->J[i1 + 1] - 1;
 			ds->J[i1] = j1;
-			change(outfile, ds, args, file1, f1, file2, f2, i0, i1, j0, j1, &flags);
+			error = change(outfile, ds, args, file1, f1, file2, f2,
+			    i0, i1, j0, j1, &flags);
+			if (error)
+				return (error);
 		}
 	} else {
 		for (i0 = m; i0 >= 1; i0 = i1 - 1) {
@@ -844,16 +868,23 @@ output(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
 				i1--;
 			j1 = ds->J[i1 - 1] + 1;
 			ds->J[i1] = j1;
-			change(outfile, ds, args, file1, f1, file2, f2, i1, i0, j1, j0, &flags);
+			change(outfile, ds, args, file1, f1, file2, f2, i1, i0,
+			    j1, j0, &flags);
+			if (error)
+				return (error);
 		}
 	}
-	if (m == 0)
-		change(outfile, ds, args, file1, f1, file2, f2, 1, 0, 1, ds->len[1], &flags);
+	if (m == 0) {
+		error = change(outfile, ds, args, file1, f1, file2, f2, 1, 0,
+		    1, ds->len[1], &flags);
+		if (error)
+			return (error);
+	}
 	if (args->diff_format == D_IFDEF) {
 		for (;;) {
 #define	c i0
 			if ((c = getc(f1)) == EOF)
-				return;
+				return (0);
 			diff_output(outfile, "%c", c);
 		}
 #undef c
@@ -864,6 +895,8 @@ output(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
 		else if (args->diff_format == D_UNIFIED)
 			dump_unified_vec(outfile, ds, args, f1, f2, flags);
 	}
+
+	return (0);
 }
 
 static void
@@ -891,9 +924,13 @@ preadline(int fd, size_t rlen, off_t off)
 	char *line;
 	ssize_t nr;
 
-	line = xmalloc(rlen + 1);
-	if ((nr = pread(fd, line, rlen, off)) < 0)
-		err(2, "preadline");
+	line = malloc(rlen + 1);
+	if (line == NULL)
+		return NULL;
+	if ((nr = pread(fd, line, rlen, off)) < 0) {
+		free(line);
+		return NULL;
+	}
 	if (nr > 0 && line[nr-1] == '\n')
 		nr--;
 	line[nr] = '\0';
@@ -913,7 +950,7 @@ ignoreline(char *line)
  * lines appended (beginning at b).  If c is greater than d then there are
  * lines missing from the to file.
  */
-static void
+static int
 change(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
     const char *file1, FILE *f1, const char *file2, FILE *f2,
     int a, int b, int c, int d, int *pflags)
@@ -923,7 +960,7 @@ change(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
 
 restart:
 	if (args->diff_format != D_IFDEF && a > b && c > d)
-		return;
+		return (0);
 	if (args->ignore_pats != NULL) {
 		char *line;
 		/*
@@ -934,7 +971,10 @@ restart:
 		if (a <= b) {		/* Changes and deletes. */
 			for (i = a; i <= b; i++) {
 				line = preadline(fileno(f1),
-				    ds->ixold[i] - ds->ixold[i - 1], ds->ixold[i - 1]);
+				    ds->ixold[i] - ds->ixold[i - 1],
+				    ds->ixold[i - 1]);
+				if (line == NULL)
+					return (-1);
 				if (!ignoreline(line))
 					goto proceed;
 			}
@@ -942,12 +982,15 @@ restart:
 		if (a > b || c <= d) {	/* Changes and inserts. */
 			for (i = c; i <= d; i++) {
 				line = preadline(fileno(f2),
-				    ds->ixnew[i] - ds->ixnew[i - 1], ds->ixnew[i - 1]);
+				    ds->ixnew[i] - ds->ixnew[i - 1],
+				    ds->ixnew[i - 1]);
+				if (line == NULL)
+					return (-1);
 				if (!ignoreline(line))
 					goto proceed;
 			}
 		}
-		return;
+		return (0);
 	}
 proceed:
 	if (*pflags & D_HEADER) {
@@ -961,8 +1004,11 @@ proceed:
 		if (ds->context_vec_ptr == ds->context_vec_end - 1) {
 			ptrdiff_t offset = ds->context_vec_ptr - ds->context_vec_start;
 			max_context <<= 1;
-			ds->context_vec_start = xreallocarray(ds->context_vec_start,
-			    max_context, sizeof(*ds->context_vec_start));
+			ds->context_vec_start =
+			    reallocarray(ds->context_vec_start, max_context,
+				sizeof(*ds->context_vec_start));
+			if (ds->context_vec_start == NULL)
+				return (-1);
 			ds->context_vec_end = ds->context_vec_start + max_context;
 			ds->context_vec_ptr = ds->context_vec_start + offset;
 		}
@@ -988,13 +1034,13 @@ proceed:
 		ds->context_vec_ptr->b = b;
 		ds->context_vec_ptr->c = c;
 		ds->context_vec_ptr->d = d;
-		return;
+		return (0);
 	}
 	if (ds->anychange == 0)
 		ds->anychange = 1;
 	switch (args->diff_format) {
 	case D_BRIEF:
-		return;
+		return (0);
 	case D_NORMAL:
 	case D_EDIT:
 		range(outfile, a, b, ",");
@@ -1046,6 +1092,8 @@ proceed:
 		diff_output(outfile, "#endif /* %s */\n", args->ifdefname);
 		ds->inifdef = 0;
 	}
+
+	return (0);
 }
 
 static int
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
deleted file mode 100644
index a17c3fe..0000000
--- a/lib/xmalloc.c
+++ /dev/null
@@ -1,85 +0,0 @@
-/* $OpenBSD: xmalloc.c,v 1.9 2015/11/17 18:25:02 tobias Exp $ */
-/*
- * Author: Tatu Ylonen <ylo@cs.hut.fi>
- * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
- *                    All rights reserved
- * Versions of malloc and friends that check their results, and never return
- * failure (they call fatal if they encounter an error).
- *
- * As far as I am concerned, the code I have written for this software
- * can be used freely for any purpose.  Any derived versions of this
- * software must be clearly marked as such, and if the derived work is
- * incompatible with the protocol description in the RFC file, it must be
- * called by a name other than "ssh" or "Secure Shell".
- */
-
-#include <err.h>
-#include <stdarg.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "xmalloc.h"
-
-void *
-xmalloc(size_t size)
-{
-	void *ptr;
-
-	if (size == 0)
-		errx(2, "xmalloc: zero size");
-	ptr = malloc(size);
-	if (ptr == NULL)
-		err(2, "xmalloc: allocating %zu bytes", size);
-	return ptr;
-}
-
-void *
-xcalloc(size_t nmemb, size_t size)
-{
-	void *ptr;
-
-	ptr = calloc(nmemb, size);
-	if (ptr == NULL)
-		err(2, "xcalloc: allocating %zu * %zu bytes", nmemb, size);
-	return ptr;
-}
-
-void *
-xreallocarray(void *ptr, size_t nmemb, size_t size)
-{
-	void *new_ptr;
-
-	new_ptr = reallocarray(ptr, nmemb, size);
-	if (new_ptr == NULL)
-		err(2, "xreallocarray: allocating %zu * %zu bytes",
-		    nmemb, size);
-	return new_ptr;
-}
-
-char *
-xstrdup(const char *str)
-{
-	char *cp;
-
-	if ((cp = strdup(str)) == NULL)
-		err(2, "xstrdup");
-	return cp;
-}
-
-int
-xasprintf(char **ret, const char *fmt, ...)
-{
-	va_list ap;
-	int i;
-
-	va_start(ap, fmt);
-	i = vasprintf(ret, fmt, ap);
-	va_end(ap);
-
-	if (i < 0 || *ret == NULL)
-		err(2, "xasprintf");
-
-	return i;
-}
diff --git a/lib/xmalloc.h b/lib/xmalloc.h
deleted file mode 100644
index 2139671..0000000
--- a/lib/xmalloc.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/* $OpenBSD: xmalloc.h,v 1.4 2015/11/12 16:30:30 mmcc Exp $ */
-
-/*
- * Author: Tatu Ylonen <ylo@cs.hut.fi>
- * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
- *                    All rights reserved
- * Created: Mon Mar 20 22:09:17 1995 ylo
- *
- * Versions of malloc and friends that check their results, and never return
- * failure (they call fatal if they encounter an error).
- *
- * As far as I am concerned, the code I have written for this software
- * can be used freely for any purpose.  Any derived versions of this
- * software must be clearly marked as such, and if the derived work is
- * incompatible with the protocol description in the RFC file, it must be
- * called by a name other than "ssh" or "Secure Shell".
- */
-
-#ifndef XMALLOC_H
-#define XMALLOC_H
-
-void	*xmalloc(size_t);
-void	*xcalloc(size_t, size_t);
-void	*xreallocarray(void *, size_t, size_t);
-char	*xstrdup(const char *);
-int	 xasprintf(char **, const char *, ...)
-                __attribute__((__format__ (printf, 2, 3)))
-                __attribute__((__nonnull__ (2)));
-
-#endif	/* XMALLOC_H */
diff --git a/regress/repository/Makefile b/regress/repository/Makefile
index c506dc7..ca55e0e 100644
--- a/regress/repository/Makefile
+++ b/regress/repository/Makefile
@@ -2,7 +2,7 @@
 
 PROG = repository_test
 SRCS = path.c repository.c error.c refs.c object.c sha1.c diff.c \
-	diffreg.c xmalloc.c pack.c delta.c repository_test.c
+	diffreg.c pack.c delta.c repository_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include
 LDADD = -lutil -lz